Skip to contentSkip to conversation

Teach "git clone" to pack refs

PATCH In review Johan Herland 3 participants 13 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Teach "git clone" to pack refs

(This is meant for AFTER v1.5.6.)

This is a re-post of the series I posted while builtin-clone was still under development. The rationale for this series can be found in earlier threads, but it roughly boils down to the following:

1. "git clone" currently creates "loose" refs for every ref in the cloned repo. A subsequent "git gc" will pack these refs into a packed-refs file. Having "git clone" produce the "packed" refs in the first place seems more efficient.

2. For repos with few refs the performance difference between writing loose refs and packed refs is negligible. However, for repos with thousands of refs [1], the difference between writing one packed-refs file and thousands of "loose" refs files is definitely noticeable. Even more so on Windows.

3. When the user updates a ref, a "loose" ref is written, and the corresponding packed ref (if any) is left unused. By making "git clone" write packed refs, we increase the overhead of unused packed refs (proportionally to the number of refs updated by the user). However, the number of refs updated by the user is typically small. If the user updates tens - or even hundreds - of refs, I still expect this overhead to be negligible, and in any case outweighed by the added performance when cloning repos with many refs.

The series is based on current 'next'.

Johan Herland (4):
Incorporate fetched packs in future object traversal Move pack_refs() and friends into libgit Prepare testsuite for a "git clone" that packs refs Teach "git clone" to pack refs

 Makefile                     |    2 +
 builtin-clone.c              |    8 ++-
 builtin-fetch-pack.c         |    1 +
 builtin-pack-refs.c          |  121 +-----------------------------------------
 pack-refs.c                  |  117 ++++++++++++++++++++++++++++++++++++++++
 pack-refs.h                  |   18 ++++++
 t/t5515-fetch-merge-logic.sh |   19 +++++++
 7 files changed, 164 insertions(+), 122 deletions(-)
 create mode 100644 pack-refs.c
 create mode 100644 pack-refs.h

Have fun! :)

...Johan

[1]: At $dayjob I'm converting old CVS modules with up to ~10 years of
history, and some of the resulting repos have ~30000 refs (mostly build tags). When cloning these repos, the speedup of this series can be measured in seconds on Linux; minutes on Windows.

#1 lore raw

PATCH 1/4 Incorporate fetched packs in future object traversal

Immediately after fetching a pack, we should call reprepare_packed_git() to make sure the objects in the pack are reachable. Otherwise, we will fail to look up objects that are present only in the fetched pack.

---
 builtin-fetch-pack.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
builtin-fetch-pack.c +1 -0
index de1e8d1..f4dbcf0 100644
@@ -820,5 +820,6 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
 		}
 	}
 
+	reprepare_packed_git();
 	return ref_cpy;
 }
Signature · 1 line
1.5.6.rc2.128.gf64ae
#2 lore raw

PATCH 2/4 Move pack_refs() and friends into libgit

In reply to Johan Herland

This moves pack_refs() and underlying functionality into the library, to make pack-refs functionality easily available to all git programs.

Most of builtin-pack-refs.c has been moved verbatim into a new file pack-refs.c that is compiled into libgit.a. A corresponding header file, pack-refs.h, has also been added, declaring pack_refs() and the #defines associated with the flags parameter to pack_refs().

This patch introduces no other changes in functionality.

---
 Makefile            |    2 +
 builtin-pack-refs.c |  121 +--------------------------------------------------
 pack-refs.c         |  117 +++++++++++++++++++++++++++++++++++++++++++++++++
 pack-refs.h         |   18 ++++++++
 4 files changed, 138 insertions(+), 120 deletions(-)
 create mode 100644 pack-refs.c
 create mode 100644 pack-refs.h
Makefile +2 -0
index 1937507..4a78388 100644
@@ -354,6 +354,7 @@ LIB_H += log-tree.h
 LIB_H += mailmap.h
 LIB_H += object.h
 LIB_H += pack.h
