THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pre-rebase.sample 4.8KB

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. <<\DOC_END
  85. This sample hook safeguards topic branches that have been
  86. published from being rewound.
  87. The workflow assumed here is:
  88. * Once a topic branch forks from "master", "master" is never
  89. merged into it again (either directly or indirectly).
  90. * Once a topic branch is fully cooked and merged into "master",
  91. it is deleted. If you need to build on top of it to correct
  92. earlier mistakes, a new topic branch is created by forking at
  93. the tip of the "master". This is not strictly necessary, but
  94. it makes it easier to keep your history simple.
  95. * Whenever you need to test or publish your changes to topic
  96. branches, merge them into "next" branch.
  97. The script, being an example, hardcodes the publish branch name
  98. to be "next", but it is trivial to make it configurable via
  99. $GIT_DIR/config mechanism.
  100. With this workflow, you would want to know:
  101. (1) ... if a topic branch has ever been merged to "next". Young
  102. topic branches can have stupid mistakes you would rather
  103. clean up before publishing, and things that have not been
  104. merged into other branches can be easily rebased without
  105. affecting other people. But once it is published, you would
  106. not want to rewind it.
  107. (2) ... if a topic branch has been fully merged to "master".
  108. Then you can delete it. More importantly, you should not
  109. build on top of it -- other people may already want to
  110. change things related to the topic as patches against your
  111. "master", so if you need further changes, it is better to
  112. fork the topic (perhaps with the same name) afresh from the
  113. tip of "master".
  114. Let's look at this example:
  115. o---o---o---o---o---o---o---o---o---o "next"
  116. / / / /
  117. / a---a---b A / /
  118. / / / /
  119. / / c---c---c---c B /
  120. / / / \ /
  121. / / / b---b C \ /
  122. / / / / \ /
  123. ---o---o---o---o---o---o---o---o---o---o---o "master"
  124. A, B and C are topic branches.
  125. * A has one fix since it was merged up to "next".
  126. * B has finished. It has been fully merged up to "master" and "next",
  127. and is ready to be deleted.
  128. * C has not merged to "next" at all.
  129. We would want to allow C to be rebased, refuse A, and encourage
  130. B to be deleted.
  131. To compute (1):
  132. git rev-list ^master ^topic next
  133. git rev-list ^master next
  134. if these match, topic has not merged in next at all.
  135. To compute (2):
  136. git rev-list master..topic
  137. if this is empty, it is fully merged to "master".
  138. DOC_END