yading@10: #!/bin/sh yading@10: # yading@10: # Copyright (c) 2006, 2008 Junio C Hamano yading@10: # yading@10: # The "pre-rebase" hook is run just before "git rebase" starts doing yading@10: # its job, and can prevent the command from running by exiting with yading@10: # non-zero status. yading@10: # yading@10: # The hook is called with the following parameters: yading@10: # yading@10: # $1 -- the upstream the series was forked from. yading@10: # $2 -- the branch being rebased (or empty when rebasing the current branch). yading@10: # yading@10: # This sample shows how to prevent topic branches that are already yading@10: # merged to 'next' branch from getting rebased, because allowing it yading@10: # would result in rebasing already published history. yading@10: yading@10: publish=next yading@10: basebranch="$1" yading@10: if test "$#" = 2 yading@10: then yading@10: topic="refs/heads/$2" yading@10: else yading@10: topic=`git symbolic-ref HEAD` || yading@10: exit 0 ;# we do not interrupt rebasing detached HEAD yading@10: fi yading@10: yading@10: case "$topic" in yading@10: refs/heads/??/*) yading@10: ;; yading@10: *) yading@10: exit 0 ;# we do not interrupt others. yading@10: ;; yading@10: esac yading@10: yading@10: # Now we are dealing with a topic branch being rebased yading@10: # on top of master. Is it OK to rebase it? yading@10: yading@10: # Does the topic really exist? yading@10: git show-ref -q "$topic" || { yading@10: echo >&2 "No such branch $topic" yading@10: exit 1 yading@10: } yading@10: yading@10: # Is topic fully merged to master? yading@10: not_in_master=`git rev-list --pretty=oneline ^master "$topic"` yading@10: if test -z "$not_in_master" yading@10: then yading@10: echo >&2 "$topic is fully merged to master; better remove it." yading@10: exit 1 ;# we could allow it, but there is no point. yading@10: fi yading@10: yading@10: # Is topic ever merged to next? If so you should not be rebasing it. yading@10: only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` yading@10: only_next_2=`git rev-list ^master ${publish} | sort` yading@10: if test "$only_next_1" = "$only_next_2" yading@10: then yading@10: not_in_topic=`git rev-list "^$topic" master` yading@10: if test -z "$not_in_topic" yading@10: then yading@10: echo >&2 "$topic is already up-to-date with master" yading@10: exit 1 ;# we could allow it, but there is no point. yading@10: else yading@10: exit 0 yading@10: fi yading@10: else yading@10: not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` yading@10: /usr/bin/perl -e ' yading@10: my $topic = $ARGV[0]; yading@10: my $msg = "* $topic has commits already merged to public branch:\n"; yading@10: my (%not_in_next) = map { yading@10: /^([0-9a-f]+) /; yading@10: ($1 => 1); yading@10: } split(/\n/, $ARGV[1]); yading@10: for my $elem (map { yading@10: /^([0-9a-f]+) (.*)$/; yading@10: [$1 => $2]; yading@10: } split(/\n/, $ARGV[2])) { yading@10: if (!exists $not_in_next{$elem->[0]}) { yading@10: if ($msg) { yading@10: print STDERR $msg; yading@10: undef $msg; yading@10: } yading@10: print STDERR " $elem->[1]\n"; yading@10: } yading@10: } yading@10: ' "$topic" "$not_in_next" "$not_in_master" yading@10: exit 1 yading@10: fi yading@10: yading@10: exit 0 yading@10: yading@10: ################################################################ yading@10: yading@10: This sample hook safeguards topic branches that have been yading@10: published from being rewound. yading@10: yading@10: The workflow assumed here is: yading@10: yading@10: * Once a topic branch forks from "master", "master" is never yading@10: merged into it again (either directly or indirectly). yading@10: yading@10: * Once a topic branch is fully cooked and merged into "master", yading@10: it is deleted. If you need to build on top of it to correct yading@10: earlier mistakes, a new topic branch is created by forking at yading@10: the tip of the "master". This is not strictly necessary, but yading@10: it makes it easier to keep your history simple. yading@10: yading@10: * Whenever you need to test or publish your changes to topic yading@10: branches, merge them into "next" branch. yading@10: yading@10: The script, being an example, hardcodes the publish branch name yading@10: to be "next", but it is trivial to make it configurable via yading@10: $GIT_DIR/config mechanism. yading@10: yading@10: With this workflow, you would want to know: yading@10: yading@10: (1) ... if a topic branch has ever been merged to "next". Young yading@10: topic branches can have stupid mistakes you would rather yading@10: clean up before publishing, and things that have not been yading@10: merged into other branches can be easily rebased without yading@10: affecting other people. But once it is published, you would yading@10: not want to rewind it. yading@10: yading@10: (2) ... if a topic branch has been fully merged to "master". yading@10: Then you can delete it. More importantly, you should not yading@10: build on top of it -- other people may already want to yading@10: change things related to the topic as patches against your yading@10: "master", so if you need further changes, it is better to yading@10: fork the topic (perhaps with the same name) afresh from the yading@10: tip of "master". yading@10: yading@10: Let's look at this example: yading@10: yading@10: o---o---o---o---o---o---o---o---o---o "next" yading@10: / / / / yading@10: / a---a---b A / / yading@10: / / / / yading@10: / / c---c---c---c B / yading@10: / / / \ / yading@10: / / / b---b C \ / yading@10: / / / / \ / yading@10: ---o---o---o---o---o---o---o---o---o---o---o "master" yading@10: yading@10: yading@10: A, B and C are topic branches. yading@10: yading@10: * A has one fix since it was merged up to "next". yading@10: yading@10: * B has finished. It has been fully merged up to "master" and "next", yading@10: and is ready to be deleted. yading@10: yading@10: * C has not merged to "next" at all. yading@10: yading@10: We would want to allow C to be rebased, refuse A, and encourage yading@10: B to be deleted. yading@10: yading@10: To compute (1): yading@10: yading@10: git rev-list ^master ^topic next yading@10: git rev-list ^master next yading@10: yading@10: if these match, topic has not merged in next at all. yading@10: yading@10: To compute (2): yading@10: yading@10: git rev-list master..topic yading@10: yading@10: if this is empty, it is fully merged to "master".