@matheusbsbs / matheus-bagliano / commits / 1b1dce4bae
Fork 0

Teach rebase an interactive mode

Don't you just hate the fact sometimes, that git-rebase just applies the patches, without any possibility to edit them, or rearrange them? With "--interactive", git-rebase now lets you edit the list of patches, so that you can reorder, edit and delete patches. Such a list will typically look like this: pick deadbee The oneline of this commit pick fa1afe1 The oneline of the next commit ... By replacing the command "pick" with the command "edit", you can amend that patch and/or its commit message, and by replacing it with "squash" you can tell rebase to fold that patch into the patch before that. It is derived from the script sent to the list in <Pine.LNX.4.63.0702252156190.22628@wbgn013.biozentrum.uni-wuerzburg.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 25, 2007 at 01:11 UTC 1b1dce4bae760248a1fc3e29548a72c446e77270
5 files changed +546 -5
Documentation/git-rebase.txt
+82 -3
@@ -8,7 +8,8 @@ git-rebase - Forward-port local commits to the updated upstream head
8 SYNOPSIS
9 --------
10 [verse]
11 -'git-rebase' [-v] [--merge] [-C<n>] [--onto <newbase>] <upstream> [<branch>]
11 +'git-rebase' [-i | --interactive] [-v | --verbose] [--merge] [-C<n>]
12 + [--onto <newbase>] <upstream> [<branch>]
13 'git-rebase' --continue | --skip | --abort
14
15 DESCRIPTION
@@ -208,6 +209,10 @@ OPTIONS
209 context exist they all must match. By default no context is
210 ever ignored.
211
212 +-i, \--interactive::
213 + Make a list of the commits which are about to be rebased. Let the
214 + user edit that list before rebasing.
215 +
216 include::merge-strategies.txt[]
217
218 NOTES
@@ -226,9 +231,83 @@ pre-rebase hook script for an example.
231 You must be in the top directory of your project to start (or continue)
232 a rebase. Upon completion, <branch> will be the current branch.
233
229 -Author
234 +INTERACTIVE MODE
235 +----------------
236 +
237 +Rebasing interactively means that you have a chance to edit the commits
238 +which are rebased. You can reorder the commits, and you can
239 +remove them (weeding out bad or otherwise unwanted patches).
240 +
241 +The interactive mode is meant for this type of workflow:
242 +
243 +1. have a wonderful idea
244 +2. hack on the code
245 +3. prepare a series for submission
246 +4. submit
247 +
248 +where point 2. consists of several instances of
249 +
250 +a. regular use
251 + 1. finish something worthy of a commit
252 + 2. commit
253 +b. independent fixup
254 + 1. realize that something does not work
255 + 2. fix that
256 + 3. commit it
257 +
258 +Sometimes the thing fixed in b.2. cannot be amended to the not-quite
259 +perfect commit it fixes, because that commit is buried deeply in a
260 +patch series. That is exactly what interactive rebase is for: use it
261 +after plenty of "a"s and "b"s, by rearranging and editing
262 +commits, and squashing multiple commits into one.
263 +
264 +Start it with the last commit you want to retain as-is:
265 +
266 + git rebase -i <after-this-commit>
267 +
268 +An editor will be fired up with all the commits in your current branch
269 +(ignoring merge commits), which come after the given commit. You can
270 +reorder the commits in this list to your heart's content, and you can
271 +remove them. The list looks more or less like this:
272 +
273 +-------------------------------------------
274 +pick deadbee The oneline of this commit
275 +pick fa1afe1 The oneline of the next commit
276 +...
277 +-------------------------------------------
278 +
279 +The oneline descriptions are purely for your pleasure; `git-rebase` will
280 +not look at them but at the commit names ("deadbee" and "fa1afe1" in this
281 +example), so do not delete or edit the names.
282 +
283 +By replacing the command "pick" with the command "edit", you can tell
284 +`git-rebase` to stop after applying that commit, so that you can edit
285 +the files and/or the commit message, amend the commit, and continue
286 +rebasing.
287 +
288 +If you want to fold two or more commits into one, replace the command
289 +"pick" with "squash" for the second and subsequent commit. If the
290 +commits had different authors, it will attribute the squashed commit to
291 +the author of the last commit.
292 +
293 +In both cases, or when a "pick" does not succeed (because of merge
294 +errors), the loop will stop to let you fix things, and you can continue
295 +the loop with `git rebase --continue`.
296 +
297 +For example, if you want to reorder the last 5 commits, such that what
298 +was HEAD~4 becomes the new HEAD. To achieve that, you would call
299 +`git-rebase` like this:
300 +
301 +----------------------
302 +$ git rebase -i HEAD~5
303 +----------------------
304 +
305 +And move the first patch to the end of the list.
306 +
307 +Authors
308 ------
231 -Written by Junio C Hamano <junkio@cox.net>
309 +Written by Junio C Hamano <junkio@cox.net> and
310 +Johannes E. Schindelin <johannes.schindelin@gmx.de>
311
312 Documentation
313 --------------
Makefile
+1 -1
@@ -204,7 +204,7 @@ SCRIPT_SH = \
204 git-fetch.sh \
205 git-ls-remote.sh \
206 git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
207 - git-pull.sh git-rebase.sh \
207 + git-pull.sh git-rebase.sh git-rebase--interactive.sh \
208 git-repack.sh git-request-pull.sh git-reset.sh \
209 git-sh-setup.sh \
210 git-tag.sh git-verify-tag.sh \
git-rebase--interactive.sh new
+289
@@ -0,0 +1,289 @@
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 +
13 +USAGE='(--continue | --abort | --skip | [--onto <branch>] <upstream> [<branch>])'
14 +
15 +. git-sh-setup
16 +require_work_tree
17 +
18 +DOTEST="$GIT_DIR/.dotest-merge"
19 +TODO="$DOTEST"/todo
20 +DONE="$DOTEST"/done
21 +STRATEGY=
22 +VERBOSE=
23 +
24 +warn () {
25 + echo "$*" >&2
26 +}
27 +
28 +require_clean_work_tree () {
29 + # test if working tree is dirty
30 + git rev-parse --verify HEAD > /dev/null &&
31 + git update-index --refresh &&
32 + git diff-files --quiet &&
33 + git diff-index --cached --quiet HEAD ||
34 + die "Working tree is dirty"
35 +}
36 +
37 +ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
38 +
39 +comment_for_reflog () {
40 + case "$ORIG_REFLOG_ACTION" in
41 + ''|rebase*)
42 + GIT_REFLOG_ACTION="rebase -i ($1)"
43 + export GIT_REFLOG_ACTION
44 + esac
45 +}
46 +
47 +mark_action_done () {
48 + sed -e 1q < "$TODO" >> "$DONE"
49 + sed -e 1d < "$TODO" >> "$TODO".new
50 + mv -f "$TODO".new "$TODO"
51 +}
52 +
53 +make_patch () {
54 + parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
55 + git diff "$parent_sha1".."$1" > "$DOTEST"/patch
56 +}
57 +
58 +die_with_patch () {
59 + make_patch "$1"
60 + die "$2"
61 +}
62 +
63 +pick_one () {
64 + case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
65 + git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
66 + parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
67 + current_sha1=$(git rev-parse --verify HEAD)
68 + if [ $current_sha1 = $parent_sha1 ]; then
69 + git reset --hard $sha1
70 + sha1=$(git rev-parse --short $sha1)
71 + warn Fast forward to $sha1
72 + else
73 + git cherry-pick $STRATEGY "$@"
74 + fi
75 +}
76 +
77 +do_next () {
78 + read command sha1 rest < "$TODO"
79 + case "$command" in
80 + \#|'')
81 + mark_action_done
82 + continue
83 + ;;
84 + pick)
85 + comment_for_reflog pick
86 +
87 + mark_action_done
88 + pick_one $sha1 ||
89 + die_with_patch $sha1 "Could not apply $sha1... $rest"
90 + ;;
91 + edit)
92 + comment_for_reflog edit
93 +
94 + mark_action_done
95 + pick_one $sha1 ||
96 + die_with_patch $sha1 "Could not apply $sha1... $rest"
97 + make_patch $sha1
98 + warn
99 + warn "You can amend the commit now, with"
100 + warn
101 + warn " git commit --amend"
102 + warn
103 + exit 0
104 + ;;
105 + squash)
106 + comment_for_reflog squash
107 +
108 + test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
109 + die "Cannot 'squash' without a previous commit"
110 +
111 + mark_action_done
112 + failed=f
113 + pick_one -n $sha1 || failed=t
114 + MSG="$DOTEST"/message
115 + echo "# This is a combination of two commits." > "$MSG"
116 + echo "# The first commit's message is:" >> "$MSG"
117 + echo >> "$MSG"
118 + git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
119 + echo >> "$MSG"
120 + echo "# And this is the 2nd commit message:" >> "$MSG"
121 + echo >> "$MSG"
122 + git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
123 + git reset --soft HEAD^
124 + author_script=$(get_author_ident_from_commit $sha1)
125 + case $failed in
126 + f)
127 + # This is like --amend, but with a different message
128 + eval "$author_script"
129 + export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
130 + git commit -F "$MSG" -e
131 + ;;
132 + t)
133 + cp "$MSG" "$GIT_DIR"/MERGE_MSG
134 + warn
135 + warn "Could not apply $sha1... $rest"
136 + warn "After you fixed that, commit the result with"
137 + warn
138 + warn " $(echo $author_script | tr '\012' ' ') \\"
139 + warn " git commit -F \"$GIT_DIR\"/MERGE_MSG -e"
140 + die_with_patch $sha1 ""
141 + esac
142 + ;;
143 + *)
144 + warn "Unknown command: $command $sha1 $rest"
145 + die_with_patch $sha1 "Please fix this in the file $TODO."
146 + esac
147 + test -s "$TODO" && return
148 +
149 + HEAD=$(git rev-parse HEAD)
150 + HEADNAME=$(cat "$DOTEST"/head-name)
151 + rm -rf "$DOTEST" &&
152 + warn "Successfully rebased and updated $HEADNAME."
153 +
154 + exit
155 +}
156 +
157 +do_rest () {
158 + while :
159 + do
160 + do_next
161 + done
162 + test -f "$DOTEST"/verbose &&
163 + git diff --stat $(cat "$DOTEST"/head)..HEAD
164 + exit
165 +}
166 +
167 +while case $# in 0) break ;; esac
168 +do
169 + case "$1" in
170 + --continue)
171 + comment_for_reflog continue
172 +
173 + test -d "$DOTEST" || die "No interactive rebase running"
174 +
175 + require_clean_work_tree
176 + do_rest
177 + ;;
178 + --abort)
179 + comment_for_reflog abort
180 +
181 + test -d "$DOTEST" || die "No interactive rebase running"
182 +
183 + HEADNAME=$(cat "$DOTEST"/head-name)
184 + HEAD=$(cat "$DOTEST"/head)
185 + git symbolic-ref HEAD $HEADNAME &&
186 + git reset --hard $HEAD &&
187 + rm -rf "$DOTEST"
188 + exit
189 + ;;
190 + --skip)
191 + comment_for_reflog skip
192 +
193 + test -d "$DOTEST" || die "No interactive rebase running"
194 +
195 + git reset --hard && do_rest
196 + ;;
197 + -s|--strategy)
198 + shift
199 + case "$#,$1" in
200 + *,*=*)
201 + STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
202 + 1,*)
203 + usage ;;
204 + *)
205 + STRATEGY="-s $2"
206 + shift ;;
207 + esac
208 + ;;
209 + --merge)
210 + # we use merge anyway
211 + ;;
212 + -C*)
213 + die "Interactive rebase uses merge, so $1 does not make sense"
214 + ;;
215 + -v)
216 + VERBOSE=t
217 + ;;
218 + -i|--interactive)
219 + # yeah, we know
220 + ;;
221 + ''|-h)
222 + usage
223 + ;;
224 + *)
225 + test -d "$DOTEST" &&
226 + die "Interactive rebase already started"
227 +
228 + git var GIT_COMMITTER_IDENT >/dev/null ||
229 + die "You need to set your committer info first"
230 +
231 + comment_for_reflog start
232 +
233 + ONTO=
234 + case "$1" in
235 + --onto)
236 + ONTO=$(git rev-parse --verify "$2") ||
237 + die "Does not point to a valid commit: $2"
238 + shift; shift
239 + ;;
240 + esac
241 +
242 + require_clean_work_tree
243 +
244 + if [ ! -z "$2"]
245 + then
246 + git show-ref --verify --quiet "refs/heads/$2" ||
247 + die "Invalid branchname: $2"
248 + git checkout "$2" ||
249 + die "Could not checkout $2"
250 + fi
251 +
252 + HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
253 + UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
254 +
255 + test -z "$ONTO" && ONTO=$UPSTREAM
256 +
257 + mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
258 + : > "$DOTEST"/interactive || die "Could not mark as interactive"
259 + git symbolic-ref HEAD > "$DOTEST"/head-name ||
260 + die "Could not get HEAD"
261 +
262 + echo $HEAD > "$DOTEST"/head
263 + echo $UPSTREAM > "$DOTEST"/upstream
264 + echo $ONTO > "$DOTEST"/onto
265 + test t = "$VERBOSE" && : > "$DOTEST"/verbose
266 +
267 + cat > "$TODO" << EOF
268 +# Rebasing $UPSTREAM..$HEAD onto $ONTO
269 +#
270 +# Commands:
271 +# pick = use commit
272 +# edit = use commit, but stop for amending
273 +# squash = use commit, but meld into previous commit
274 +EOF
275 + git rev-list --no-merges --pretty=oneline --abbrev-commit \
276 + --abbrev=7 --reverse $UPSTREAM..$HEAD | \
277 + sed "s/^/pick /" >> "$TODO"
278 +
279 + test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
280 + die "Nothing to do"
281 +
282 + cp "$TODO" "$TODO".backup
283 + ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
284 + die "Could not execute editor"
285 +
286 + git reset --hard $ONTO && do_rest
287 + esac
288 + shift
289 +done
git-rebase.sh
+11 -1
@@ -3,7 +3,7 @@
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
6 -USAGE='[-v] [--onto <newbase>] <upstream> [<branch>]'
6 +USAGE='[--interactive | -i] [-v] [--onto <newbase>] <upstream> [<branch>]'
7 LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8 same name. When the --onto option is provided the new branch starts
9 out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
@@ -120,6 +120,16 @@ finish_rb_merge () {
120 echo "All done."
121 }
122
123 +is_interactive () {
124 + test -f "$dotest"/interactive ||
125 + while case $#,"$1" in 0,|*,-i|*,--interactive) break ;; esac
126 + do
127 + shift
128 + done && test -n "$1"
129 +}
130 +
131 +is_interactive "$@" && exec git-rebase--interactive "$@"
132 +
133 while case "$#" in 0) break ;; esac
134 do
135 case "$1" in
t/t3404-rebase-interactive.sh new
+163
@@ -0,0 +1,163 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Johannes E. Schindelin
4 +#
5 +
6 +test_description='git rebase interactive
7 +
8 +This test runs git rebase "interactively", by faking an edit, and verifies
9 +that the result still makes sense.
10 +'
11 +. ./test-lib.sh
12 +
13 +# set up two branches like this:
14 +#
15 +# A - B - C - D - E
16 +# \
17 +# F - G - H
18 +# \
19 +# I
20 +#
21 +# where B, D and G touch the same file.
22 +
23 +test_expect_success 'setup' '
24 + : > file1 &&
25 + git add file1 &&
26 + test_tick &&
27 + git commit -m A &&
28 + git tag A &&
29 + echo 1 > file1 &&
30 + test_tick &&
31 + git commit -m B file1 &&
32 + : > file2 &&
33 + git add file2 &&
34 + test_tick &&
35 + git commit -m C &&
36 + echo 2 > file1 &&
37 + test_tick &&
38 + git commit -m D file1 &&
39 + : > file3 &&
40 + git add file3 &&
41 + test_tick &&
42 + git commit -m E &&
43 + git checkout -b branch1 A &&
44 + : > file4 &&
45 + git add file4 &&
46 + test_tick &&
47 + git commit -m F &&
48 + git tag F &&
49 + echo 3 > file1 &&
50 + test_tick &&
51 + git commit -m G file1 &&
52 + : > file5 &&
53 + git add file5 &&
54 + test_tick &&
55 + git commit -m H &&
56 + git checkout -b branch2 F &&
57 + : > file6 &&
58 + git add file6 &&
59 + test_tick &&
60 + git commit -m I &&
61 + git tag I
62 +'
63 +
64 +cat > fake-editor.sh << EOF
65 +#!/bin/sh
66 +test "\$1" = .git/COMMIT_EDITMSG && exit
67 +test -z "\$FAKE_LINES" && exit
68 +grep -v "^#" < "\$1" > "\$1".tmp
69 +rm "\$1"
70 +cat "\$1".tmp
71 +action=pick
72 +for line in \$FAKE_LINES; do
73 + case \$line in
74 + squash)
75 + action="\$line";;
76 + *)
77 + echo sed -n "\${line}s/^pick/\$action/p"
78 + sed -n "\${line}p" < "\$1".tmp
79 + sed -n "\${line}s/^pick/\$action/p" < "\$1".tmp >> "\$1"
80 + action=pick;;
81 + esac
82 +done
83 +EOF
84 +
85 +chmod a+x fake-editor.sh
86 +VISUAL="$(pwd)/fake-editor.sh"
87 +export VISUAL
88 +
89 +test_expect_success 'no changes are a nop' '
90 + git rebase -i F &&
91 + test $(git rev-parse I) = $(git rev-parse HEAD)
92 +'
93 +
94 +test_expect_success 'rebase on top of a non-conflicting commit' '
95 + git checkout branch1 &&
96 + git tag original-branch1 &&
97 + git rebase -i branch2 &&
98 + test file6 = $(git diff --name-only original-branch1) &&
99 + test $(git rev-parse I) = $(git rev-parse HEAD~2)
100 +'
101 +
102 +test_expect_success 'exchange two commits' '
103 + FAKE_LINES="2 1" git rebase -i HEAD~2 &&
104 + test H = $(git cat-file commit HEAD^ | tail -n 1) &&
105 + test G = $(git cat-file commit HEAD | tail -n 1)
106 +'
107 +
108 +cat > expect << EOF
109 +diff --git a/file1 b/file1
110 +index e69de29..00750ed 100644
111 +--- a/file1
112 ++++ b/file1
113 +@@ -0,0 +1 @@
114 ++3
115 +EOF
116 +
117 +cat > expect2 << EOF
118 +<<<<<<< HEAD:file1
119 +2
120 +=======
121 +3
122 +>>>>>>> b7ca976... G:file1
123 +EOF
124 +
125 +test_expect_success 'stop on conflicting pick' '
126 + git tag new-branch1 &&
127 + ! git rebase -i master &&
128 + diff -u expect .git/.dotest-merge/patch &&
129 + diff -u expect2 file1 &&
130 + test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
131 + test 0 = $(grep -v "^#" < .git/.dotest-merge/todo | wc -l)
132 +'
133 +
134 +test_expect_success 'abort' '
135 + git rebase --abort &&
136 + test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
137 + ! test -d .git/.dotest-merge
138 +'
139 +
140 +test_expect_success 'retain authorship' '
141 + echo A > file7 &&
142 + git add file7 &&
143 + GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
144 + git tag twerp &&
145 + git rebase -i --onto master HEAD^ &&
146 + git show HEAD | grep "^Author: Twerp Snog"
147 +'
148 +
149 +test_expect_success 'squash' '
150 + git reset --hard twerp &&
151 + echo B > file7 &&
152 + GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
153 + echo "******************************" &&
154 + FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
155 + test B = $(cat file7) &&
156 + test $(git rev-parse HEAD^) = $(git rev-parse master)
157 +'
158 +
159 +test_expect_success 'retain authorship when squashing' '
160 + git show HEAD | grep "^Author: Nitfol"
161 +'
162 +
163 +test_done