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