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