annotate myTools/.git/hooks/pre-rebase.sample @ 6:f9f71cf4c3e3

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