path: root/
diff options
authorJunio C Hamano <>2009-02-10 20:16:31 (GMT)
committerJunio C Hamano <>2009-02-12 02:32:16 (GMT)
commit3e6b1d0abcd58f0b2b106212ce0a0e07a6de342f (patch)
treefdb5a3792a17a0279889d9b589dab58fc351bc82 /
parent784f8affe4dfc8ceec93803d6c22b4b8467a4642 (diff)
Make repack less likely to corrupt repository
Some platforms refuse to rename a file that is open. When repacking an already packed repository without adding any new object, the resulting pack will contain the same set of objects as an existing pack, and on such platforms, a newly created packfile cannot replace the existing one. The logic detected this issue but did not try hard enough to recover from it. Especially because the files that needs renaming come in pairs, there potentially are different failure modes that one can be renamed but the others cannot. Asking manual recovery to end users were error prone. This patch tries to make it more robust by first making sure all the existing files that need to be renamed have been renamed before continuing, and attempts to roll back if some failed to rename. This is based on an initial patch by Robin Rosenberg. Signed-off-by: Junio C Hamano <>
Diffstat (limited to '')
1 files changed, 67 insertions, 20 deletions
diff --git a/ b/
index 458a497..be6db5e 100755
--- a/
+++ b/
@@ -88,32 +88,79 @@ if [ -z "$names" ]; then
echo Nothing new to pack.
-for name in $names ; do
- fullbases="$fullbases pack-$name"
- chmod a-w "$PACKTMP-$name.pack"
- chmod a-w "$PACKTMP-$name.idx"
- mkdir -p "$PACKDIR" || exit
+# Ok we have prepared all new packfiles.
+mkdir -p "$PACKDIR" || exit
+# First see if there are packs of the same name and if so
+# if we can move them out of the way (this can happen if we
+# repacked immediately after packing fully.
+for name in $names
for sfx in pack idx
- if test -f "$PACKDIR/pack-$name.$sfx"
- then
- mv -f "$PACKDIR/pack-$name.$sfx" \
- "$PACKDIR/old-pack-$name.$sfx"
- fi
- done &&
+ file=pack-$name.$sfx
+ test -f "$PACKDIR/$file" || continue
+ rm -f "$PACKDIR/old-$file" &&
+ mv "$PACKDIR/$file" "$PACKDIR/old-$file" || {
+ failed=t
+ break
+ }
+ rollback="$rollback $file"
+ done
+ test -z "$failed" || break
+# If renaming failed for any of them, roll the ones we have
+# already renamed back to their original names.
+if test -n "$failed"
+ rollback_failure=
+ for file in $rollback
+ do
+ mv "$PACKDIR/old-$file" "$PACKDIR/$file" ||
+ rollback_failure="$rollback_failure $file"
+ done
+ if test -n "$rollback_failure"
+ then
+ echo >&2 "WARNING: Some packs in use have been renamed by"
+ echo >&2 "WARNING: prefixing old- to their name, in order to"
+ echo >&2 "WARNING: replace them with the new version of the"
+ echo >&2 "WARNING: file. But the operation failed, and"
+ echo >&2 "WARNING: attempt to rename them back to their"
+ echo >&2 "WARNING: original names also failed."
+ echo >&2 "WARNING: Please rename them in $PACKDIR manually:"
+ for file in $rollback_failure
+ do
+ echo >&2 "WARNING: old-$file -> $file"
+ done
+ fi
+ exit 1
+# Now the ones with the same name are out of the way...
+for name in $names
+ fullbases="$fullbases pack-$name"
+ chmod a-w "$PACKTMP-$name.pack"
+ chmod a-w "$PACKTMP-$name.idx"
mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
- mv -f "$PACKTMP-$name.idx" "$PACKDIR/pack-$name.idx" &&
- test -f "$PACKDIR/pack-$name.pack" &&
- test -f "$PACKDIR/pack-$name.idx" || {
- echo >&2 "Couldn't replace the existing pack with updated one."
- echo >&2 "The original set of packs have been saved as"
- echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
- exit 1
- }
- rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
+ mv -f "$PACKTMP-$name.idx" "$PACKDIR/pack-$name.idx" ||
+ exit
+# Remove the "old-" files
+for name in $names
+ rm -f "$PACKDIR/old-pack-$name.idx"
+ rm -f "$PACKDIR/old-pack-$name.pack"
+# End of pack replacement.
if test "$remove_redundant" = t
# We know $existing are all redundant.