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