]>
Commit | Line | Data |
---|---|---|
1b1dce4b JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
4 | ||
5 | # SHORT DESCRIPTION | |
6 | # | |
7 | # This script makes it easy to fix up commits in the middle of a series, | |
8 | # and rearrange commits. | |
9 | # | |
10 | # The original idea comes from Eric W. Biederman, in | |
11 | # http://article.gmane.org/gmane.comp.version-control.git/22407 | |
12 | ||
7970aaf0 SB |
13 | OPTIONS_KEEPDASHDASH= |
14 | OPTIONS_SPEC="\ | |
15 | git-rebase [-i] [options] [--] <upstream> [<branch>] | |
16 | git-rebase [-i] (--continue | --abort | --skip) | |
17 | -- | |
18 | Available options are | |
19 | v,verbose display a diffstat of what changed upstream | |
20 | onto= rebase onto given branch instead of upstream | |
21 | p,preserve-merges try to recreate merges instead of ignoring them | |
22 | s,strategy= use the given merge strategy | |
23 | m,merge always used (no-op) | |
24 | i,interactive always used (no-op) | |
25 | Actions: | |
26 | continue continue rebasing process | |
27 | abort abort rebasing process and restore original branch | |
28 | skip skip current patch and continue rebasing process | |
c4427656 | 29 | no-verify override pre-rebase hook from stopping the operation |
d911d146 | 30 | root rebase all reachable commmits up to the root(s) |
7970aaf0 | 31 | " |
1b1dce4b JS |
32 | |
33 | . git-sh-setup | |
34 | require_work_tree | |
35 | ||
28ed6e7b | 36 | DOTEST="$GIT_DIR/rebase-merge" |
80883bb3 MH |
37 | |
38 | # The file containing rebase commands, comments, and empty lines. | |
39 | # This file is created by "git rebase -i" then edited by the user. As | |
40 | # the lines are processed, they are removed from the front of this | |
41 | # file and written to the tail of $DONE. | |
c22486c9 | 42 | TODO="$DOTEST"/git-rebase-todo |
80883bb3 MH |
43 | |
44 | # The rebase command lines that have already been processed. A line | |
45 | # is moved here when it is first handled, before any associated user | |
46 | # actions. | |
1b1dce4b | 47 | DONE="$DOTEST"/done |
80883bb3 MH |
48 | |
49 | # The commit message that is planned to be used for any changes that | |
50 | # need to be committed following a user interaction. | |
6368f3f8 | 51 | MSG="$DOTEST"/message |
80883bb3 MH |
52 | |
53 | # The file into which is accumulated the suggested commit message for | |
54 | # squash/fixup commands. When the first of a series of squash/fixups | |
55 | # is seen, the file is created and the commit message from the | |
56 | # previous commit and from the first squash/fixup commit are written | |
57 | # to it. The commit message for each subsequent squash/fixup commit | |
58 | # is appended to the file as it is processed. | |
59 | # | |
60 | # The first line of the file is of the form | |
61 | # # This is a combination of $COUNT commits. | |
62 | # where $COUNT is the number of commits whose messages have been | |
63 | # written to the file so far (including the initial "pick" commit). | |
64 | # Each time that a commit message is processed, this line is read and | |
65 | # updated. It is deleted just before the combined commit is made. | |
6368f3f8 | 66 | SQUASH_MSG="$DOTEST"/message-squash |
80883bb3 MH |
67 | |
68 | # $REWRITTEN is the name of a directory containing files for each | |
69 | # commit that is reachable by at least one merge base of $HEAD and | |
70 | # $UPSTREAM. They are not necessarily rewritten, but their children | |
71 | # might be. This ensures that commits on merged, but otherwise | |
72 | # unrelated side branches are left alone. (Think "X" in the man page's | |
73 | # example.) | |
f09c9b8c | 74 | REWRITTEN="$DOTEST"/rewritten |
80883bb3 | 75 | |
faae853c | 76 | DROPPED="$DOTEST"/dropped |
0aac0de4 MH |
77 | |
78 | # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
79 | # GIT_AUTHOR_DATE that will be used for the commit that is currently | |
80 | # being rebased. | |
81 | AUTHOR_SCRIPT="$DOTEST"/author-script | |
82 | ||
a4049ae7 MH |
83 | # When an "edit" rebase command is being processed, the SHA1 of the |
84 | # commit to be edited is recorded in this file. When "git rebase | |
85 | # --continue" is executed, if there are any staged changes then they | |
86 | # will be amended to the HEAD commit, but only provided the HEAD | |
87 | # commit is still the commit to be edited. When any other rebase | |
88 | # command is processed, this file is deleted. | |
89 | AMEND="$DOTEST"/amend | |
90 | ||
f09c9b8c | 91 | PRESERVE_MERGES= |
1b1dce4b | 92 | STRATEGY= |
7970aaf0 | 93 | ONTO= |
1b1dce4b | 94 | VERBOSE= |
c4427656 | 95 | OK_TO_SKIP_PRE_REBASE= |
d911d146 | 96 | REBASE_ROOT= |
1b1dce4b | 97 | |
804c7174 WC |
98 | GIT_CHERRY_PICK_HELP=" After resolving the conflicts, |
99 | mark the corrected paths with 'git add <paths>', and | |
100 | run 'git rebase --continue'" | |
101 | export GIT_CHERRY_PICK_HELP | |
102 | ||
1b1dce4b JS |
103 | warn () { |
104 | echo "$*" >&2 | |
105 | } | |
106 | ||
dfa49f33 JS |
107 | output () { |
108 | case "$VERBOSE" in | |
109 | '') | |
5166810b | 110 | output=$("$@" 2>&1 ) |
dfa49f33 | 111 | status=$? |
5166810b | 112 | test $status != 0 && printf "%s\n" "$output" |
dfa49f33 | 113 | return $status |
376ccb8c | 114 | ;; |
dfa49f33 JS |
115 | *) |
116 | "$@" | |
376ccb8c | 117 | ;; |
dfa49f33 JS |
118 | esac |
119 | } | |
120 | ||
d70b4a8f | 121 | run_pre_rebase_hook () { |
c4427656 NS |
122 | if test -z "$OK_TO_SKIP_PRE_REBASE" && |
123 | test -x "$GIT_DIR/hooks/pre-rebase" | |
d70b4a8f NS |
124 | then |
125 | "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || { | |
126 | echo >&2 "The pre-rebase hook refused to rebase." | |
127 | exit 1 | |
128 | } | |
129 | fi | |
130 | } | |
131 | ||
1b1dce4b JS |
132 | require_clean_work_tree () { |
133 | # test if working tree is dirty | |
134 | git rev-parse --verify HEAD > /dev/null && | |
6848d58c JS |
135 | git update-index --ignore-submodules --refresh && |
136 | git diff-files --quiet --ignore-submodules && | |
137 | git diff-index --cached --quiet HEAD --ignore-submodules -- || | |
1b1dce4b JS |
138 | die "Working tree is dirty" |
139 | } | |
140 | ||
141 | ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION" | |
142 | ||
143 | comment_for_reflog () { | |
144 | case "$ORIG_REFLOG_ACTION" in | |
145 | ''|rebase*) | |
146 | GIT_REFLOG_ACTION="rebase -i ($1)" | |
147 | export GIT_REFLOG_ACTION | |
376ccb8c | 148 | ;; |
1b1dce4b JS |
149 | esac |
150 | } | |
151 | ||
4e673877 | 152 | last_count= |
1b1dce4b JS |
153 | mark_action_done () { |
154 | sed -e 1q < "$TODO" >> "$DONE" | |
155 | sed -e 1d < "$TODO" >> "$TODO".new | |
156 | mv -f "$TODO".new "$TODO" | |
e1622bfc JH |
157 | count=$(sane_grep -c '^[^#]' < "$DONE") |
158 | total=$(($count+$(sane_grep -c '^[^#]' < "$TODO"))) | |
4e673877 JH |
159 | if test "$last_count" != "$count" |
160 | then | |
161 | last_count=$count | |
162 | printf "Rebasing (%d/%d)\r" $count $total | |
163 | test -z "$VERBOSE" || echo | |
164 | fi | |
1b1dce4b JS |
165 | } |
166 | ||
167 | make_patch () { | |
4fb1a19d JS |
168 | sha1_and_parents="$(git rev-list --parents -1 "$1")" |
169 | case "$sha1_and_parents" in | |
170 | ?*' '?*' '?*) | |
171 | git diff --cc $sha1_and_parents | |
172 | ;; | |
173 | ?*' '?*) | |
174 | git diff-tree -p "$1^!" | |
175 | ;; | |
176 | *) | |
177 | echo "Root commit" | |
178 | ;; | |
179 | esac > "$DOTEST"/patch | |
bdb011ad MH |
180 | test -f "$MSG" || |
181 | git cat-file commit "$1" | sed "1,/^$/d" > "$MSG" | |
0aac0de4 MH |
182 | test -f "$AUTHOR_SCRIPT" || |
183 | get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT" | |
1b1dce4b JS |
184 | } |
185 | ||
186 | die_with_patch () { | |
187 | make_patch "$1" | |
ecfe72ff | 188 | git rerere |
1b1dce4b JS |
189 | die "$2" |
190 | } | |
191 | ||
c54b7817 JS |
192 | die_abort () { |
193 | rm -rf "$DOTEST" | |
194 | die "$1" | |
195 | } | |
196 | ||
376ccb8c | 197 | has_action () { |
e1622bfc | 198 | sane_grep '^[^#]' "$1" >/dev/null |
376ccb8c JS |
199 | } |
200 | ||
1b1dce4b | 201 | pick_one () { |
1d25c8cf JS |
202 | no_ff= |
203 | case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac | |
dfa49f33 | 204 | output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1" |
f09c9b8c JS |
205 | test -d "$REWRITTEN" && |
206 | pick_one_preserving_merges "$@" && return | |
699f13ca | 207 | if test -n "$REBASE_ROOT" |
d911d146 TR |
208 | then |
209 | output git cherry-pick "$@" | |
210 | return | |
211 | fi | |
376ccb8c JS |
212 | parent_sha1=$(git rev-parse --verify $sha1^) || |
213 | die "Could not get the parent of $sha1" | |
1b1dce4b | 214 | current_sha1=$(git rev-parse --verify HEAD) |
1d621fea MH |
215 | if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1" |
216 | then | |
dfa49f33 | 217 | output git reset --hard $sha1 |
aa7eaff8 | 218 | output warn Fast-forward to $(git rev-parse --short $sha1) |
1b1dce4b | 219 | else |
2a9c53e0 | 220 | output git cherry-pick "$@" |
1b1dce4b JS |
221 | fi |
222 | } | |
223 | ||
f09c9b8c | 224 | pick_one_preserving_merges () { |
71d9451e TR |
225 | fast_forward=t |
226 | case "$1" in | |
227 | -n) | |
228 | fast_forward=f | |
229 | sha1=$2 | |
230 | ;; | |
231 | *) | |
232 | sha1=$1 | |
233 | ;; | |
234 | esac | |
f09c9b8c JS |
235 | sha1=$(git rev-parse $sha1) |
236 | ||
3b38ec16 | 237 | if test -f "$DOTEST"/current-commit |
f09c9b8c | 238 | then |
4c1360f4 | 239 | if test "$fast_forward" = t |
bb645071 SH |
240 | then |
241 | cat "$DOTEST"/current-commit | while read current_commit | |
242 | do | |
243 | git rev-parse HEAD > "$REWRITTEN"/$current_commit | |
244 | done | |
245 | rm "$DOTEST"/current-commit || | |
246 | die "Cannot write current commit's replacement sha1" | |
247 | fi | |
f09c9b8c JS |
248 | fi |
249 | ||
bb645071 | 250 | echo $sha1 >> "$DOTEST"/current-commit |
a96dc01e | 251 | |
f09c9b8c | 252 | # rewrite parents; if none were rewritten, we can fast-forward. |
f09c9b8c | 253 | new_parents= |
d911d146 TR |
254 | pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)" |
255 | if test "$pend" = " " | |
256 | then | |
257 | pend=" root" | |
258 | fi | |
faae853c | 259 | while [ "$pend" != "" ] |
f09c9b8c | 260 | do |
faae853c SH |
261 | p=$(expr "$pend" : ' \([^ ]*\)') |
262 | pend="${pend# $p}" | |
263 | ||
3b38ec16 | 264 | if test -f "$REWRITTEN"/$p |
f09c9b8c | 265 | then |
f09c9b8c | 266 | new_p=$(cat "$REWRITTEN"/$p) |
80fe82e4 SH |
267 | |
268 | # If the todo reordered commits, and our parent is marked for | |
269 | # rewriting, but hasn't been gotten to yet, assume the user meant to | |
270 | # drop it on top of the current HEAD | |
271 | if test -z "$new_p" | |
272 | then | |
273 | new_p=$(git rev-parse HEAD) | |
274 | fi | |
275 | ||
f09c9b8c JS |
276 | test $p != $new_p && fast_forward=f |
277 | case "$new_parents" in | |
278 | *$new_p*) | |
279 | ;; # do nothing; that parent is already there | |
280 | *) | |
281 | new_parents="$new_parents $new_p" | |
376ccb8c | 282 | ;; |
f09c9b8c | 283 | esac |
1c5fa0a1 | 284 | else |
faae853c SH |
285 | if test -f "$DROPPED"/$p |
286 | then | |
287 | fast_forward=f | |
d911d146 TR |
288 | replacement="$(cat "$DROPPED"/$p)" |
289 | test -z "$replacement" && replacement=root | |
290 | pend=" $replacement$pend" | |
faae853c SH |
291 | else |
292 | new_parents="$new_parents $p" | |
293 | fi | |
f09c9b8c JS |
294 | fi |
295 | done | |
296 | case $fast_forward in | |
297 | t) | |
a75d7b54 | 298 | output warn "Fast-forward to $sha1" |
71d9451e | 299 | output git reset --hard $sha1 || |
a75d7b54 | 300 | die "Cannot fast-forward to $sha1" |
f09c9b8c JS |
301 | ;; |
302 | f) | |
376ccb8c | 303 | first_parent=$(expr "$new_parents" : ' \([^ ]*\)') |
a4f25e36 SH |
304 | |
305 | if [ "$1" != "-n" ] | |
306 | then | |
307 | # detach HEAD to current parent | |
308 | output git checkout $first_parent 2> /dev/null || | |
309 | die "Cannot move HEAD to $first_parent" | |
310 | fi | |
f09c9b8c | 311 | |
f09c9b8c | 312 | case "$new_parents" in |
376ccb8c | 313 | ' '*' '*) |
a4f25e36 SH |
314 | test "a$1" = a-n && die "Refusing to squash a merge: $sha1" |
315 | ||
f09c9b8c JS |
316 | # redo merge |
317 | author_script=$(get_author_ident_from_commit $sha1) | |
318 | eval "$author_script" | |
376ccb8c | 319 | msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')" |
f91333d6 BS |
320 | # No point in merging the first parent, that's HEAD |
321 | new_parents=${new_parents# $first_parent} | |
ae830ed7 JS |
322 | if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
323 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
324 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
325 | output git merge $STRATEGY -m "$msg" \ | |
326 | $new_parents | |
f09c9b8c | 327 | then |
376ccb8c | 328 | printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG |
f5b49ea6 | 329 | die_with_patch $sha1 "Error redoing merge $sha1" |
f09c9b8c JS |
330 | fi |
331 | ;; | |
332 | *) | |
2a9c53e0 | 333 | output git cherry-pick "$@" || |
f09c9b8c | 334 | die_with_patch $sha1 "Could not pick $sha1" |
376ccb8c | 335 | ;; |
f09c9b8c | 336 | esac |
376ccb8c | 337 | ;; |
f09c9b8c JS |
338 | esac |
339 | } | |
340 | ||
6368f3f8 JS |
341 | nth_string () { |
342 | case "$1" in | |
343 | *1[0-9]|*[04-9]) echo "$1"th;; | |
344 | *1) echo "$1"st;; | |
345 | *2) echo "$1"nd;; | |
346 | *3) echo "$1"rd;; | |
347 | esac | |
348 | } | |
349 | ||
350 | make_squash_message () { | |
3b38ec16 | 351 | if test -f "$SQUASH_MSG"; then |
f99e269c MH |
352 | COUNT=$(($(sed -n \ |
353 | -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \ | |
354 | -e "q" < "$SQUASH_MSG")+1)) | |
6368f3f8 | 355 | echo "# This is a combination of $COUNT commits." |
8ad1065e JH |
356 | sed -e 1d -e '2,/^./{ |
357 | /^$/d | |
358 | }' <"$SQUASH_MSG" | |
6368f3f8 JS |
359 | else |
360 | COUNT=2 | |
5065ed29 | 361 | echo "# This is a combination of 2 commits." |
6368f3f8 JS |
362 | echo "# The first commit's message is:" |
363 | echo | |
364 | git cat-file commit HEAD | sed -e '1,/^$/d' | |
6368f3f8 | 365 | fi |
0205e72f MH |
366 | case $1 in |
367 | squash) | |
368 | echo | |
369 | echo "# This is the $(nth_string $COUNT) commit message:" | |
370 | echo | |
371 | git cat-file commit $2 | sed -e '1,/^$/d' | |
372 | ;; | |
373 | fixup) | |
374 | echo | |
375 | echo "# The $(nth_string $COUNT) commit message will be skipped:" | |
376 | echo | |
0205e72f MH |
377 | git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /' |
378 | ;; | |
379 | esac | |
6368f3f8 JS |
380 | } |
381 | ||
382 | peek_next_command () { | |
234b3dae | 383 | sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO" |
6368f3f8 JS |
384 | } |
385 | ||
1b1dce4b | 386 | do_next () { |
a4049ae7 | 387 | rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit |
1b1dce4b JS |
388 | read command sha1 rest < "$TODO" |
389 | case "$command" in | |
ff74126c | 390 | '#'*|''|noop) |
1b1dce4b | 391 | mark_action_done |
1b1dce4b | 392 | ;; |
f8babc4d | 393 | pick|p) |
1b1dce4b JS |
394 | comment_for_reflog pick |
395 | ||
396 | mark_action_done | |
397 | pick_one $sha1 || | |
398 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
399 | ;; | |
6741aa6c BG |
400 | reword|r) |
401 | comment_for_reflog reword | |
402 | ||
403 | mark_action_done | |
404 | pick_one $sha1 || | |
405 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
7725cb5e | 406 | git commit --amend |
6741aa6c | 407 | ;; |
f8babc4d | 408 | edit|e) |
1b1dce4b JS |
409 | comment_for_reflog edit |
410 | ||
411 | mark_action_done | |
412 | pick_one $sha1 || | |
413 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
414 | make_patch $sha1 | |
a4049ae7 | 415 | git rev-parse --verify HEAD > "$AMEND" |
a8ccc204 | 416 | warn "Stopped at $sha1... $rest" |
1b1dce4b JS |
417 | warn "You can amend the commit now, with" |
418 | warn | |
419 | warn " git commit --amend" | |
420 | warn | |
0460fb44 JS |
421 | warn "Once you are satisfied with your changes, run" |
422 | warn | |
423 | warn " git rebase --continue" | |
424 | warn | |
1b1dce4b JS |
425 | exit 0 |
426 | ;; | |
0205e72f MH |
427 | squash|s|fixup|f) |
428 | case "$command" in | |
429 | squash|s) | |
430 | squash_style=squash | |
431 | ;; | |
432 | fixup|f) | |
433 | squash_style=fixup | |
434 | ;; | |
435 | esac | |
436 | comment_for_reflog $squash_style | |
1b1dce4b | 437 | |
12dd1112 | 438 | test -f "$DONE" && has_action "$DONE" || |
0205e72f | 439 | die "Cannot '$squash_style' without a previous commit" |
1b1dce4b JS |
440 | |
441 | mark_action_done | |
0205e72f | 442 | make_squash_message $squash_style $sha1 > "$MSG" |
7c418836 SG |
443 | failed=f |
444 | author_script=$(get_author_ident_from_commit HEAD) | |
445 | output git reset --soft HEAD^ | |
446 | pick_one -n $sha1 || failed=t | |
6368f3f8 | 447 | case "$(peek_next_command)" in |
0205e72f | 448 | squash|s|fixup|f) |
91e1ee77 | 449 | USE_OUTPUT=output |
7c418836 | 450 | MSG_OPT=-F |
94c88ede | 451 | EDIT_OR_FILE="$MSG" |
6368f3f8 | 452 | cp "$MSG" "$SQUASH_MSG" |
376ccb8c | 453 | ;; |
6368f3f8 | 454 | *) |
91e1ee77 | 455 | USE_OUTPUT= |
7c418836 | 456 | MSG_OPT= |
94c88ede | 457 | EDIT_OR_FILE=-e |
376ccb8c | 458 | rm -f "$SQUASH_MSG" || exit |
943cea90 | 459 | cp "$MSG" "$GIT_DIR"/SQUASH_MSG |
7c418836 | 460 | rm -f "$GIT_DIR"/MERGE_MSG || exit |
376ccb8c | 461 | ;; |
6368f3f8 | 462 | esac |
0aac0de4 | 463 | echo "$author_script" > "$AUTHOR_SCRIPT" |
dbedf972 SP |
464 | if test $failed = f |
465 | then | |
1b1dce4b JS |
466 | # This is like --amend, but with a different message |
467 | eval "$author_script" | |
ae830ed7 JS |
468 | GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
469 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
470 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
94c88ede JS |
471 | $USE_OUTPUT git commit --no-verify \ |
472 | $MSG_OPT "$EDIT_OR_FILE" || failed=t | |
dbedf972 SP |
473 | fi |
474 | if test $failed = t | |
475 | then | |
1b1dce4b JS |
476 | cp "$MSG" "$GIT_DIR"/MERGE_MSG |
477 | warn | |
478 | warn "Could not apply $sha1... $rest" | |
1b1dce4b | 479 | die_with_patch $sha1 "" |
dbedf972 | 480 | fi |
1b1dce4b JS |
481 | ;; |
482 | *) | |
483 | warn "Unknown command: $command $sha1 $rest" | |
f1be316a JK |
484 | if git rev-parse --verify -q "$sha1" >/dev/null |
485 | then | |
486 | die_with_patch $sha1 "Please fix this in the file $TODO." | |
487 | else | |
488 | die "Please fix this in the file $TODO." | |
489 | fi | |
376ccb8c | 490 | ;; |
1b1dce4b JS |
491 | esac |
492 | test -s "$TODO" && return | |
493 | ||
68a163c9 JS |
494 | comment_for_reflog finish && |
495 | HEADNAME=$(cat "$DOTEST"/head-name) && | |
496 | OLDHEAD=$(cat "$DOTEST"/head) && | |
497 | SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) && | |
72583e6c | 498 | NEWHEAD=$(git rev-parse HEAD) && |
73697a0b JS |
499 | case $HEADNAME in |
500 | refs/*) | |
a0c0447b | 501 | message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" && |
73697a0b JS |
502 | git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD && |
503 | git symbolic-ref HEAD $HEADNAME | |
504 | ;; | |
505 | esac && { | |
3df0a859 | 506 | test ! -f "$DOTEST"/verbose || |
f3d5e463 | 507 | git diff-tree --stat $(cat "$DOTEST"/head)..HEAD |
3df0a859 | 508 | } && |
1b1dce4b | 509 | rm -rf "$DOTEST" && |
73697a0b | 510 | git gc --auto && |
1b1dce4b JS |
511 | warn "Successfully rebased and updated $HEADNAME." |
512 | ||
513 | exit | |
514 | } | |
515 | ||
516 | do_rest () { | |
517 | while : | |
518 | do | |
519 | do_next | |
520 | done | |
1b1dce4b JS |
521 | } |
522 | ||
0e757e30 JS |
523 | # skip picking commits whose parents are unchanged |
524 | skip_unnecessary_picks () { | |
525 | fd=3 | |
526 | while read command sha1 rest | |
527 | do | |
528 | # fd=3 means we skip the command | |
529 | case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in | |
530 | 3,pick,"$ONTO"*|3,p,"$ONTO"*) | |
531 | # pick a commit whose parent is current $ONTO -> skip | |
532 | ONTO=$sha1 | |
533 | ;; | |
534 | 3,#*|3,,*) | |
535 | # copy comments | |
536 | ;; | |
537 | *) | |
538 | fd=1 | |
539 | ;; | |
540 | esac | |
541 | echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd | |
542 | done <"$TODO" >"$TODO.new" 3>>"$DONE" && | |
543 | mv -f "$TODO".new "$TODO" || | |
544 | die "Could not skip unnecessary pick commands" | |
545 | } | |
546 | ||
7970aaf0 SB |
547 | # check if no other options are set |
548 | is_standalone () { | |
549 | test $# -eq 2 -a "$2" = '--' && | |
550 | test -z "$ONTO" && | |
551 | test -z "$PRESERVE_MERGES" && | |
552 | test -z "$STRATEGY" && | |
553 | test -z "$VERBOSE" | |
554 | } | |
555 | ||
556 | get_saved_options () { | |
557 | test -d "$REWRITTEN" && PRESERVE_MERGES=t | |
558 | test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)" | |
559 | test -f "$DOTEST"/verbose && VERBOSE=t | |
a6c7a276 | 560 | test -f "$DOTEST"/rebase-root && REBASE_ROOT=t |
7970aaf0 SB |
561 | } |
562 | ||
822f7c73 | 563 | while test $# != 0 |
1b1dce4b JS |
564 | do |
565 | case "$1" in | |
c4427656 NS |
566 | --no-verify) |
567 | OK_TO_SKIP_PRE_REBASE=yes | |
568 | ;; | |
569 | --verify) | |
570 | ;; | |
1b1dce4b | 571 | --continue) |
7970aaf0 SB |
572 | is_standalone "$@" || usage |
573 | get_saved_options | |
1b1dce4b JS |
574 | comment_for_reflog continue |
575 | ||
576 | test -d "$DOTEST" || die "No interactive rebase running" | |
577 | ||
ab119032 JH |
578 | # Sanity check |
579 | git rev-parse --verify HEAD >/dev/null || | |
580 | die "Cannot read HEAD" | |
6848d58c JS |
581 | git update-index --ignore-submodules --refresh && |
582 | git diff-files --quiet --ignore-submodules || | |
ab119032 JH |
583 | die "Working tree is dirty" |
584 | ||
585 | # do we have anything to commit? | |
6848d58c | 586 | if git diff-index --cached --quiet --ignore-submodules HEAD -- |
03270628 | 587 | then |
ab119032 JH |
588 | : Nothing to commit -- skip this |
589 | else | |
0aac0de4 | 590 | . "$AUTHOR_SCRIPT" || |
ab119032 | 591 | die "Cannot find the author identity" |
8beb1f33 | 592 | amend= |
a4049ae7 | 593 | if test -f "$AMEND" |
ab119032 | 594 | then |
8beb1f33 | 595 | amend=$(git rev-parse --verify HEAD) |
a4049ae7 | 596 | test "$amend" = $(cat "$AMEND") || |
c14c3c82 DP |
597 | die "\ |
598 | You have uncommitted changes in your working tree. Please, commit them | |
599 | first and then run 'git rebase --continue' again." | |
ab119032 JH |
600 | git reset --soft HEAD^ || |
601 | die "Cannot rewind the HEAD" | |
602 | fi | |
603 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE && | |
bdb011ad | 604 | git commit --no-verify -F "$MSG" -e || { |
8beb1f33 DP |
605 | test -n "$amend" && git reset --soft $amend |
606 | die "Could not commit staged changes." | |
607 | } | |
03270628 | 608 | fi |
18640d99 | 609 | |
1b1dce4b JS |
610 | require_clean_work_tree |
611 | do_rest | |
612 | ;; | |
613 | --abort) | |
7970aaf0 SB |
614 | is_standalone "$@" || usage |
615 | get_saved_options | |
1b1dce4b JS |
616 | comment_for_reflog abort |
617 | ||
ecfe72ff | 618 | git rerere clear |
1b1dce4b JS |
619 | test -d "$DOTEST" || die "No interactive rebase running" |
620 | ||
621 | HEADNAME=$(cat "$DOTEST"/head-name) | |
622 | HEAD=$(cat "$DOTEST"/head) | |
73697a0b JS |
623 | case $HEADNAME in |
624 | refs/*) | |
625 | git symbolic-ref HEAD $HEADNAME | |
626 | ;; | |
627 | esac && | |
dfa49f33 | 628 | output git reset --hard $HEAD && |
1b1dce4b JS |
629 | rm -rf "$DOTEST" |
630 | exit | |
631 | ;; | |
632 | --skip) | |
7970aaf0 SB |
633 | is_standalone "$@" || usage |
634 | get_saved_options | |
1b1dce4b JS |
635 | comment_for_reflog skip |
636 | ||
ecfe72ff | 637 | git rerere clear |
1b1dce4b JS |
638 | test -d "$DOTEST" || die "No interactive rebase running" |
639 | ||
dfa49f33 | 640 | output git reset --hard && do_rest |
1b1dce4b | 641 | ;; |
7970aaf0 | 642 | -s) |
1b1dce4b JS |
643 | case "$#,$1" in |
644 | *,*=*) | |
b5e960b1 | 645 | STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;; |
1b1dce4b JS |
646 | 1,*) |
647 | usage ;; | |
648 | *) | |
649 | STRATEGY="-s $2" | |
650 | shift ;; | |
651 | esac | |
652 | ;; | |
7970aaf0 | 653 | -m) |
1b1dce4b JS |
654 | # we use merge anyway |
655 | ;; | |
7970aaf0 | 656 | -v) |
1b1dce4b JS |
657 | VERBOSE=t |
658 | ;; | |
7970aaf0 | 659 | -p) |
f09c9b8c JS |
660 | PRESERVE_MERGES=t |
661 | ;; | |
7970aaf0 | 662 | -i) |
1b1dce4b JS |
663 | # yeah, we know |
664 | ;; | |
d911d146 TR |
665 | --root) |
666 | REBASE_ROOT=t | |
667 | ;; | |
7970aaf0 SB |
668 | --onto) |
669 | shift | |
670 | ONTO=$(git rev-parse --verify "$1") || | |
671 | die "Does not point to a valid commit: $1" | |
1b1dce4b | 672 | ;; |
7970aaf0 SB |
673 | --) |
674 | shift | |
277d7e91 JS |
675 | test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 || |
676 | test ! -z "$REBASE_ROOT" -a $# -le 1 || usage | |
1b1dce4b JS |
677 | test -d "$DOTEST" && |
678 | die "Interactive rebase already started" | |
679 | ||
680 | git var GIT_COMMITTER_IDENT >/dev/null || | |
681 | die "You need to set your committer info first" | |
682 | ||
d911d146 TR |
683 | if test -z "$REBASE_ROOT" |
684 | then | |
685 | UPSTREAM_ARG="$1" | |
686 | UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base" | |
687 | test -z "$ONTO" && ONTO=$UPSTREAM | |
688 | shift | |
689 | else | |
a6c7a276 | 690 | UPSTREAM= |
d911d146 TR |
691 | UPSTREAM_ARG=--root |
692 | test -z "$ONTO" && | |
693 | die "You must specify --onto when using --root" | |
694 | fi | |
695 | run_pre_rebase_hook "$UPSTREAM_ARG" "$@" | |
d8fab023 | 696 | |
1b1dce4b JS |
697 | comment_for_reflog start |
698 | ||
1b1dce4b JS |
699 | require_clean_work_tree |
700 | ||
d911d146 | 701 | if test ! -z "$1" |
1b1dce4b | 702 | then |
d911d146 TR |
703 | output git show-ref --verify --quiet "refs/heads/$1" || |
704 | die "Invalid branchname: $1" | |
705 | output git checkout "$1" || | |
706 | die "Could not checkout $1" | |
1b1dce4b JS |
707 | fi |
708 | ||
709 | HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?" | |
5166810b MK |
710 | mkdir "$DOTEST" || die "Could not create temporary $DOTEST" |
711 | ||
1b1dce4b | 712 | : > "$DOTEST"/interactive || die "Could not mark as interactive" |
73697a0b JS |
713 | git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null || |
714 | echo "detached HEAD" > "$DOTEST"/head-name | |
1b1dce4b JS |
715 | |
716 | echo $HEAD > "$DOTEST"/head | |
a6c7a276 JH |
717 | case "$REBASE_ROOT" in |
718 | '') | |
719 | rm -f "$DOTEST"/rebase-root ;; | |
720 | *) | |
721 | : >"$DOTEST"/rebase-root ;; | |
722 | esac | |
1b1dce4b | 723 | echo $ONTO > "$DOTEST"/onto |
8e4a91bd | 724 | test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy |
1b1dce4b | 725 | test t = "$VERBOSE" && : > "$DOTEST"/verbose |
3b38ec16 | 726 | if test t = "$PRESERVE_MERGES" |
f09c9b8c | 727 | then |
d911d146 TR |
728 | if test -z "$REBASE_ROOT" |
729 | then | |
730 | mkdir "$REWRITTEN" && | |
731 | for c in $(git merge-base --all $HEAD $UPSTREAM) | |
732 | do | |
733 | echo $ONTO > "$REWRITTEN"/$c || | |
734 | die "Could not init rewritten commits" | |
735 | done | |
736 | else | |
737 | mkdir "$REWRITTEN" && | |
738 | echo $ONTO > "$REWRITTEN"/root || | |
f09c9b8c | 739 | die "Could not init rewritten commits" |
d911d146 | 740 | fi |
acc8559a SH |
741 | # No cherry-pick because our first pass is to determine |
742 | # parents to rewrite and skipping dropped commits would | |
743 | # prematurely end our probe | |
f09c9b8c | 744 | MERGES_OPTION= |
d80d6bc1 | 745 | first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)" |
f09c9b8c | 746 | else |
acc8559a | 747 | MERGES_OPTION="--no-merges --cherry-pick" |
f09c9b8c | 748 | fi |
1b1dce4b | 749 | |
c54b7817 JS |
750 | SHORTHEAD=$(git rev-parse --short $HEAD) |
751 | SHORTONTO=$(git rev-parse --short $ONTO) | |
d911d146 TR |
752 | if test -z "$REBASE_ROOT" |
753 | # this is now equivalent to ! -z "$UPSTREAM" | |
754 | then | |
755 | SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM) | |
756 | REVISIONS=$UPSTREAM...$HEAD | |
757 | SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD | |
758 | else | |
759 | REVISIONS=$ONTO...$HEAD | |
760 | SHORTREVISIONS=$SHORTHEAD | |
761 | fi | |
6047a234 | 762 | git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \ |
acc8559a | 763 | --abbrev=7 --reverse --left-right --topo-order \ |
d911d146 | 764 | $REVISIONS | \ |
acc8559a SH |
765 | sed -n "s/^>//p" | while read shortsha1 rest |
766 | do | |
767 | if test t != "$PRESERVE_MERGES" | |
768 | then | |
769 | echo "pick $shortsha1 $rest" >> "$TODO" | |
770 | else | |
771 | sha1=$(git rev-parse $shortsha1) | |
d911d146 TR |
772 | if test -z "$REBASE_ROOT" |
773 | then | |
774 | preserve=t | |
775 | for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-) | |
776 | do | |
1830d9cb | 777 | if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \) |
d911d146 TR |
778 | then |
779 | preserve=f | |
780 | fi | |
781 | done | |
782 | else | |
783 | preserve=f | |
784 | fi | |
acc8559a SH |
785 | if test f = "$preserve" |
786 | then | |
787 | touch "$REWRITTEN"/$sha1 | |
788 | echo "pick $shortsha1 $rest" >> "$TODO" | |
789 | fi | |
790 | fi | |
791 | done | |
792 | ||
793 | # Watch for commits that been dropped by --cherry-pick | |
794 | if test t = "$PRESERVE_MERGES" | |
795 | then | |
796 | mkdir "$DROPPED" | |
797 | # Save all non-cherry-picked changes | |
d911d146 | 798 | git rev-list $REVISIONS --left-right --cherry-pick | \ |
acc8559a SH |
799 | sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks |
800 | # Now all commits and note which ones are missing in | |
801 | # not-cherry-picks and hence being dropped | |
d911d146 | 802 | git rev-list $REVISIONS | |
e249044c | 803 | while read rev |
acc8559a | 804 | do |
e1622bfc | 805 | if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = "" |
acc8559a SH |
806 | then |
807 | # Use -f2 because if rev-list is telling us this commit is | |
808 | # not worthwhile, we don't want to track its multiple heads, | |
809 | # just the history of its first-parent for others that will | |
810 | # be rebasing on top of it | |
d911d146 | 811 | git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev |
e249044c | 812 | short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev) |
e1622bfc | 813 | sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO" |
acc8559a SH |
814 | rm "$REWRITTEN"/$rev |
815 | fi | |
816 | done | |
817 | fi | |
d911d146 | 818 | |
ff74126c | 819 | test -s "$TODO" || echo noop >> "$TODO" |
6047a234 JS |
820 | cat >> "$TODO" << EOF |
821 | ||
d911d146 | 822 | # Rebase $SHORTREVISIONS onto $SHORTONTO |
1b1dce4b JS |
823 | # |
824 | # Commands: | |
88b1f0b8 | 825 | # p, pick = use commit |
6741aa6c | 826 | # r, reword = use commit, but edit the commit message |
88b1f0b8 MV |
827 | # e, edit = use commit, but stop for amending |
828 | # s, squash = use commit, but meld into previous commit | |
0205e72f | 829 | # f, fixup = like "squash", but discard this commit's log message |
82576ddb JS |
830 | # |
831 | # If you remove a line here THAT COMMIT WILL BE LOST. | |
6047a234 | 832 | # However, if you remove everything, the rebase will be aborted. |
82576ddb | 833 | # |
1b1dce4b | 834 | EOF |
1b1dce4b | 835 | |
376ccb8c | 836 | has_action "$TODO" || |
c54b7817 | 837 | die_abort "Nothing to do" |
1b1dce4b JS |
838 | |
839 | cp "$TODO" "$TODO".backup | |
ef0c2abf | 840 | git_editor "$TODO" || |
1b1dce4b JS |
841 | die "Could not execute editor" |
842 | ||
376ccb8c | 843 | has_action "$TODO" || |
c54b7817 JS |
844 | die_abort "Nothing to do" |
845 | ||
0e757e30 JS |
846 | test -d "$REWRITTEN" || skip_unnecessary_picks |
847 | ||
22e40795 | 848 | git update-ref ORIG_HEAD $HEAD |
dfa49f33 | 849 | output git checkout $ONTO && do_rest |
376ccb8c | 850 | ;; |
1b1dce4b JS |
851 | esac |
852 | shift | |
853 | done |