pre-rebase.sample 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2006, 2008 Junio C Hamano
  4. #
  5. # The "pre-rebase" hook is run just before "git rebase" starts doing
  6. # its job, and can prevent the command from running by exiting with
  7. # non-zero status.
  8. #
  9. # The hook is called with the following parameters:
  10. #
  11. # $1 -- the upstream the series was forked from.
  12. # $2 -- the branch being rebased (or empty when rebasing the current branch).
  13. #
  14. # This sample shows how to prevent topic branches that are already
  15. # merged to 'next' branch from getting rebased, because allowing it
  16. # would result in rebasing already published history.
  17. publish=next
  18. basebranch="$1"
  19. if test "$#" = 2
  20. then
  21. topic="refs/heads/$2"
  22. else
  23. topic=`git symbolic-ref HEAD` ||
  24. exit 0 ;# we do not interrupt rebasing detached HEAD
  25. fi
  26. case "$topic" in
  27. refs/heads/??/*)
  28. ;;
  29. *)
  30. exit 0 ;# we do not interrupt others.
  31. ;;
  32. esac
  33. # Now we are dealing with a topic branch being rebased
  34. # on top of master. Is it OK to rebase it?
  35. # Does the topic really exist?
  36. git show-ref -q "$topic" || {
  37. echo >&2 "No such branch $topic"
  38. exit 1
  39. }
  40. # Is topic fully merged to master?
  41. not_in_master=`git rev-list --pretty=oneline ^master "$topic"`
  42. if test -z "$not_in_master"
  43. then
  44. echo >&2 "$topic is fully merged to master; better remove it."
  45. exit 1 ;# we could allow it, but there is no point.
  46. fi
  47. # Is topic ever merged to next? If so you should not be rebasing it.
  48. only_next_1=`git rev-list ^master "^$topic" ${publish} | sort`
  49. only_next_2=`git rev-list ^master ${publish} | sort`
  50. if test "$only_next_1" = "$only_next_2"
  51. then
  52. not_in_topic=`git rev-list "^$topic" master`
  53. if test -z "$not_in_topic"
  54. then
  55. echo >&2 "$topic is already up-to-date with master"
  56. exit 1 ;# we could allow it, but there is no point.
  57. else
  58. exit 0
  59. fi
  60. else
  61. not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"`
  62. /usr/bin/perl -e '
  63. my $topic = $ARGV[0];
  64. my $msg = "* $topic has commits already merged to public branch:\n";
  65. my (%not_in_next) = map {
  66. /^([0-9a-f]+) /;
  67. ($1 => 1);
  68. } split(/\n/, $ARGV[1]);
  69. for my $elem (map {
  70. /^([0-9a-f]+) (.*)$/;
  71. [$1 => $2];
  72. } split(/\n/, $ARGV[2])) {
  73. if (!exists $not_in_next{$elem->[0]}) {
  74. if ($msg) {
  75. print STDERR $msg;
  76. undef $msg;
  77. }
  78. print STDERR " $elem->[1]\n";
  79. }
  80. }
  81. ' "$topic" "$not_in_next" "$not_in_master"
  82. exit 1
  83. fi
  84. exit 0
  85. ################################################################
  86. This sample hook safeguards topic branches that have been
  87. published from being rewound.
  88. The workflow assumed here is:
  89. * Once a topic branch forks from "master", "master" is never
  90. merged into it again (either directly or indirectly).
  91. * Once a topic branch is fully cooked and merged into "master",
  92. it is deleted. If you need to build on top of it to correct
  93. earlier mistakes, a new topic branch is created by forking at
  94. the tip of the "master". This is not strictly necessary, but
  95. it makes it easier to keep your history simple.
  96. * Whenever you need to test or publish your changes to topic
  97. branches, merge them into "next" branch.
  98. The script, being an example, hardcodes the publish branch name
  99. to be "next", but it is trivial to make it configurable via
  100. $GIT_DIR/config mechanism.
  101. With this workflow, you would want to know:
  102. (1) ... if a topic branch has ever been merged to "next". Young
  103. topic branches can have stupid mistakes you would rather
  104. clean up before publishing, and things that have not been
  105. merged into other branches can be easily rebased without
  106. affecting other people. But once it is published, you would
  107. not want to rewind it.
  108. (2) ... if a topic branch has been fully merged to "master".
  109. Then you can delete it. More importantly, you should not
  110. build on top of it -- other people may already want to
  111. change things related to the topic as patches against your
  112. "master", so if you need further changes, it is better to
  113. fork the topic (perhaps with the same name) afresh from the
  114. tip of "master".
  115. Let's look at this example:
  116. o---o---o---o---o---o---o---o---o---o "next"
  117. / / / /
  118. / a---a---b A / /
  119. / / / /
  120. / / c---c---c---c B /
  121. / / / \ /
  122. / / / b---b C \ /
  123. / / / / \ /
  124. ---o---o---o---o---o---o---o---o---o---o---o "master"
  125. A, B and C are topic branches.
  126. * A has one fix since it was merged up to "next".
  127. * B has finished. It has been fully merged up to "master" and "next",
  128. and is ready to be deleted.
  129. * C has not merged to "next" at all.
  130. We would want to allow C to be rebased, refuse A, and encourage
  131. B to be deleted.
  132. To compute (1):
  133. git rev-list ^master ^topic next
  134. git rev-list ^master next
  135. if these match, topic has not merged in next at all.
  136. To compute (2):
  137. git rev-list master..topic
  138. if this is empty, it is fully merged to "master".