Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 700 lines (635 sloc) 17.049 kb
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
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
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
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
c442765 rebase --no-verify
Nanako Shiraishi authored
29 no-verify override pre-rebase hook from stopping the operation
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
30 "
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
31
32 . git-sh-setup
33 require_work_tree
34
28ed6e7 @dscho Rename ".dotest/" to ".git/rebase" and ".dotest-merge" to "rebase-merge"
dscho authored
35 DOTEST="$GIT_DIR/rebase-merge"
c22486c Rename git-rebase interactive buffer: todo => git-rebase-todo
Seth Falcon authored
36 TODO="$DOTEST"/git-rebase-todo
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
37 DONE="$DOTEST"/done
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
38 MSG="$DOTEST"/message
39 SQUASH_MSG="$DOTEST"/message-squash
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
40 REWRITTEN="$DOTEST"/rewritten
faae853 @stephenh rebase--interactive: fix parent rewriting for dropped commits
stephenh authored
41 DROPPED="$DOTEST"/dropped
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
42 PRESERVE_MERGES=
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
43 STRATEGY=
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
44 ONTO=
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
45 VERBOSE=
c442765 rebase --no-verify
Nanako Shiraishi authored
46 OK_TO_SKIP_PRE_REBASE=
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
47
804c717 @wincent revert/cherry-pick: Allow overriding the help text by the calling Porcel...
wincent authored
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
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
53 warn () {
54 echo "$*" >&2
55 }
56
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
57 output () {
58 case "$VERBOSE" in
59 '')
5166810 @kraai rebase -i: create .dotest-merge after validating options.
kraai authored
60 output=$("$@" 2>&1 )
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
61 status=$?
5166810 @kraai rebase -i: create .dotest-merge after validating options.
kraai authored
62 test $status != 0 && printf "%s\n" "$output"
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
63 return $status
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
64 ;;
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
65 *)
66 "$@"
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
67 ;;
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
68 esac
69 }
70
d70b4a8 Teach rebase -i to honor pre-rebase hook
Nanako Shiraishi authored
71 run_pre_rebase_hook () {
c442765 rebase --no-verify
Nanako Shiraishi authored
72 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
73 test -x "$GIT_DIR/hooks/pre-rebase"
d70b4a8 Teach rebase -i to honor pre-rebase hook
Nanako Shiraishi authored
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
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
82 require_clean_work_tree () {
83 # test if working tree is dirty
84 git rev-parse --verify HEAD > /dev/null &&
6848d58 @dscho Ignore dirty submodule states during rebase and stash
dscho authored
85 git update-index --ignore-submodules --refresh &&
86 git diff-files --quiet --ignore-submodules &&
87 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
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
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
98 ;;
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
99 esac
100 }
101
4e67387 @gitster Squelch bogus progress output from git-rebase--interactive
gitster authored
102 last_count=
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
103 mark_action_done () {
104 sed -e 1q < "$TODO" >> "$DONE"
105 sed -e 1d < "$TODO" >> "$TODO".new
106 mv -f "$TODO".new "$TODO"
aadbe44 @peff grep portability fix: don't use "-e" or "-q"
peff authored
107 count=$(grep -c '^[^#]' < "$DONE")
108 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
4e67387 @gitster Squelch bogus progress output from git-rebase--interactive
gitster authored
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
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
115 }
116
117 make_patch () {
4fb1a19 @dscho rebase -i -p: leave a --cc patch when a merge could not be redone
dscho authored
118 sha1_and_parents="$(git rev-list --parents -1 "$1")"
119 case "$sha1_and_parents" in
120 ?*' '?*' '?*)
121 git diff --cc $sha1_and_parents
122 ;;
123 ?*' '?*)
124 git diff-tree -p "$1^!"
125 ;;
126 *)
127 echo "Root commit"
128 ;;
129 esac > "$DOTEST"/patch
be6ff20 @dscho rebase -i: commit when continuing after "edit"
dscho authored
130 test -f "$DOTEST"/message ||
131 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
132 test -f "$DOTEST"/author-script ||
133 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
134 }
135
136 die_with_patch () {
137 make_patch "$1"
ecfe72f @dscho rebase -i: give rerere a chance
dscho authored
138 git rerere
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
139 die "$2"
140 }
141
c54b781 @dscho rebase -i: several cleanups
dscho authored
142 die_abort () {
143 rm -rf "$DOTEST"
144 die "$1"
145 }
146
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
147 has_action () {
aadbe44 @peff grep portability fix: don't use "-e" or "-q"
peff authored
148 grep '^[^#]' "$1" >/dev/null
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
149 }
150
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
151 pick_one () {
1d25c8c @dscho rebase -i: fix squashing corner case
dscho authored
152 no_ff=
153 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
154 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
155 test -d "$REWRITTEN" &&
156 pick_one_preserving_merges "$@" && return
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
157 parent_sha1=$(git rev-parse --verify $sha1^) ||
158 die "Could not get the parent of $sha1"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
159 current_sha1=$(git rev-parse --verify HEAD)
2858028 @mwolson git-rebase--interactive.sh: Quote arguments to test
mwolson authored
160 if test "$no_ff$current_sha1" = "$parent_sha1"; then
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
161 output git reset --hard $sha1
162 test "a$1" = a-n && output git reset --soft $current_sha1
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
163 sha1=$(git rev-parse --short $sha1)
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
164 output warn Fast forward to $sha1
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
165 else
2a9c53e @dotdash git-rebase--interactive.sh: Don't pass a strategy to git-cherry-pick.
dotdash authored
166 output git cherry-pick "$@"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
167 fi
168 }
169
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
170 pick_one_preserving_merges () {
71d9451 @trast rebase -i -p: handle index and workdir correctly
trast authored
171 fast_forward=t
172 case "$1" in
173 -n)
174 fast_forward=f
175 sha1=$2
176 ;;
177 *)
178 sha1=$1
179 ;;
180 esac
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
181 sha1=$(git rev-parse $sha1)
182
3b38ec1 @dscho rebase -i: exchange all "if [ .. ]" by "if test .."
dscho authored
183 if test -f "$DOTEST"/current-commit
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
184 then
4c1360f @gitster git-rebase--interactive.sh: comparision with == is bashism
gitster authored
185 if test "$fast_forward" = t
bb64507 @stephenh rebase-i-p: delay saving current-commit to REWRITTEN if squashing
stephenh authored
186 then
187 cat "$DOTEST"/current-commit | while read current_commit
188 do
189 git rev-parse HEAD > "$REWRITTEN"/$current_commit
190 done
191 rm "$DOTEST"/current-commit ||
192 die "Cannot write current commit's replacement sha1"
193 fi
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
194 fi
195
bb64507 @stephenh rebase-i-p: delay saving current-commit to REWRITTEN if squashing
stephenh authored
196 echo $sha1 >> "$DOTEST"/current-commit
a96dc01 @trast rebase -i -p: fix parent rewriting
trast authored
197
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
198 # rewrite parents; if none were rewritten, we can fast-forward.
199 new_parents=
faae853 @stephenh rebase--interactive: fix parent rewriting for dropped commits
stephenh authored
200 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
201 while [ "$pend" != "" ]
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
202 do
faae853 @stephenh rebase--interactive: fix parent rewriting for dropped commits
stephenh authored
203 p=$(expr "$pend" : ' \([^ ]*\)')
204 pend="${pend# $p}"
205
3b38ec1 @dscho rebase -i: exchange all "if [ .. ]" by "if test .."
dscho authored
206 if test -f "$REWRITTEN"/$p
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
207 then
208 new_p=$(cat "$REWRITTEN"/$p)
80fe82e @stephenh rebase-i-p: if todo was reordered use HEAD as the rewritten parent
stephenh authored
209
210 # If the todo reordered commits, and our parent is marked for
211 # rewriting, but hasn't been gotten to yet, assume the user meant to
212 # drop it on top of the current HEAD
213 if test -z "$new_p"
214 then
215 new_p=$(git rev-parse HEAD)
216 fi
217
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
218 test $p != $new_p && fast_forward=f
219 case "$new_parents" in
220 *$new_p*)
221 ;; # do nothing; that parent is already there
222 *)
223 new_parents="$new_parents $new_p"
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
224 ;;
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
225 esac
1c5fa0a @sbeyer rebase-i: keep old parents when preserving merges
sbeyer authored
226 else
faae853 @stephenh rebase--interactive: fix parent rewriting for dropped commits
stephenh authored
227 if test -f "$DROPPED"/$p
228 then
229 fast_forward=f
230 pend=" $(cat "$DROPPED"/$p)$pend"
231 else
232 new_parents="$new_parents $p"
233 fi
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
234 fi
235 done
236 case $fast_forward in
237 t)
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
238 output warn "Fast forward to $sha1"
71d9451 @trast rebase -i -p: handle index and workdir correctly
trast authored
239 output git reset --hard $sha1 ||
240 die "Cannot fast forward to $sha1"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
241 ;;
242 f)
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
243 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
a4f25e3 @stephenh rebase-i-p: fix 'no squashing merges' tripping up non-merges
stephenh authored
244
245 if [ "$1" != "-n" ]
246 then
247 # detach HEAD to current parent
248 output git checkout $first_parent 2> /dev/null ||
249 die "Cannot move HEAD to $first_parent"
250 fi
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
251
252 case "$new_parents" in
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
253 ' '*' '*)
a4f25e3 @stephenh rebase-i-p: fix 'no squashing merges' tripping up non-merges
stephenh authored
254 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
255
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
256 # redo merge
257 author_script=$(get_author_ident_from_commit $sha1)
258 eval "$author_script"
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
259 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
f91333d @dotdash git-rebase--interactive.sh: Make 3-way merge strategies work for -p.
dotdash authored
260 # No point in merging the first parent, that's HEAD
261 new_parents=${new_parents# $first_parent}
ae830ed @dscho rebase -i: avoid exporting GIT_AUTHOR_* variables
dscho authored
262 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
263 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
264 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
265 output git merge $STRATEGY -m "$msg" \
266 $new_parents
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
267 then
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
268 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
f5b49ea rebase -i -p: Fix --continue after a merge could not be redone
Johannes Sixt authored
269 die_with_patch $sha1 "Error redoing merge $sha1"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
270 fi
271 ;;
272 *)
2a9c53e @dotdash git-rebase--interactive.sh: Don't pass a strategy to git-cherry-pick.
dotdash authored
273 output git cherry-pick "$@" ||
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
274 die_with_patch $sha1 "Could not pick $sha1"
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
275 ;;
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
276 esac
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
277 ;;
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
278 esac
279 }
280
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
281 nth_string () {
282 case "$1" in
283 *1[0-9]|*[04-9]) echo "$1"th;;
284 *1) echo "$1"st;;
285 *2) echo "$1"nd;;
286 *3) echo "$1"rd;;
287 esac
288 }
289
290 make_squash_message () {
3b38ec1 @dscho rebase -i: exchange all "if [ .. ]" by "if test .."
dscho authored
291 if test -f "$SQUASH_MSG"; then
c7965af @gitster Avoid one-or-more (\+) non BRE in sed scripts.
gitster authored
292 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
b4ce54f @peff remove use of "tail -n 1" and "tail -1"
peff authored
293 < "$SQUASH_MSG" | sed -ne '$p')+1))
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
294 echo "# This is a combination of $COUNT commits."
8ad1065 @gitster git-rebase -i behaves better on commits with incomplete messages
gitster authored
295 sed -e 1d -e '2,/^./{
296 /^$/d
297 }' <"$SQUASH_MSG"
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
298 else
299 COUNT=2
300 echo "# This is a combination of two commits."
301 echo "# The first commit's message is:"
302 echo
303 git cat-file commit HEAD | sed -e '1,/^$/d'
304 fi
8ad1065 @gitster git-rebase -i behaves better on commits with incomplete messages
gitster authored
305 echo
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
306 echo "# This is the $(nth_string $COUNT) commit message:"
307 echo
308 git cat-file commit $1 | sed -e '1,/^$/d'
309 }
310
311 peek_next_command () {
312 sed -n "1s/ .*$//p" < "$TODO"
313 }
314
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
315 do_next () {
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
316 rm -f "$DOTEST"/message "$DOTEST"/author-script \
317 "$DOTEST"/amend || exit
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
318 read command sha1 rest < "$TODO"
319 case "$command" in
ff74126 @dscho rebase -i: do not fail when there is no commit to cherry-pick
dscho authored
320 '#'*|''|noop)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
321 mark_action_done
322 ;;
f8babc4 @dscho rebase -i: support single-letter abbreviations for the actions
dscho authored
323 pick|p)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
324 comment_for_reflog pick
325
326 mark_action_done
327 pick_one $sha1 ||
328 die_with_patch $sha1 "Could not apply $sha1... $rest"
329 ;;
f8babc4 @dscho rebase -i: support single-letter abbreviations for the actions
dscho authored
330 edit|e)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
331 comment_for_reflog edit
332
333 mark_action_done
334 pick_one $sha1 ||
335 die_with_patch $sha1 "Could not apply $sha1... $rest"
336 make_patch $sha1
c14c3c8 @dmpot git-rebase--interactive: auto amend only edited commit
dmpot authored
337 git rev-parse --verify HEAD > "$DOTEST"/amend
a8ccc20 rebase -i: When an 'edit' stops, mention the commit
Johannes Sixt authored
338 warn "Stopped at $sha1... $rest"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
339 warn "You can amend the commit now, with"
340 warn
341 warn " git commit --amend"
342 warn
0460fb4 Prompt to continue when editing during rebase --interactive
Jonathan del Strother authored
343 warn "Once you are satisfied with your changes, run"
344 warn
345 warn " git rebase --continue"
346 warn
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
347 exit 0
348 ;;
f8babc4 @dscho rebase -i: support single-letter abbreviations for the actions
dscho authored
349 squash|s)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
350 comment_for_reflog squash
351
12dd111 @MadCoder Avoid spurious error messages on error mistakes.
MadCoder authored
352 test -f "$DONE" && has_action "$DONE" ||
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
353 die "Cannot 'squash' without a previous commit"
354
355 mark_action_done
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
356 make_squash_message $sha1 > "$MSG"
7c41883 @szeder rebase -i: proper prepare-commit-msg hook argument when squashing
szeder authored
357 failed=f
358 author_script=$(get_author_ident_from_commit HEAD)
359 output git reset --soft HEAD^
360 pick_one -n $sha1 || failed=t
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
361 case "$(peek_next_command)" in
f8babc4 @dscho rebase -i: support single-letter abbreviations for the actions
dscho authored
362 squash|s)
91e1ee7 @dscho rebase -i: fix overzealous output redirection
dscho authored
363 USE_OUTPUT=output
7c41883 @szeder rebase -i: proper prepare-commit-msg hook argument when squashing
szeder authored
364 MSG_OPT=-F
94c88ed @dscho Fix submodule squashing into unrelated commit
dscho authored
365 EDIT_OR_FILE="$MSG"
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
366 cp "$MSG" "$SQUASH_MSG"
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
367 ;;
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
368 *)
91e1ee7 @dscho rebase -i: fix overzealous output redirection
dscho authored
369 USE_OUTPUT=
7c41883 @szeder rebase -i: proper prepare-commit-msg hook argument when squashing
szeder authored
370 MSG_OPT=
94c88ed @dscho Fix submodule squashing into unrelated commit
dscho authored
371 EDIT_OR_FILE=-e
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
372 rm -f "$SQUASH_MSG" || exit
943cea9 @szeder rebase -i: remove leftover debugging
szeder authored
373 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
7c41883 @szeder rebase -i: proper prepare-commit-msg hook argument when squashing
szeder authored
374 rm -f "$GIT_DIR"/MERGE_MSG || exit
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
375 ;;
6368f3f @dscho rebase -i: call editor just once for a multi-squash
dscho authored
376 esac
18640d9 @dscho rebase -i: handle --continue more like non-interactive rebase
dscho authored
377 echo "$author_script" > "$DOTEST"/author-script
dbedf97 @spearce Catch and handle git-commit failures in git-rebase --interactive
spearce authored
378 if test $failed = f
379 then
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
380 # This is like --amend, but with a different message
381 eval "$author_script"
ae830ed @dscho rebase -i: avoid exporting GIT_AUTHOR_* variables
dscho authored
382 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
383 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
384 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
94c88ed @dscho Fix submodule squashing into unrelated commit
dscho authored
385 $USE_OUTPUT git commit --no-verify \
386 $MSG_OPT "$EDIT_OR_FILE" || failed=t
dbedf97 @spearce Catch and handle git-commit failures in git-rebase --interactive
spearce authored
387 fi
388 if test $failed = t
389 then
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
390 cp "$MSG" "$GIT_DIR"/MERGE_MSG
391 warn
392 warn "Could not apply $sha1... $rest"
393 die_with_patch $sha1 ""
dbedf97 @spearce Catch and handle git-commit failures in git-rebase --interactive
spearce authored
394 fi
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
395 ;;
396 *)
397 warn "Unknown command: $command $sha1 $rest"
398 die_with_patch $sha1 "Please fix this in the file $TODO."
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
399 ;;
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
400 esac
401 test -s "$TODO" && return
402
68a163c @dscho rebase -i: provide reasonable reflog for the rebased branch
dscho authored
403 comment_for_reflog finish &&
404 HEADNAME=$(cat "$DOTEST"/head-name) &&
405 OLDHEAD=$(cat "$DOTEST"/head) &&
406 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
72583e6 @stephenh rebase-i-p: use HEAD for updating the ref instead of mapping OLDHEAD
stephenh authored
407 NEWHEAD=$(git rev-parse HEAD) &&
73697a0 @dscho rebase -i: work on a detached HEAD
dscho authored
408 case $HEADNAME in
409 refs/*)
410 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
411 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
412 git symbolic-ref HEAD $HEADNAME
413 ;;
414 esac && {
3df0a85 @dscho rebase -i: actually show the diffstat when being verbose
dscho authored
415 test ! -f "$DOTEST"/verbose ||
f3d5e46 @dscho rebase -i: use diff plumbing instead of porcelain
dscho authored
416 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
3df0a85 @dscho rebase -i: actually show the diffstat when being verbose
dscho authored
417 } &&
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
418 rm -rf "$DOTEST" &&
73697a0 @dscho rebase -i: work on a detached HEAD
dscho authored
419 git gc --auto &&
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
420 warn "Successfully rebased and updated $HEADNAME."
421
422 exit
423 }
424
425 do_rest () {
426 while :
427 do
428 do_next
429 done
430 }
431
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
432 # check if no other options are set
433 is_standalone () {
434 test $# -eq 2 -a "$2" = '--' &&
435 test -z "$ONTO" &&
436 test -z "$PRESERVE_MERGES" &&
437 test -z "$STRATEGY" &&
438 test -z "$VERBOSE"
439 }
440
441 get_saved_options () {
442 test -d "$REWRITTEN" && PRESERVE_MERGES=t
443 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
444 test -f "$DOTEST"/verbose && VERBOSE=t
445 }
446
822f7c7 Supplant the "while case ... break ;; esac" idiom
David Kastrup authored
447 while test $# != 0
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
448 do
449 case "$1" in
c442765 rebase --no-verify
Nanako Shiraishi authored
450 --no-verify)
451 OK_TO_SKIP_PRE_REBASE=yes
452 ;;
453 --verify)
454 ;;
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
455 --continue)
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
456 is_standalone "$@" || usage
457 get_saved_options
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
458 comment_for_reflog continue
459
460 test -d "$DOTEST" || die "No interactive rebase running"
461
ab11903 @gitster git-rebase -i: clean-up error check codepath.
gitster authored
462 # Sanity check
463 git rev-parse --verify HEAD >/dev/null ||
464 die "Cannot read HEAD"
6848d58 @dscho Ignore dirty submodule states during rebase and stash
dscho authored
465 git update-index --ignore-submodules --refresh &&
466 git diff-files --quiet --ignore-submodules ||
ab11903 @gitster git-rebase -i: clean-up error check codepath.
gitster authored
467 die "Working tree is dirty"
468
469 # do we have anything to commit?
6848d58 @dscho Ignore dirty submodule states during rebase and stash
dscho authored
470 if git diff-index --cached --quiet --ignore-submodules HEAD --
0327062 @spearce Reallow git-rebase --interactive --continue if commit is unnecessary
spearce authored
471 then
ab11903 @gitster git-rebase -i: clean-up error check codepath.
gitster authored
472 : Nothing to commit -- skip this
473 else
474 . "$DOTEST"/author-script ||
475 die "Cannot find the author identity"
8beb1f3 @dmpot git-rebase-interactive: do not squash commits on abort
dmpot authored
476 amend=
ab11903 @gitster git-rebase -i: clean-up error check codepath.
gitster authored
477 if test -f "$DOTEST"/amend
478 then
8beb1f3 @dmpot git-rebase-interactive: do not squash commits on abort
dmpot authored
479 amend=$(git rev-parse --verify HEAD)
c14c3c8 @dmpot git-rebase--interactive: auto amend only edited commit
dmpot authored
480 test "$amend" = $(cat "$DOTEST"/amend) ||
481 die "\
482 You have uncommitted changes in your working tree. Please, commit them
483 first and then run 'git rebase --continue' again."
ab11903 @gitster git-rebase -i: clean-up error check codepath.
gitster authored
484 git reset --soft HEAD^ ||
485 die "Cannot rewind the HEAD"
486 fi
487 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
8beb1f3 @dmpot git-rebase-interactive: do not squash commits on abort
dmpot authored
488 git commit --no-verify -F "$DOTEST"/message -e || {
489 test -n "$amend" && git reset --soft $amend
490 die "Could not commit staged changes."
491 }
0327062 @spearce Reallow git-rebase --interactive --continue if commit is unnecessary
spearce authored
492 fi
18640d9 @dscho rebase -i: handle --continue more like non-interactive rebase
dscho authored
493
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
494 require_clean_work_tree
495 do_rest
496 ;;
497 --abort)
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
498 is_standalone "$@" || usage
499 get_saved_options
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
500 comment_for_reflog abort
501
ecfe72f @dscho rebase -i: give rerere a chance
dscho authored
502 git rerere clear
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
503 test -d "$DOTEST" || die "No interactive rebase running"
504
505 HEADNAME=$(cat "$DOTEST"/head-name)
506 HEAD=$(cat "$DOTEST"/head)
73697a0 @dscho rebase -i: work on a detached HEAD
dscho authored
507 case $HEADNAME in
508 refs/*)
509 git symbolic-ref HEAD $HEADNAME
510 ;;
511 esac &&
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
512 output git reset --hard $HEAD &&
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
513 rm -rf "$DOTEST"
514 exit
515 ;;
516 --skip)
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
517 is_standalone "$@" || usage
518 get_saved_options
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
519 comment_for_reflog skip
520
ecfe72f @dscho rebase -i: give rerere a chance
dscho authored
521 git rerere clear
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
522 test -d "$DOTEST" || die "No interactive rebase running"
523
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
524 output git reset --hard && do_rest
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
525 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
526 -s)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
527 case "$#,$1" in
528 *,*=*)
b5e960b Avoid a few unportable, needlessly nested "...`...".
Ralf Wildenhues authored
529 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
530 1,*)
531 usage ;;
532 *)
533 STRATEGY="-s $2"
534 shift ;;
535 esac
536 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
537 -m)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
538 # we use merge anyway
539 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
540 -v)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
541 VERBOSE=t
542 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
543 -p)
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
544 PRESERVE_MERGES=t
545 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
546 -i)
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
547 # yeah, we know
548 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
549 --onto)
550 shift
551 ONTO=$(git rev-parse --verify "$1") ||
552 die "Does not point to a valid commit: $1"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
553 ;;
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
554 --)
555 shift
d70b4a8 Teach rebase -i to honor pre-rebase hook
Nanako Shiraishi authored
556 run_pre_rebase_hook ${1+"$@"}
7970aaf @sbeyer Make rebase--interactive use OPTIONS_SPEC
sbeyer authored
557 test $# -eq 1 -o $# -eq 2 || usage
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
558 test -d "$DOTEST" &&
559 die "Interactive rebase already started"
560
561 git var GIT_COMMITTER_IDENT >/dev/null ||
562 die "You need to set your committer info first"
563
564 comment_for_reflog start
565
566 require_clean_work_tree
567
69e66f5 rebase --interactive: Compute upstream SHA1 before switching branches
Johannes Sixt authored
568 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
569 test -z "$ONTO" && ONTO=$UPSTREAM
570
3b38ec1 @dscho rebase -i: exchange all "if [ .. ]" by "if test .."
dscho authored
571 if test ! -z "$2"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
572 then
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
573 output git show-ref --verify --quiet "refs/heads/$2" ||
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
574 die "Invalid branchname: $2"
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
575 output git checkout "$2" ||
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
576 die "Could not checkout $2"
577 fi
578
579 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
5166810 @kraai rebase -i: create .dotest-merge after validating options.
kraai authored
580 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
581
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
582 : > "$DOTEST"/interactive || die "Could not mark as interactive"
73697a0 @dscho rebase -i: work on a detached HEAD
dscho authored
583 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
584 echo "detached HEAD" > "$DOTEST"/head-name
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
585
586 echo $HEAD > "$DOTEST"/head
587 echo $UPSTREAM > "$DOTEST"/upstream
588 echo $ONTO > "$DOTEST"/onto
8e4a91b @dscho rebase -i: remember the settings of -v, -s and -p when interrupted
dscho authored
589 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
590 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec1 @dscho rebase -i: exchange all "if [ .. ]" by "if test .."
dscho authored
591 if test t = "$PRESERVE_MERGES"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
592 then
593 # $REWRITTEN contains files for each commit that is
594 # reachable by at least one merge base of $HEAD and
595 # $UPSTREAM. They are not necessarily rewritten, but
596 # their children might be.
597 # This ensures that commits on merged, but otherwise
598 # unrelated side branches are left alone. (Think "X"
599 # in the man page's example.)
600 mkdir "$REWRITTEN" &&
601 for c in $(git merge-base --all $HEAD $UPSTREAM)
602 do
603 echo $ONTO > "$REWRITTEN"/$c ||
604 die "Could not init rewritten commits"
605 done
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
606 # No cherry-pick because our first pass is to determine
607 # parents to rewrite and skipping dropped commits would
608 # prematurely end our probe
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
609 MERGES_OPTION=
d80d6bc @stephenh rebase-i-p: do not include non-first-parent commits touching UPSTREAM
stephenh authored
610 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
611 else
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
612 MERGES_OPTION="--no-merges --cherry-pick"
f09c9b8 @dscho Teach rebase -i about --preserve-merges
dscho authored
613 fi
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
614
c54b781 @dscho rebase -i: several cleanups
dscho authored
615 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
616 SHORTHEAD=$(git rev-parse --short $HEAD)
617 SHORTONTO=$(git rev-parse --short $ONTO)
6047a23 @dscho rebase -i: move help to end of todo file
dscho authored
618 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
619 --abbrev=7 --reverse --left-right --topo-order \
6047a23 @dscho rebase -i: move help to end of todo file
dscho authored
620 $UPSTREAM...$HEAD | \
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
621 sed -n "s/^>//p" | while read shortsha1 rest
622 do
623 if test t != "$PRESERVE_MERGES"
624 then
625 echo "pick $shortsha1 $rest" >> "$TODO"
626 else
627 sha1=$(git rev-parse $shortsha1)
628 preserve=t
629 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
630 do
d80d6bc @stephenh rebase-i-p: do not include non-first-parent commits touching UPSTREAM
stephenh authored
631 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
632 then
633 preserve=f
634 fi
635 done
636 if test f = "$preserve"
637 then
638 touch "$REWRITTEN"/$sha1
639 echo "pick $shortsha1 $rest" >> "$TODO"
640 fi
641 fi
642 done
643
644 # Watch for commits that been dropped by --cherry-pick
645 if test t = "$PRESERVE_MERGES"
646 then
647 mkdir "$DROPPED"
648 # Save all non-cherry-picked changes
649 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
650 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
651 # Now all commits and note which ones are missing in
652 # not-cherry-picks and hence being dropped
e249044 @gitster rebase-i-p: minimum fix to obvious issues
gitster authored
653 git rev-list $UPSTREAM..$HEAD |
654 while read rev
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
655 do
656 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
657 then
658 # Use -f2 because if rev-list is telling us this commit is
659 # not worthwhile, we don't want to track its multiple heads,
660 # just the history of its first-parent for others that will
661 # be rebasing on top of it
662 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
e249044 @gitster rebase-i-p: minimum fix to obvious issues
gitster authored
663 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
664 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
acc8559 @stephenh rebase-i-p: only list commits that require rewriting in todo
stephenh authored
665 rm "$REWRITTEN"/$rev
666 fi
667 done
668 fi
ff74126 @dscho rebase -i: do not fail when there is no commit to cherry-pick
dscho authored
669 test -s "$TODO" || echo noop >> "$TODO"
6047a23 @dscho rebase -i: move help to end of todo file
dscho authored
670 cat >> "$TODO" << EOF
671
672 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
673 #
674 # Commands:
88b1f0b git-rebase -i: mention the short command aliases in the todo list
Miklos Vajna authored
675 # p, pick = use commit
676 # e, edit = use commit, but stop for amending
677 # s, squash = use commit, but meld into previous commit
82576dd @dscho rebase -i: put a nice warning into the todo list
dscho authored
678 #
679 # If you remove a line here THAT COMMIT WILL BE LOST.
6047a23 @dscho rebase -i: move help to end of todo file
dscho authored
680 # However, if you remove everything, the rebase will be aborted.
82576dd @dscho rebase -i: put a nice warning into the todo list
dscho authored
681 #
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
682 EOF
683
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
684 has_action "$TODO" ||
c54b781 @dscho rebase -i: several cleanups
dscho authored
685 die_abort "Nothing to do"
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
686
687 cp "$TODO" "$TODO".backup
ef0c2ab @aroben Add GIT_EDITOR environment and core.editor configuration variables
aroben authored
688 git_editor "$TODO" ||
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
689 die "Could not execute editor"
690
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
691 has_action "$TODO" ||
c54b781 @dscho rebase -i: several cleanups
dscho authored
692 die_abort "Nothing to do"
693
22e4079 @gitster Teach "am" and "rebase" to mark the original position with ORIG_HEAD
gitster authored
694 git update-ref ORIG_HEAD $HEAD
dfa49f3 @dscho Shut "git rebase -i" up when no --verbose was given
dscho authored
695 output git checkout $ONTO && do_rest
376ccb8 @dscho rebase -i: style fixes and minor cleanups
dscho authored
696 ;;
1b1dce4 @dscho Teach rebase an interactive mode
dscho authored
697 esac
698 shift
699 done
Something went wrong with that request. Please try again.