pre-commit 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/sh
  2. #
  3. # An example hook script to verify what is about to be committed.
  4. # Called by git-commit with no arguments. The hook should
  5. # exit with non-zero status after issuing an appropriate message if
  6. # it wants to stop the commit.
  7. #
  8. # To enable this hook, make this file executable.
  9. # This is slightly modified from Andrew Morton's Perfect Patch.
  10. # Lines you introduce should not have trailing whitespace.
  11. # Also check for an indentation that has SP before a TAB.
  12. if git-rev-parse --verify HEAD 2>/dev/null
  13. then
  14. git-diff-index -p -M --cached HEAD
  15. else
  16. # NEEDSWORK: we should produce a diff with an empty tree here
  17. # if we want to do the same verification for the initial import.
  18. :
  19. fi |
  20. perl -e '
  21. my $found_bad = 0;
  22. my $filename;
  23. my $reported_filename = "";
  24. my $lineno;
  25. sub bad_line {
  26. my ($why, $line) = @_;
  27. if (!$found_bad) {
  28. print STDERR "*\n";
  29. print STDERR "* You have some suspicious patch lines:\n";
  30. print STDERR "*\n";
  31. $found_bad = 1;
  32. }
  33. if ($reported_filename ne $filename) {
  34. print STDERR "* In $filename\n";
  35. $reported_filename = $filename;
  36. }
  37. print STDERR "* $why (line $lineno)\n";
  38. print STDERR "$filename:$lineno:$line\n";
  39. }
  40. while (<>) {
  41. if (m|^diff --git a/(.*) b/\1$|) {
  42. $filename = $1;
  43. next;
  44. }
  45. if (/^@@ -\S+ \+(\d+)/) {
  46. $lineno = $1 - 1;
  47. next;
  48. }
  49. if (/^ /) {
  50. $lineno++;
  51. next;
  52. }
  53. if (s/^\+//) {
  54. $lineno++;
  55. chomp;
  56. if (/\s$/) {
  57. bad_line("trailing whitespace", $_);
  58. }
  59. if (/^\s* /) {
  60. bad_line("indent SP followed by a TAB", $_);
  61. }
  62. if (/^(?:[<>=]){7}/) {
  63. bad_line("unresolved merge conflict", $_);
  64. }
  65. }
  66. }
  67. exit($found_bad);
  68. '