Skip to contentSkip to conversation

Add '--create-index' to git-unpack-objects

PATCH In review Johannes Schindelin 4 participants 12 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add '--create-index' to git-unpack-objects

Add the option '--create-index' to git-unpack-objects, which makes it create an index file instead of expanding its contents. While at it, document the dry-run option '-n', and optionally take a pack file instead of stdin.

---

Is anyone interested in writing the index to stdout? It is easy to add this feature to sha1create...

 Documentation/git-unpack-objects.txt |   13 +++-
 t/t5300-pack-object.sh               |    5 ++
 unpack-objects.c                     |  108 +++++++++++++++++++++++++++++++---
 3 files changed, 114 insertions(+), 12 deletions(-)

applies-to: 6dfbbc44a77ad3300caed532814e7feb02ff794b 1bff150c89d2636c54fa45bf62e7b248ba2a7b8d

Documentation/git-unpack-objects.txt +10 -3
index b716ba1..58120e4 100644
@@ -8,14 +8,15 @@ git-unpack-objects - Unpack objects from
 
 SYNOPSIS
 --------
-'git-unpack-objects' [-q] <pack-file
+'git-unpack-objects' [-q] [-n] [--create-index index-file] [pack-file]
 
 
 DESCRIPTION
 -----------
-Reads a packed archive (.pack) from the standard input, and
+Reads a packed archive (.pack) from a file or stdin, and
 expands the objects contained in the pack into "one-file
-one-object" format in $GIT_OBJECT_DIRECTORY.
+one-object" format in $GIT_OBJECT_DIRECTORY, or alternatively,
+create an index for it.
 
 OPTIONS
 -------
@@ -23,6 +24,12 @@ OPTIONS
 	The command usually shows percentage progress.  This
 	flag suppresses it.
 
+-n::
+	Perform a dry run, i.e. do not write any files.
+
+--create-index <filename>::
+	Instead of unpacking the files, create an index for this
+	pack.
 
 Author
 ------
t/t5300-pack-object.sh +5 -0
index bb62336..593bfc2 100755
@@ -79,6 +79,11 @@ test_expect_success \
      git-unpack-objects -n <test-2-${packname_2}.pack &&
      git-unpack-objects <test-2-${packname_2}.pack'
 
+test_expect_success \
+     'create-index from pack' \
+     'git-unpack-objects --create-index index <test-2-${packname_2}.pack &&
+     cmp test-2-${packname_2}.idx index'
+
 unset GIT_OBJECT_DIRECTORY
 cd $TRASH/.git2
 test_expect_success \
unpack-objects.c +99 -9

211 lines not on this page · raw message

---
0.99.8.GIT

#1 lore raw
Quoted 11 lines · On Wed, 12 Oct 2005 13:02:36 +0200 (CEST) Johannes Schindelin wrote:

Add the option '--create-index' to git-unpack-objects, which makes it create an index file instead of expanding its contents. While at it, document the dry-run option '-n', and optionally take a pack file instead of stdin.

