annotate BSseeker2/.git/hooks/pre-rebase.sample @ 1:8b26adf64adc draft default tip

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