# Fix archive-destroying "git repack -a -d" bug

4 messages from 2005-09-18 to 2005-09-18. Participants: Linus Torvalds, Dave Jones, Junio C Hamano.
Thread: https://gitlist.dev/t/1842

## Linus Torvalds, 2005-09-18 01:46

Subject: Fix archive-destroying "git repack -a -d" bug
Message-ID: <Pine.LNX.4.58.0509171839590.26803@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0509171839590.26803%40g5.osdl.org

```

Using "git repack -a -d" can destroy your git archive if you use it twice 
in succession.

Why? Because the new pack will be called the same as the old pack. And we 
delete the old packs _after_ we've moved the new pack to the .git/objects/ 
directory, which means that we'll delete the new pack too!

This is a totally untested patch that may or may not fix this.

Signed-off-by: Linus "testing is for suckers" Torvalds <torvalds@osdl.org>
---
diff --git a/git-repack.sh b/git-repack.sh
--- a/git-repack.sh
+++ b/git-repack.sh
@@ -31,10 +31,6 @@ case ",$all_into_one," in
 	rev_list=
 	rev_parse='--all'
 	pack_objects=
-	# This part is a stop-gap until we have proper pack redundancy
-	# checker.
-	existing=`cd "$PACKDIR" && \
-	    find . -type f \( -name '*.pack' -o -name '*.idx' \) -print`
 	;;
 esac
 name=$(git-rev-list --objects $rev_list $(git-rev-parse $rev_parse) |
@@ -54,8 +50,13 @@ exit
 
 if test "$remove_redandant" = t
 then
+	# This part is a stop-gap until we have proper pack redundancy
+	# checker.
 	# We know $existing are all redandant only when
 	# all-into-one is used.
+	existing=`cd "$PACKDIR" && \
+	    find . -type f \( -name '*.pack' -o -name '*.idx' \) -print |
+	    grep -v $name`
 	if test "$all_into_one" != '' && test "$existing" != ''
 	then
 		( cd "$PACKDIR" && rm -f $existing )

```

## Dave Jones, 2005-09-18 03:18

Subject: Re: Fix archive-destroying "git repack -a -d" bug
Message-ID: <20050918031837.GB23405@redhat.com>
URL: https://gitlist.dev/e/20050918031837.GB23405%40redhat.com
In-Reply-To: <Pine.LNX.4.58.0509171839590.26803@g5.osdl.org>

```
On Sat, Sep 17, 2005 at 06:46:40PM -0700, Linus Torvalds wrote:
 > 
 > Using "git repack -a -d" can destroy your git archive if you use it twice 
 > in succession.
 > 
 > Why? Because the new pack will be called the same as the old pack. And we 
 > delete the old packs _after_ we've moved the new pack to the .git/objects/ 
 > directory, which means that we'll delete the new pack too!

Hmm, I'm sure I've done this several times in my x86info git repo
(http://www.codemonkey.org.uk/projects/x86info/x86info.git)

It seems to look ok to git-fsck-cache though. Would that pick up
any breakage if present ?

Running git-whatchanged, the history seems to go all the way
back to the beginning of its cvs doppelganger.

		Dave

```

## Linus Torvalds, 2005-09-18 17:00

Subject: Re: Fix archive-destroying "git repack -a -d" bug
Message-ID: <Pine.LNX.4.58.0509180958310.26803@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0509180958310.26803%40g5.osdl.org
In-Reply-To: <20050918031837.GB23405@redhat.com>

```


On Sat, 17 Sep 2005, Dave Jones wrote:
> 
> Hmm, I'm sure I've done this several times in my x86info git repo
> (http://www.codemonkey.org.uk/projects/x86info/x86info.git)
> 
> It seems to look ok to git-fsck-cache though. Would that pick up
> any breakage if present ?

It will literally remove every single object in the system, so yes, there 
would be major breakage. Even "git log" won't work.

Note: this only happens if you haven't done _anything_ in between repacks. 
If you commit anything or do anything else to cause the list of objects to 
change, "git repack" is fine.

			Linus

```

## Junio C Hamano, 2005-09-18 17:26

Subject: Re: Fix archive-destroying "git repack -a -d" bug
Message-ID: <7v4q8i5ltl.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v4q8i5ltl.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20050918031837.GB23405@redhat.com>

```
Dave Jones <davej@redhat.com> writes:

> Hmm, I'm sure I've done this several times in my x86info git repo
> (http://www.codemonkey.org.uk/projects/x86info/x86info.git)
>
> It seems to look ok to git-fsck-cache though. Would that pick up
> any breakage if present ?

Yes.  This is a real bug and I owe Linus big for spotting it
before 0.99.7.

```