+LIB_H += pack-refs.h
 LIB_H += pack-revindex.h
 LIB_H += parse-options.h
 LIB_H += patch-ids.h
@@ -429,6 +430,7 @@ LIB_OBJS += merge-file.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
+LIB_OBJS += pack-refs.o
 LIB_OBJS += pack-revindex.o
 LIB_OBJS += pack-write.o
 LIB_OBJS += pager.o
builtin-pack-refs.c +1 -120

126 lines not on this page · raw message

pack-refs.c new file +117 -0

117 lines not on this page · raw message

pack-refs.h new file +18 -0
new file mode 100644
index 0000000..518acfb
@@ -0,0 +1,18 @@
+#ifndef PACK_REFS_H
+#define PACK_REFS_H
+
+/*
+ * Flags for controlling behaviour of pack_refs()
+ * PACK_REFS_PRUNE: Prune loose refs after packing
+ * PACK_REFS_ALL:   Pack _all_ refs, not just tags and already packed refs
+ */
+#define PACK_REFS_PRUNE 0x0001
+#define PACK_REFS_ALL   0x0002
+
+/*
+ * Write a packed-refs file for the current repository.
+ * flags: Combination of the above PACK_REFS_* flags.
+ */
+int pack_refs(unsigned int flags);
+
+#endif /* PACK_REFS_H */
Signature · 1 line
1.5.6.rc2.128.gf64ae
#3 lore raw

Re: [PATCH 2/4] Move pack_refs() and friends into libgit

Quoted 8 lines · Johan Herland
---
 Makefile            |    2 +
 builtin-pack-refs.c |  121 +-----------------------------------------------
 pack-refs.c         |  117 ++++++++++++++++++++++++++++++++++++++++++++++
 pack-refs.h         |   18 ++++++++
 4 files changed, 138 insertions(+), 120 deletions(-)
 create mode 100644 pack-refs.c
 create mode 100644 pack-refs.h

This patch would have been a lot easier to read, btw, if it had been generated with '-C' (then patch to pack-refs.c is based on builtin-pack-refs instead of /dev/null).

-Peff

#4 lore raw

PATCH v2 2/4 Move pack_refs() and friends into libgit

This moves pack_refs() and underlying functionality into the library, to make pack-refs functionality easily available to all git programs.

Most of builtin-pack-refs.c has been moved verbatim into a new file pack-refs.c that is compiled into libgit.a. A corresponding header file, pack-refs.h, has also been added, declaring pack_refs() and the #defines associated with the flags parameter to pack_refs().

This patch introduces no other changes in functionality.

---

Quoted 3 lines · Jeff King

This patch would have been a lot easier to read, btw, if it had been generated with '-C' (then patch to pack-refs.c is based on builtin-pack-refs instead of /dev/null).

Sorry about that. Here you go.

...Johan

 Makefile                           |    2 +
 builtin-pack-refs.c                |  121 +-----------------------------------
 builtin-pack-refs.c => pack-refs.c |   27 +--------
 pack-refs.h                        |   18 +++++
 4 files changed, 23 insertions(+), 145 deletions(-)
 copy builtin-pack-refs.c => pack-refs.c (80%)
 create mode 100644 pack-refs.h
Makefile +2 -0
index 1937507..4a78388 100644
@@ -354,6 +354,7 @@ LIB_H += log-tree.h
 LIB_H += mailmap.h
 LIB_H += object.h
 LIB_H += pack.h
+LIB_H += pack-refs.h
 LIB_H += pack-revindex.h
 LIB_H += parse-options.h
 LIB_H += patch-ids.h
@@ -429,6 +430,7 @@ LIB_OBJS += merge-file.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
+LIB_OBJS += pack-refs.o
 LIB_OBJS += pack-revindex.o
 LIB_OBJS += pack-write.o
 LIB_OBJS += pager.o
builtin-pack-refs.c +1 -120

126 lines not on this page · raw message

