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