unpack-objects.c +0 -1
@@ -104,7 +119,15 @@ static void added_object(unsigned char *
 static void write_object(void *buf, unsigned long size, const char *type)
 {
 	unsigned char sha1[20];
-	if (write_sha1_file(buf, size, type, sha1) < 0)

unpack-objects.c static void added_object(unsigned char * · line 131

Quoted 10 lines
unpack-objects.c +9 -0
+	if (create_index) {
+		char header[100];
+		SHA_CTX c;
+
+		SHA1_Init(&c);
+		SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
+		SHA1_Update(&c, buf, size);
+		SHA1_Final(current_sha1, &c);
+	} else if (write_sha1_file(buf, size, type, sha1) < 0)
 		die("failed to write object");

Sorry, but this cannot work. git-unpack-objects does a streaming unpack, and it needs to be able to read back the objects it has written out previously (in case a delta later in the stream references some older object). Saving unpacked objects in memory would obviously be unacceptable.

However, if you need to create a pack index, you obviously have a pack file with random access ability, and in this case it is possible to build the index efficiently (in two passes over the pack file) without storing unpacked objects in the filesystem. I made a separate utility to do this; will send a patch in some minutes.

#2 lore raw

PATCH 1/1 Add git-index-pack utility

git-index-pack builds a pack index file for an existing packed archive. With this utility a packed archive which was transferred without the corresponding pack index can be added to objects/pack/ without repacking.

---

 Documentation/git-index-pack.txt |   44 ++++
 Documentation/git.txt            |    3 
 Makefile                         |    2 
 index-pack.c                     |  451 ++++++++++++++++++++++++++++++++++++++
 t/t5300-pack-object.sh           |   18 ++
 5 files changed, 517 insertions(+), 1 deletions(-)
 create mode 100644 Documentation/git-index-pack.txt
 create mode 100644 index-pack.c

230c0068103bd1ed87788a6464dda2d7de58c2e2

Documentation/git-index-pack.txt new file +44 -0
new file mode 100644
@@ -0,0 +1,44 @@
+git-index-pack(1)
+=================
+
+NAME
+----
+git-index-pack - Build pack index file for an existing packed archive
+
+
+SYNOPSIS
+--------
+'git-index-pack' [-o <index-file>] <pack-file>
+
+
+DESCRIPTION
+-----------
+Reads a packed archive (.pack) from the specified file, and
+builds a pack index file (.idx) for it.  The packed archive
+together with the pack index can then be placed in the
+objects/pack/ directory of a git repository.
+
+
+OPTIONS
+-------
+-o <index-file>::
+	Write the generated pack index into the specified
+	file.  Without this option the name of pack index
+	file is constructed from the name of packed archive
+	file by replacing .pack with .idx (and the program
+	fails if the name of packed archive does not end
+	with .pack).
+
+
+Author
+------
+Written by Sergey Vlasov <[email protected]>
+
+Documentation
+-------------
+Documentation by Sergey Vlasov
+
+GIT
+---
+Part of the gitlink:git[7] suite
+
Documentation/git.txt +3 -0
@@ -68,6 +68,9 @@ gitlink:git-commit-tree[1]::
 gitlink:git-hash-object[1]::
 	Computes the object ID from a file.
 
+gitlink:git-index-pack.html[1]::
+	Build pack index file for an existing packed archive.
+
 gitlink:git-init-db[1]::
 	Creates an empty git object database
 
Makefile +1 -1
@@ -110,7 +110,7 @@ PROGRAMS = \
 	git-convert-objects$X git-diff-files$X \
 	git-diff-index$X git-diff-stages$X \
 	git-diff-tree$X git-fetch-pack$X git-fsck-objects$X \
-	git-hash-object$X git-init-db$X \
+	git-hash-object$X git-index-pack$X git-init-db$X \
 	git-local-fetch$X git-ls-files$X git-ls-tree$X git-merge-base$X \
 	git-merge-index$X git-mktag$X git-pack-objects$X git-patch-id$X \
 	git-peek-remote$X git-prune-packed$X git-read-tree$X \
index-pack.c new file +451 -0

451 lines not on this page · raw message

t/t5300-pack-object.sh +18 -0
@@ -165,4 +165,22 @@ test_expect_success \
 
      :'
 
+test_expect_success \
+    'build pack index for an existing pack' \
+    'cp test-1-${packname_1}.pack test-3.pack &&
+     git-index-pack -o tmp.idx test-3.pack &&
+     cmp tmp.idx test-1-${packname_1}.idx &&
+
+     git-index-pack test-3.pack &&
+     cmp test-3.idx test-1-${packname_1}.idx &&
+
+     cp test-2-${packname_2}.pack test-3.pack &&
+     git-index-pack -o tmp.idx test-2-${packname_2}.pack &&
+     cmp tmp.idx test-2-${packname_2}.idx &&
+
+     git-index-pack test-3.pack &&
+     cmp test-3.idx test-2-${packname_2}.idx &&
+
+     :'
+
 test_done
#3 lore raw

PATCH 1/1 Fix packname hash generation.

This changes the generation of hash packfiles have in their names, from "hash of object names as fed to us" to "hash of object names in the resulting pack, in the order they appear in the index file". The new "git-index-pack" command is taught to output the computed hash value to its standard output.

With this, we can store downloaded pack in a temporary file without knowing its final name, run git-index-pack to generate idx for it while finding out its final name, and then rename the pack and idx to their final names.

---

* Right now, the pack "hash" name only serves the collision avoidance purposes, but not true identification. The same set of objects can be fed to pack-objects in different order, produce the same pack, and still end up with different names.

This will be used in the next experiment, "git-clone not exploding the downloaded pack".

 index-pack.c   |   15 +++++++++++++--
 pack-objects.c |   14 ++++++++++----
 2 files changed, 23 insertions(+), 6 deletions(-)

applies-to: ea37b42d53264d65f746b3e42577349e8a44d5c4 3b97470e3711d7af3505baddc34428a0d9bd8214

index-pack.c +13 -2
index badbeab..785fe71 100644
@@ -349,7 +349,7 @@ static int sha1_compare(const void *_a, 
 	return memcmp(a->sha1, b->sha1, 20);
 }
 
-static void write_index_file(const char *index_name)
+static void write_index_file(const char *index_name, unsigned char *sha1)
 {
 	struct sha1file *f;
 	struct object_entry **sorted_by_sha =
@@ -358,6 +358,7 @@ static void write_index_file(const char 
 	struct object_entry **last = sorted_by_sha + nr_objects;
 	unsigned int array[256];
 	int i;
+	SHA_CTX ctx;
 
 	for (i = 0; i < nr_objects; ++i)
 		sorted_by_sha[i] = &objects[i];
@@ -385,6 +386,11 @@ static void write_index_file(const char 
 	}
 	sha1write(f, array, 256 * sizeof(int));
 
+	/* recompute the SHA1 hash of sorted object names.
+	 * currently pack-objects does not do this, but that
+	 * can be fixed.
+	 */
+	SHA1_Init(&ctx);
 	/*
 	 * Write the actual SHA1 entries..
 	 */
@@ -394,10 +400,12 @@ static void write_index_file(const char 
 		unsigned int offset = htonl(obj->offset);
 		sha1write(f, &offset, 4);
 		sha1write(f, obj->sha1, 20);
+		SHA1_Update(&ctx, obj->sha1, 20);
 	}
 	sha1write(f, pack_base + pack_size - 20, 20);
 	sha1close(f, NULL, 1);
 	free(sorted_by_sha);
+	SHA1_Final(sha1, &ctx);
 }
 
 int main(int argc, char **argv)
@@ -405,6 +413,7 @@ int main(int argc, char **argv)
 	int i;
 	char *index_name = NULL;
 	char *index_name_buf = NULL;
+	unsigned char sha1[20];
 
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
@@ -443,9 +452,11 @@ int main(int argc, char **argv)
 	deltas = xcalloc(nr_objects, sizeof(struct delta_entry));
 	parse_pack_objects();
 	free(deltas);
-	write_index_file(index_name);
+	write_index_file(index_name, sha1);
 	free(objects);
 	free(index_name_buf);
 
+	printf("%s\n", sha1_to_hex(sha1));
+
 	return 0;
 }
pack-objects.c +10 -4
index 3d62278..ef55cab 100644
@@ -393,6 +393,7 @@ int main(int argc, char **argv)
 	SHA_CTX ctx;
 	char line[PATH_MAX + 20];
 	int window = 10, depth = 10, pack_to_stdout = 0;
+	struct object_entry **list;
 	int i;
 
 	for (i = 1; i < argc; i++) {
@@ -435,7 +436,6 @@ int main(int argc, char **argv)
 	if (pack_to_stdout != !base_name)
 		usage(pack_usage);
 
-	SHA1_Init(&ctx);
 	while (fgets(line, sizeof(line), stdin) != NULL) {
 		unsigned int hash;
 		char *p;
@@ -451,10 +451,8 @@ int main(int argc, char **argv)
 				continue;
 			hash = hash * 11 + c;
 		}
-		if (add_object_entry(sha1, hash))
-			SHA1_Update(&ctx, sha1, 20);
+		add_object_entry(sha1, hash);
 	}
-	SHA1_Final(object_list_sha1, &ctx);
 	if (non_empty && !nr_objects)
 		return 0;
 	get_object_details();
@@ -462,6 +460,14 @@ int main(int argc, char **argv)
 	fprintf(stderr, "Packing %d objects\n", nr_objects);
 
 	sorted_by_sha = create_sorted_list(sha1_sort);
+	SHA1_Init(&ctx);
+	list = sorted_by_sha;
+	for (i = 0; i < nr_objects; i++) {
+		struct object_entry *entry = *list++;
+		SHA1_Update(&ctx, entry->sha1, 20);
+	}
+	SHA1_Final(object_list_sha1, &ctx);
+
 	sorted_by_type = create_sorted_list(type_size_sort);
 	if (window && depth)
 		find_deltas(sorted_by_type, window+1, depth);

---
0.99.8.GIT

#4 lore raw

Re: [PATCH] Fix packname hash generation.

Quoted 5 lines · Junio C Hamano

This changes the generation of hash packfiles have in their names, from "hash of object names as fed to us" to "hash of object names in the resulting pack, in the order they appear in the index file". The new "git-index-pack" command is taught to output the computed hash value to its standard output.

In case it was not obvious, this is not a backward incompatible change. Your existing packs will be valid after this change.

What those 40-byte hashes were buying us was that we did not have to worry about name clashes. We could have said "these two packs have the same name so they must have the same set of objects", but there is no tool that relies on this fact. We could not even say "these two packs have different names so the set of objects contained by them must be different" -- the resulting pack name depended on the order of objects fed to git-pack-objects, even if you fed the same set of objects.

The really core part never cared about how packfiles and their indices are named. The only restrictions were that they live immediately under .git/objects/pack/, have .pack and .idx suffix respectively, and their basename match with each other.

The commit walkers (anything that link with fetch.c) impose another limitation that their basenames are "pack-" followed by 40-byte hexadecimal digits. But they do not check if the name is consistent with the set of objects in the pack (checking it was computationally infeasible for huge packs in the previous hashing mechanism -- you have to feed all permutations of objects contained in the pack to SHA1 hash and see if any produces the same hash as the pack name). We _could_ now do this additional check if we wanted to (the same goes to the really core part in sha1_file.c::check_packed_git_idx()).

In short, it does not matter if your existing packs are named using the old hashing mechanism. They will continue to be valid.

But if you really care about consistency, here is an easy way to rename your existing packs to their new names the new hashing scheme would produce.

#!/bin/sh

: ${GIT_DIR=.git}
: ${GIT_OBJECT_DIRECTORY="${GIT_DIR}/objects"}

O="$GIT_OBJECT_DIRECTORY"
P="$GIT_OBJECT_DIRECTORY/pack"
for existing in `cd "$GIT_OBJECT_DIRECTORY" &&
find pack -name '*.pack' -print`
do

    idx=`expr "$existing" : '\(.*\)\.pack$'`.idx &&
    test -f "$O/$idx" || {
        echo >&2 "Missing idx $idx?"
        continue
    }
    new=`git-index-pack -o tmp-idx "$O/$existing"` || {
        echo >&2 "Corrupt pack $existing?"
        continue
    }           
    # index generated for an existing pack should match.
    cmp "$O/$idx" tmp-idx || {
        echo >&2 "Corrupt idx $idx?"
        continue
    }
    if test "pack/pack-$new.pack" = "$existing"
    then
        echo >&2 "Already converted $existing."
        continue
    fi
    if test -f "$P/pack-$new.pack" || test -f "$P/pack-$new.idx"
    then
        echo >&2 "Name clash! $new"
        continue
    fi
    mv "$O/$existing" "$P/pack-$new.pack" &&
    mv "$O/$idx" "$P/pack-$new.idx" || {
        echo >&2 "Cannot move $existing to $new"
        continue
    }
    echo >&2 "Renamed $existing -> $new"

done

#5 lore raw

PATCH 1/1 clone-pack: new option --keep to keep the pack unexploded.

In reply to Junio C Hamano

With new option --keep, or a configuration item clone.keeppack (we need a better name, or start allowing dash,"clone.keep-pack"), the packed data downloaded while cloning is saved as a pack in .git/objects/pack/ locally, with index generated for it with git-index-pack.

---

* Here is how to use it.

    $ mkdir test
    $ cd test
    $ git-init-db
    defaulting to local storage area
    $ git-clone-pack --keep ../other/repo/sito/ry/.git
    Packing 9044 objects
    $ git-checkout

After cloning, you would notice there is no individual objects; instead you will find a single packfile in .git/objects/pack/ directory.

 Documentation/git-clone-pack.txt |    7 ++++++-
 clone-pack.c                     |    3 ++-
 2 files changed, 8 insertions(+), 2 deletions(-)

applies-to: fa5213875bff7fdb8c7d05f35a047eedf3cb3af2 b64a46c81d8347e59285584e830e1ad99ad387e0

Documentation/git-clone-pack.txt +6 -1
index 87c0e46..b58165a 100644
@@ -8,7 +8,7 @@ git-clone-pack - Clones a repository by 
 
 SYNOPSIS
 --------
-'git-clone-pack' [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<head>...]
+'git-clone-pack' [-q] [--keep] [--exec=<git-upload-pack>] [<host>:]<directory> [<head>...]
 
 DESCRIPTION
 -----------
@@ -23,6 +23,11 @@ OPTIONS
 	Pass '-q' flag to 'git-unpack-objects'; this makes the
 	cloning process less verbose.
 
+--keep::
+	Do not invoke 'git-unpack-objects' on received data, but
+	create a single packfile out of it instead, and store it
+	in the object database.
+
 --exec=<git-upload-pack>::
 	Use this to specify the path to 'git-upload-pack' on the
 	remote side, if it is not found on your $PATH.
clone-pack.c +2 -1
index 9567900..2f09df0 100644
@@ -5,7 +5,8 @@
 
 static int quiet;
 static int keep_pack;
-static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
+static const char clone_pack_usage[] =
+"git-clone-pack [-q] [--keep] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
 static const char *exec = "git-upload-pack";
 
 static void clone_handshake(int fd[2], struct ref *ref)

---
0.99.8.GIT

#7 lore raw

Re: [PATCH] Add git-index-pack utility

Quoted 2 lines · Johannes Schindelin

you cheated! You use mmap(), not lseek()! Note that mmap() is more efficient only if the platform provides mmap()...

Yes, I need to steal some code for handling the deflated streams from unpack-objects.c...

#8 lore raw
In reply to Sergey Vlasov

Hi,

Quoted 13 lines · Sergey Vlasov
Quoted 11 lines · On Wed, 12 Oct 2005 13:02:36 +0200 (CEST) Johannes Schindelin wrote:

static void write_object(void *buf, unsigned long size, const char *type) {

unpack-objects.c +7 -1
 	unsigned char sha1[20];
-	if (write_sha1_file(buf, size, type, sha1) < 0)
+	if (create_index) {
+		char header[100];
+		SHA_CTX c;
+
+		SHA1_Init(&c);
+		SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
+		SHA1_Update(&c, buf, size);
Quoted 8 lines

unpack-objects.c static void added_object(unsigned char * · line 131

Quoted 3 lines
unpack-objects.c +2 -0
+		SHA1_Final(current_sha1, &c);
+	} else if (write_sha1_file(buf, size, type, sha1) < 0)
 		die("failed to write object");

Sorry, but this cannot work. git-unpack-objects does a streaming unpack, and it needs to be able to read back the objects it has written out previously (in case a delta later in the stream references some older object).

Even worse, my code did not anticipate that the base objects could have been handled earlier (and thus the deltas would never be resolved).

Quoted 1 line

Saving unpacked objects in memory would obviously be unacceptable.

Actually, this is what git-unpack-objects does. All unresolved deltas are stored in a linked list, and handled later.

Of course, it would be nicer to use a seekable file if you have one. But then, I am not at all sure that base objects should be allowed to come later in the file: since the delta chains must not be cyclic, the objects can be sorted. Thus, it could be guaranteed that the base objects are already unpacked when unpacking the derived object.

Ciao,
Dscho

#9 lore raw
Quoted 5 lines · Johannes Schindelin
Quoted 1 line · Sergey Vlasov

Saving unpacked objects in memory would obviously be unacceptable.

Actually, this is what git-unpack-objects does. All unresolved deltas are stored in a linked list, and handled later.

Yes, this may be a problem if the pack is large and contains many deltas. But these stored deltas are thrown away immediately when the base object is found; if you want to implement a streaming reindex, you will need to store them until you reach the end of pack (or write those objects to some temporary files).

Quoted 5 lines

Of course, it would be nicer to use a seekable file if you have one. But then, I am not at all sure that base objects should be allowed to come later in the file: since the delta chains must not be cyclic, the objects can be sorted. Thus, it could be guaranteed that the base objects are already unpacked when unpacking the derived object.

Hmm, pack-objects.c:write_one() does exactly the opposite - it writes the base object _after_ writing out the delta (but it does not ensure that ordering completely, so references to base objects can be pointing in both directions). Why?

#10 lore raw

Hi,

Quoted 4 lines · Sergey Vlasov

Hmm, pack-objects.c:write_one() does exactly the opposite - it writes the base object _after_ writing out the delta (but it does not ensure that ordering completely, so references to base objects can be pointing in both directions). Why?

Okay, I did not read that far. However, having quite a few packs out there in this format, there is no option to change the format now.

Ciao,
Dscho

#11 lore raw
In reply to Sergey Vlasov
Quoted 5 lines · Sergey Vlasov

Hmm, pack-objects.c:write_one() does exactly the opposite - it writes the base object _after_ writing out the delta (but it does not ensure that ordering completely, so references to base objects can be pointing in both directions). Why?

pack-objects.c is actually going to some trouble to make sure that the resulting pack is "optimal" in layout for the most recent case.

Not that I have actually verified optimality, but it was _meant_ to be that way. And my limited tests seemed to agree.

So it writes out all objects in "recency order", which is the order it gets them from git-rev-list: it's the same order as the objects are discovered when we traverse the history in time (except all commits come first, since most operations will traverse the commit history more than they will traverse the rest of the object links).

So the objects that are reachable in the most recent tree are all supposed to be at the beginning of the pack-file, just after the commits.

Now, think about what happens if such an object is a delta against something else...

In other words, if the most recent tree contains a delta against a much older object, we want not only the _delta_ to be early in the pack-file, we want the object that it is a delta _against_ to be there too (just _after_ the delta, to be exact: we obviously read the delta first, so it should come first in the pack).

The point being, that if you unpack the latest tree (ie "git checkout" or any of the normal "git diff" behaviour), the pack-file will basically be walked in a dense manner, and linearly starting roughly from the beginning. Which is the optimal IO pattern. Dense and ascending reads.

Now, if the object is reachable through some recent branch, but the delta is not, then that is not true. In that case, you want to write the recent base object early in the pack-file, but you do _not_ want to write the delta together with it, because that would be the wrong thing for the "recent head" case: it would add stuff to the beginning of the pack-file that isn't needed for recent objects.

So that's why it's an assymmetric thing. The preferred ordering of time breaks the symmetry.

Linus