@matheusbsbs / matheus-bagliano / commits / 2ad47d61b9
Fork 0

git-repack: Be careful when updating the same pack as an existing one.

After a clone, packfiles are read-only by default and "mv" to replace the pack with a new one goes interactive, asking if the user wants to replace it. If one is successfully moved and the other is not, the pack and its idx would become out-of-sync and corrupts the repository. Recovering is straightforward -- it is just the matter of finding the remaining .tmp-pack-* and make sure they are both moved -- but we should be extra careful not to do something so alarming to the users. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 25, 2006 at 05:28 UTC 2ad47d61b93e4027bd20f71963dcf1429dccc1fa
1 file changed +18 -3
git-repack.sh
+18 -3
@@ -54,9 +54,24 @@ else
54 fi
55 mkdir -p "$PACKDIR" || exit
56
57 - mv .tmp-pack-$name.pack "$PACKDIR/pack-$name.pack" &&
58 - mv .tmp-pack-$name.idx "$PACKDIR/pack-$name.idx" ||
59 - exit
57 + for sfx in pack idx
58 + do
59 + if test -f "$PACKDIR/pack-$name.$sfx"
60 + then
61 + mv -f "$PACKDIR/pack-$name.$sfx" \
62 + "$PACKDIR/old-pack-$name.$sfx"
63 + fi
64 + done &&
65 + mv -f .tmp-pack-$name.pack "$PACKDIR/pack-$name.pack" &&
66 + mv -f .tmp-pack-$name.idx "$PACKDIR/pack-$name.idx" &&
67 + test -f "$PACKDIR/pack-$name.pack" &&
68 + test -f "$PACKDIR/pack-$name.idx" || {
69 + echo >&2 "Couldn't replace the existing pack with updated one."
70 + echo >&2 "The original set of packs have been saved as"
71 + echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
72 + exit 1
73 + }
74 + rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
75 fi
76
77 if test "$remove_redundant" = t