pack-refs.c renamed from builtin-pack-refs.c +2 -25
similarity index 80%
copy from builtin-pack-refs.c
copy to pack-refs.c
index 1aaa76d..848d311 100644
@@ -1,9 +1,7 @@
-#include "builtin.h"
 #include "cache.h"
 #include "refs.h"
-#include "object.h"
 #include "tag.h"
-#include "parse-options.h"
+#include "pack-refs.h"
 
 struct ref_to_prune {
 	struct ref_to_prune *next;
@@ -11,9 +9,6 @@ struct ref_to_prune {
 	char name[FLEX_ARRAY];
 };
 
-#define PACK_REFS_PRUNE	0x0001
-#define PACK_REFS_ALL	0x0002
-
 struct pack_refs_cb_data {
 	unsigned int flags;
 	struct ref_to_prune *ref_to_prune;
@@ -86,7 +81,7 @@ static void prune_refs(struct ref_to_prune *r)
 
 static struct lock_file packed;
 
-static int pack_refs(unsigned int flags)
+int pack_refs(unsigned int flags)
 {
 	int fd;
 	struct pack_refs_cb_data cbdata;
@@ -120,21 +115,3 @@ static int pack_refs(unsigned int flags)
 		prune_refs(cbdata.ref_to_prune);
 	return 0;
 }
-
-static char const * const pack_refs_usage[] = {
-	"git-pack-refs [options]",
-	NULL
-};
-
-int cmd_pack_refs(int argc, const char **argv, const char *prefix)
-{
-	unsigned int flags = PACK_REFS_PRUNE;
-	struct option opts[] = {
-		OPT_BIT(0, "all",   &flags, "pack everything", PACK_REFS_ALL),
-		OPT_BIT(0, "prune", &flags, "prune loose refs (default)", PACK_REFS_PRUNE),
-		OPT_END(),
-	};
-	if (parse_options(argc, argv, opts, pack_refs_usage, 0))
-		usage_with_options(pack_refs_usage, opts);
-	return pack_refs(flags);
-}
pack-refs.h new file +18 -0
new file mode 100644
index 0000000..518acfb
@@ -0,0 +1,18 @@
+#ifndef PACK_REFS_H
+#define PACK_REFS_H
+
+/*
+ * Flags for controlling behaviour of pack_refs()
+ * PACK_REFS_PRUNE: Prune loose refs after packing
+ * PACK_REFS_ALL:   Pack _all_ refs, not just tags and already packed refs
+ */
+#define PACK_REFS_PRUNE 0x0001
+#define PACK_REFS_ALL   0x0002
+
+/*
+ * Write a packed-refs file for the current repository.
+ * flags: Combination of the above PACK_REFS_* flags.
+ */
+int pack_refs(unsigned int flags);
+
+#endif /* PACK_REFS_H */
Signature · 1 line
1.5.6.rc2.128.gf64ae
#5 lore raw

PATCH 3/4 Prepare testsuite for a "git clone" that packs refs

In reply to Johan Herland

t5515-fetch-merge-logic removes many, but not all, refs between each test. This is done by removing the corresponding refs/foo/* files in the .git/refs hierarchy. However, once "git clone" starts producing packed refs, these refs will no longer be in the .git/refs hierarchy, but rather listed in .git/packed-refs. This patch therefore teaches t5515-fetch-merge-logic to also remove the refs in question from the packed-refs file.

---
 t/t5515-fetch-merge-logic.sh |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
t/t5515-fetch-merge-logic.sh +19 -0
index 3def75e..b3e1b97 100755
@@ -145,6 +145,25 @@ do
 			rm -f .git/refs/heads/*
 			rm -f .git/refs/remotes/rem/*
 			rm -f .git/refs/tags/*
+			cat .git/packed-refs | \
+			while read sha1 refname
+			do
+				case "$sha1" in
+				^*) # remove peeled tags
+					;;
+				*)
+					case "$refname" in
+					refs/heads/*|\
+					refs/remotes/rem/*|\
+					refs/tags/*) # remove same as above
+						;;
+					*) # keep everything else
+						echo "$sha1 $refname"
+						;;
+					esac
+				esac
+			done > .git/packed-refs.new
+			mv .git/packed-refs.new .git/packed-refs
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual_f" &&
Signature · 1 line
1.5.6.rc2.128.gf64ae
#6 lore raw

Re: [PATCH 3/4] Prepare testsuite for a "git clone" that packs refs

Quoted 12 lines · Johan Herland

.git/packed-refs. This patch therefore teaches t5515-fetch-merge-logic to also remove the refs in question from the packed-refs file. [...]

t/t5515-fetch-merge-logic.sh +9 -0
+			cat .git/packed-refs | \
+			while read sha1 refname
+			do
+				case "$sha1" in
+				^*) # remove peeled tags
+					;;
+				*)
+					case "$refname" in
+					refs/heads/*|\

t/t5515-fetch-merge-logic.sh do · line 166

Quoted 10 lines
t/t5515-fetch-merge-logic.sh +10 -0
+					refs/remotes/rem/*|\
+					refs/tags/*) # remove same as above
+						;;
+					*) # keep everything else
+						echo "$sha1 $refname"
+						;;
+					esac
+				esac
+			done > .git/packed-refs.new
+			mv .git/packed-refs.new .git/packed-refs

Might it not be simpler to just convert it to use plumbing to delete the refs? Something like piping for-each-ref into update-ref -d?

-Peff

#7 lore raw

Re: [PATCH 3/4] Prepare testsuite for a "git clone" that packs refs

Quoted 19 lines · Jeff King
Quoted 17 lines · Johan Herland

.git/packed-refs. This patch therefore teaches t5515-fetch-merge-logic to also remove the refs in question from the packed-refs file. [...]

t/t5515-fetch-merge-logic.sh +14 -0
+			cat .git/packed-refs | \
+			while read sha1 refname
+			do
+				case "$sha1" in
+				^*) # remove peeled tags
+					;;
+				*)
+					case "$refname" in
+					refs/heads/*|\
+					refs/remotes/rem/*|\
+					refs/tags/*) # remove same as above
+						;;
+					*) # keep everything else
+						echo "$sha1 $refname"
Quoted 8 lines

t/t5515-fetch-merge-logic.sh do · line 166

Quoted 5 lines
t/t5515-fetch-merge-logic.sh +5 -0
+						;;
+					esac
+				esac
+			done > .git/packed-refs.new
+			mv .git/packed-refs.new .git/packed-refs

Might it not be simpler to just convert it to use plumbing to delete the refs? Something like piping for-each-ref into update-ref -d?

Or use git-for-each-ref with --shell option to generate code for deleting refs?

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#8 lore raw

PATCH v2 3/4 Prepare testsuite for a "git clone" that packs refs

t5515-fetch-merge-logic removes many, but not all, refs between each test. This is done by removing the corresponding refs/foo/* files in the .git/refs hierarchy. However, once "git clone" starts producing packed refs, these refs will no longer be in the .git/refs hierarchy, but rather listed in .git/packed-refs. This patch teaches t5515-fetch-merge-logic to remove the refs using "git update-ref -d" which properly handles packed refs.

---

Quoted 6 lines · Jakub Narebski
Quoted 2 lines · Jeff King

Might it not be simpler to just convert it to use plumbing to delete the refs? Something like piping for-each-ref into update-ref -d?

Or use git-for-each-ref with --shell option to generate code for deleting refs?

Thanks for the tip. The following seems to do the job, and looks much better.

Have fun! :)

...Johan

 t/t5515-fetch-merge-logic.sh |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
t/t5515-fetch-merge-logic.sh +6 -3
index 3def75e..9de8408 100755
@@ -142,9 +142,12 @@ do
 			set x $cmd; shift
 			git symbolic-ref HEAD refs/heads/$1 ; shift
 			rm -f .git/FETCH_HEAD
-			rm -f .git/refs/heads/*
-			rm -f .git/refs/remotes/rem/*
-			rm -f .git/refs/tags/*
+			git for-each-ref --format="%(refname)" \
+				refs/heads refs/remotes/rem refs/tags | \
+			while read refname
+			do
+				git update-ref -d "$refname"
+			done
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual_f" &&
Signature · 1 line
1.5.6.rc2.128.gf64ae
#9 lore raw

PATCH 4/4 Teach "git clone" to pack refs

In reply to Johan Herland

In repos with many refs, it is unlikely that most refs will ever change. This fact is already exploited by "git gc" by executing "git pack-refs" to consolidate all refs into a single file.

When cloning a repo with many refs, it does not make sense to create the loose refs in the first place, just to have the next "git gc" consolidate them into one file. Instead, make "git clone" create the packed refs file immediately, and forego the loose refs completely.

---
 builtin-clone.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
builtin-clone.c +6 -2
index 7190952..5c5acb4 100644
@@ -18,6 +18,7 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "pack-refs.h"
 
 /*
  * Overall FIXMEs:
@@ -321,8 +322,11 @@ static struct ref *write_remote_refs(const struct ref *refs,
 	get_fetch_map(refs, tag_refspec, &tail, 0);
 
 	for (r = local_refs; r; r = r->next)
-		update_ref(reflog,
-			   r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
+		add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
+
+	pack_refs(PACK_REFS_ALL);
+	clear_extra_refs();
+
 	return local_refs;
 }
 
Signature · 1 line
1.5.6.rc2.128.gf64ae
#10 lore raw

builtin-clone.c static struct ref *write_remote_refs(const struct ref *refs, · line 327

Quoted 1 line · Johan Herland
builtin-clone.c +1 -0
+	pack_refs(PACK_REFS_ALL);

I haven't looked carefully at the pack_refs code, but my understanding was that this would pack _all_ refs, including branches. Don't we generally try to leave branches unpacked, since they change a lot? IOW, shouldn't this just be "pack_refs(0)"?

-Peff

#11 lore raw
Quoted 7 lines · Jeff King

builtin-clone.c static struct ref *write_remote_refs(const struct ref *refs, · line 327

Quoted 1 line · Johan Herland
builtin-clone.c +1 -0
+	pack_refs(PACK_REFS_ALL);

I haven't looked carefully at the pack_refs code, but my understanding was that this would pack _all_ refs, including branches. Don't we generally try to leave branches unpacked, since they change a lot? IOW, shouldn't this just be "pack_refs(0)"?

Yes, for many repos it does not make much sense to pack branches. But in the case where the repo has many inactive branches (I have repos with 1000 branches where at most 5-10 are still active), I'd much rather pack all branches and then later "unpack" the active ones, than write all those "loose" refs as separate files onto the filesystem (e.g. in CygWin *shudder*). In any case, the user normally does not work actively on hundreds of branches, so the overhead of "unpacking" active branches should be fairly negligible in any case.

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
#12 lore raw
Quoted 8 lines · Johan Herland

Yes, for many repos it does not make much sense to pack branches. But in the case where the repo has many inactive branches (I have repos with 1000 branches where at most 5-10 are still active), I'd much rather pack all branches and then later "unpack" the active ones, than write all those "loose" refs as separate files onto the filesystem (e.g. in CygWin *shudder*). In any case, the user normally does not work actively on hundreds of branches, so the overhead of "unpacking" active branches should be fairly negligible in any case.

What I was concerned about was that pack-refs would continue to pack active branches forever, because by default it repacks branches that are already packed.

However, it seems that git-gc just passes --all anyway, so it presumably is not a problem. And at the very least, clone is then consistent with the auto-gc behavior, which makes sense.

So I withdraw my complaint.

-Peff