@@ -125,6 +125,7 @@ filter_subdir=
125
125
orig_namespace=refs/original/
126
126
force=
127
127
prune_empty=
128
+ remap_to_ancestor=
128
129
while :
129
130
do
130
131
case " $1 " in
137
138
force=t
138
139
continue
139
140
;;
141
+ --remap-to-ancestor)
142
+ shift
143
+ remap_to_ancestor=t
144
+ continue
145
+ ;;
140
146
--prune-empty)
141
147
shift
142
148
prune_empty=t
182
188
;;
183
189
--subdirectory-filter)
184
190
filter_subdir=" $OPTARG "
191
+ remap_to_ancestor=t
185
192
;;
186
193
--original)
187
194
orig_namespace=$( expr " $OPTARG /" : ' \(.*[^/]\)/*$' ) /
@@ -257,15 +264,24 @@ git read-tree || die "Could not seed the index"
257
264
# map old->new commit ids for rewriting parents
258
265
mkdir ../map || die " Could not create map/ directory"
259
266
267
+ # we need "--" only if there are no path arguments in $@
268
+ nonrevs=$( git rev-parse --no-revs " $@ " ) || exit
269
+ test -z " $nonrevs " && dashdash=-- || dashdash=
270
+ rev_args=$( git rev-parse --revs-only " $@ " )
271
+
260
272
case " $filter_subdir " in
261
273
" " )
262
- git rev-list --reverse --topo-order --default HEAD \
263
- --parents --simplify-merges " $@ "
274
+ eval set -- " $( git rev-parse --sq --no-revs " $@ " ) "
264
275
;;
265
276
* )
266
- git rev-list --reverse --topo-order --default HEAD \
267
- --parents --simplify-merges " $@ " -- " $filter_subdir "
268
- esac > ../revs || die " Could not get the commits"
277
+ eval set -- " $( git rev-parse --sq --no-revs " $@ " $dashdash \
278
+ " $filter_subdir " ) "
279
+ ;;
280
+ esac
281
+
282
+ git rev-list --reverse --topo-order --default HEAD \
283
+ --parents --simplify-merges $rev_args " $@ " > ../revs ||
284
+ die " Could not get the commits"
269
285
commits=$( wc -l < ../revs | tr -d " " )
270
286
271
287
test $commits -eq 0 && die " Found nothing to rewrite"
@@ -345,19 +361,19 @@ while read commit parents; do
345
361
die " could not write rewritten commit"
346
362
done < ../revs
347
363
348
- # In case of a subdirectory filter, it is possible that a specified head
349
- # is not in the set of rewritten commits, because it was pruned by the
350
- # revision walker. Fix it by mapping these heads to the unique nearest
351
- # ancestor that survived the pruning.
364
+ # If we are filtering for paths, as in the case of a subdirectory
365
+ # filter, it is possible that a specified head is not in the set of
366
+ # rewritten commits, because it was pruned by the revision walker.
367
+ # Ancestor remapping fixes this by mapping these heads to the unique
368
+ # nearest ancestor that survived the pruning.
352
369
353
- if test " $filter_subdir "
370
+ if test " $remap_to_ancestor " = t
354
371
then
355
372
while read ref
356
373
do
357
374
sha1=$( git rev-parse " $ref " ^0)
358
375
test -f " $workdir " /../map/$sha1 && continue
359
- ancestor=$( git rev-list --simplify-merges -1 \
360
- $ref -- " $filter_subdir " )
376
+ ancestor=$( git rev-list --simplify-merges -1 " $ref " " $@ " )
361
377
test " $ancestor " && echo $( map $ancestor ) >> " $workdir " /../map/$sha1
362
378
done < " $tempdir " /heads
363
379
fi
0 commit comments