Skip to content

Commit c892ee9

Browse files
committed
subtree: persist cache between split runs
Provide a mechanism for handling problematic commits. If the algorithm in process_split_commit is getting something wrong, you can write a corrected value to the cache before running split. Signed-off-by: Tom Clarkson <[email protected]>
1 parent 87af5a3 commit c892ee9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

contrib/subtree/git-subtree.sh

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ b,branch= create a new branch from the split subtree
2727
ignore-joins ignore prior --rejoin commits
2828
onto= try connecting new tree to an existing one
2929
rejoin merge the new branch back into HEAD
30+
clear-cache reset the subtree mapping cache
3031
options for 'add', 'merge', and 'pull'
3132
squash merge subtree changes as a single commit
3233
"
@@ -48,6 +49,7 @@ annotate=
4849
squash=
4950
message=
5051
prefix=
52+
clearcache=
5153

5254
debug () {
5355
if test -n "$debug"
@@ -131,6 +133,9 @@ do
131133
--no-rejoin)
132134
rejoin=
133135
;;
136+
--clear-cache)
137+
clearcache=1
138+
;;
134139
--ignore-joins)
135140
ignore_joins=1
136141
;;
@@ -206,9 +211,13 @@ debug "opts: {$*}"
206211
debug
207212

208213
cache_setup () {
209-
cachedir="$GIT_DIR/subtree-cache/$$"
210-
rm -rf "$cachedir" ||
211-
die "Can't delete old cachedir: $cachedir"
214+
cachedir="$GIT_DIR/subtree-cache/$prefix"
215+
if test -n "$clearcache"
216+
then
217+
debug "Clearing cache"
218+
rm -rf "$cachedir" ||
219+
die "Can't delete old cachedir: $cachedir"
220+
fi
212221
mkdir -p "$cachedir" ||
213222
die "Can't create new cachedir: $cachedir"
214223
mkdir -p "$cachedir/notree" ||
@@ -266,6 +275,16 @@ cache_set () {
266275
echo "$newrev" >"$cachedir/$oldrev"
267276
}
268277

278+
cache_set_if_unset () {
279+
oldrev="$1"
280+
newrev="$2"
281+
if test -e "$cachedir/$oldrev"
282+
then
283+
return
284+
fi
285+
echo "$newrev" >"$cachedir/$oldrev"
286+
}
287+
269288
rev_exists () {
270289
if git rev-parse "$1" >/dev/null 2>&1
271290
then
@@ -375,13 +394,13 @@ find_existing_splits () {
375394
then
376395
# squash commits refer to a subtree
377396
debug " Squash: $sq from $sub"
378-
cache_set "$sq" "$sub"
397+
cache_set_if_unset "$sq" "$sub"
379398
fi
380399
if test -n "$main" -a -n "$sub"
381400
then
382401
debug " Prior: $main -> $sub"
383-
cache_set $main $sub
384-
cache_set $sub $sub
402+
cache_set_if_unset $main $sub
403+
cache_set_if_unset $sub $sub
385404
try_remove_previous "$main"
386405
try_remove_previous "$sub"
387406
fi
@@ -690,6 +709,8 @@ process_split_commit () {
690709
if test -n "$newparents"
691710
then
692711
cache_set "$rev" "$rev"
712+
else
713+
cache_set "$rev" ""
693714
fi
694715
return
695716
fi
@@ -787,7 +808,7 @@ cmd_split () {
787808
# the 'onto' history is already just the subdir, so
788809
# any parent we find there can be used verbatim
789810
debug " cache: $rev"
790-
cache_set "$rev" "$rev"
811+
cache_set_if_unset "$rev" "$rev"
791812
done
792813
fi
793814

@@ -800,7 +821,7 @@ cmd_split () {
800821
git rev-list --topo-order --skip=1 $mainline |
801822
while read rev
802823
do
803-
cache_set "$rev" ""
824+
cache_set_if_unset "$rev" ""
804825
done || exit $?
805826
fi
806827

0 commit comments

Comments
 (0)