Skip to content

Commit 2766ce2

Browse files
Johannes Sixtgitster
authored andcommitted
filter-branch: Use rev-list arguments to specify revision ranges.
A subset of commits in a branch used to be specified by options (-k, -r) as well as the branch tip itself (-s). It is more natural (for git users) to specify revision ranges like 'master..next' instead. This makes it so. If no range is specified it defaults to 'HEAD'. As a consequence, the new name of the filtered branch must be the first non-option argument. All remaining arguments are passed to 'git rev-list' unmodified. The tip of the branch that gets filtered is implied: It is the first commit that git rev-list would print for the specified range. Signed-off-by: Johannes Sixt <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9840906 commit 2766ce2

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

git-filter-branch.sh

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@
4242
# does this in the '.git-rewrite/' directory but you can override
4343
# that choice by this parameter.
4444
#
45-
# -r STARTREV:: The commit id to start the rewrite at
46-
# Normally, the command will rewrite the entire history. If you
47-
# pass this argument, though, this will be the first commit it
48-
# will rewrite and keep the previous commits intact.
49-
#
50-
# -k KEEPREV:: A commit id until which _not_ to rewrite history
51-
# If you pass this argument, this commit and all of its
52-
# predecessors are kept intact.
53-
#
5445
# Filters
5546
# ~~~~~~~
5647
# The filters are applied in the order as listed below. The COMMAND
@@ -164,27 +155,31 @@
164155
# and all children of the merge will become merge commits with P1,P2
165156
# as their parents instead of the merge commit.
166157
#
167-
# To restrict rewriting to only part of the history, use -r or -k or both.
158+
# To restrict rewriting to only part of the history, specify a revision
159+
# range in addition to the new branch name. The new branch name will
160+
# point to the top-most revision that a 'git rev-list' of this range
161+
# will print.
162+
#
168163
# Consider this history:
169164
#
170165
# D--E--F--G--H
171166
# / /
172167
# A--B-----C
173168
#
174-
# To rewrite only commits F,G,H, use:
169+
# To rewrite commits D,E,F,G,H, use:
175170
#
176-
# git-filter-branch -r F ...
171+
# git-filter-branch ... new-H C..H
177172
#
178173
# To rewrite commits E,F,G,H, use one of these:
179174
#
180-
# git-filter-branch -r E -k C ...
181-
# git-filter-branch -k D -k C ...
175+
# git-filter-branch ... new-H C..H --not D
176+
# git-filter-branch ... new-H D..H --not C
182177

183178
# Testsuite: TODO
184179

185180
set -e
186181

187-
USAGE="git-filter-branch [-d TEMPDIR] [-r STARTREV]... [-k KEEPREV]... [-s SRCBRANCH] [FILTERS] DESTBRANCH"
182+
USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
188183
. git-sh-setup
189184

190185
map()
@@ -232,15 +227,13 @@ get_parents () {
232227
}
233228

234229
tempdir=.git-rewrite
235-
unchanged=" "
236230
filter_env=
237231
filter_tree=
238232
filter_index=
239233
filter_parent=
240234
filter_msg=cat
241235
filter_commit='git-commit-tree "$@"'
242236
filter_tag_name=
243-
srcbranch=HEAD
244237
while case "$#" in 0) usage;; esac
245238
do
246239
case "$1" in
@@ -265,12 +258,6 @@ do
265258
-d)
266259
tempdir="$OPTARG"
267260
;;
268-
-r)
269-
unchanged="$(get_parents "$OPTARG") $unchanged"
270-
;;
271-
-k)
272-
unchanged="$(git-rev-parse "$OPTARG"^{commit}) $unchanged"
273-
;;
274261
--env-filter)
275262
filter_env="$OPTARG"
276263
;;
@@ -292,16 +279,14 @@ do
292279
--tag-name-filter)
293280
filter_tag_name="$OPTARG"
294281
;;
295-
-s)
296-
srcbranch="$OPTARG"
297-
;;
298282
*)
299283
usage
300284
;;
301285
esac
302286
done
303287

304288
dstbranch="$1"
289+
shift
305290
test -n "$dstbranch" || die "missing branch name"
306291
git-show-ref "refs/heads/$dstbranch" 2> /dev/null &&
307292
die "branch $dstbranch already exists"
@@ -327,7 +312,7 @@ ret=0
327312

328313
mkdir ../map # map old->new commit ids for rewriting parents
329314

330-
git-rev-list --reverse --topo-order $srcbranch --not $unchanged >../revs
315+
git-rev-list --reverse --topo-order --default HEAD "$@" >../revs
331316
commits=$(cat ../revs | wc -l | tr -d " ")
332317

333318
test $commits -eq 0 && die "Found nothing to rewrite"

t/t7003-filter-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test_expect_success 'test that the file was renamed' '
4747

4848
git tag oldD H3~4
4949
test_expect_success 'rewrite one branch, keeping a side branch' '
50-
git-filter-branch --tree-filter "mv b boh || :" -k D -s oldD modD
50+
git-filter-branch --tree-filter "mv b boh || :" modD D..oldD
5151
'
5252

5353
test_expect_success 'common ancestor is still common (unchanged)' '

0 commit comments

Comments
 (0)