git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 29/33] pack_one_ref(): use function peel_entry()

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Apr 22, 2013, 19:52 UTC
Message-ID
<1366660361-21831-30-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1366660361-21831-1-git-send-email-mhagger@alum.mit.edu>

Change pack_one_ref() to call peel_entry() rather than using its own code for peeling references. Aside from sharing code, this lets it take advantage of the optimization introduced by 6c4a060d7d.

Please note that we *could* use any peeled values that happen to already be stored in the ref_entries, which would avoid some object lookups for references that were already packed. But doing so would also propagate any peeling errors across runs of "git pack-refs" and give no way to recover from such errors. And "git pack-refs" isn't run often enough that the performance cost is a problem. So instead, add a new option to peel_entry() to force the entry to be re-peeled, and call it with that option set.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
diff --git a/refs.c b/refs.c
index f78e955..f2d83f3 100644
--- a/refs.c
+++ b/refs.c
@@ -1396,7 +1396,9 @@ static enum peel_status peel_object(const unsigned char *name, unsigned char *sh
 }
 
 /*
- * Peel the entry (if possible) and return its new peel_status.
+ * Peel the entry (if possible) and return its new peel_status.  If
+ * repeel is true, re-peel the entry even if there is an old peeled
+ * value that is already stored in it.
  *
  * It is OK to call this function with a packed reference entry that
  * might be stale and might even refer to an object that has since
@@ -1404,13 +1406,19 @@ static enum peel_status peel_object(const unsigned char *name, unsigned char *sh
  * REF_KNOWS_PEELED then leave the status unchanged and return
  * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
  */
-static enum peel_status peel_entry(struct ref_entry *entry)
+static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
 {
 	enum peel_status status;
 
-	if (entry->flag & REF_KNOWS_PEELED)
-		return is_null_sha1(entry->u.value.peeled) ?
-			PEEL_NON_TAG : PEEL_PEELED;
+	if (entry->flag & REF_KNOWS_PEELED) {
+		if (repeel) {
+			entry->flag &= ~REF_KNOWS_PEELED;
+			hashclr(entry->u.value.peeled);
+		} else {
+			return is_null_sha1(entry->u.value.peeled) ?
+				PEEL_NON_TAG : PEEL_PEELED;
+		}
+	}
 	if (entry->flag & REF_ISBROKEN)
 		return PEEL_BROKEN;
 	if (entry->flag & REF_ISSYMREF)
@@ -1429,7 +1437,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
 
 	if (current_ref && (current_ref->name == refname
 			    || !strcmp(current_ref->name, refname))) {
-		if (peel_entry(current_ref))
+		if (peel_entry(current_ref, 0))
 			return -1;
 		hashcpy(sha1, current_ref->u.value.peeled);
 		return 0;
@@ -1449,7 +1457,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
 	if (flag & REF_ISPACKED) {
 		struct ref_entry *r = get_packed_ref(refname);
 		if (r) {
-			if (peel_entry(r))
+			if (peel_entry(r, 0))
 				return -1;
 			hashcpy(sha1, r->u.value.peeled);
 			return 0;
@@ -1998,7 +2006,7 @@ struct pack_refs_cb_data {
 static int pack_one_ref(struct ref_entry *entry, void *cb_data)
 {
 	struct pack_refs_cb_data *cb = cb_data;
-	struct object *o;
+	enum peel_status peel_status;
 	int is_tag_ref;
 
 	/* Do not pack symbolic or broken refs: */
@@ -2014,13 +2022,12 @@ static int pack_one_ref(struct ref_entry *entry, void *cb_data)
 	fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(entry->u.value.sha1),
 		entry->name);
 
-	o = parse_object_or_die(entry->u.value.sha1, entry->name);
-	if (o->type == OBJ_TAG) {
-		o = deref_tag(o, entry->name, 0);
-		if (o)
-			fprintf(cb->refs_file, "^%s\n",
-				sha1_to_hex(o->sha1));
-	}
+	peel_status = peel_entry(entry, 1);
+	if (peel_status == PEEL_PEELED)
+		fprintf(cb->refs_file, "^%s\n", sha1_to_hex(entry->u.value.peeled));
+	else if (peel_status != PEEL_NON_TAG)
+		die("internal error peeling reference %s (%s)",
+		    entry->name, sha1_to_hex(entry->u.value.sha1));
 
 	/* If the ref was already packed, there is no need to prune it. */
 	if ((cb->flags & PACK_REFS_PRUNE) && !(entry->flag & REF_ISPACKED)) {
@@ -2161,7 +2168,7 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
 		return 0;
 	}
 
-	peel_status = peel_entry(entry);
+	peel_status = peel_entry(entry, 0);
 	write_packed_entry(*fd, entry->name, entry->u.value.sha1,
 			   peel_status == PEEL_PEELED ?
 			   entry->u.value.peeled : NULL);
-- 
1.8.2.1
Previous: Michael HaggertyNext: Michael Haggerty
Message 31 of 40 in “Various cleanups around reference packing and peeling”
  1. 00/33 Various cleanups around reference packing and peelingMichael Haggerty, Apr 22, 2013
  2. 01/33 refs: document flags constants REF_*Michael Haggerty, Apr 22, 2013
  3. 02/33 refs: document the fields of struct ref_valueMichael Haggerty, Apr 22, 2013
  4. 03/33 refs: document do_for_each_ref() and do_one_ref()Michael Haggerty, Apr 22, 2013
  5. 04/33 refs: document how current_ref is usedMichael Haggerty, Apr 22, 2013
  6. 05/33 refs: define constant PEELED_LINE_LENGTHMichael Haggerty, Apr 22, 2013
  7. 06/33 do_for_each_ref_in_dirs(): remove dead codeMichael Haggerty, Apr 22, 2013
  8. 07/33 get_packed_ref(): return a ref_entryMichael Haggerty, Apr 22, 2013
  9. 08/33 peel_ref(): use function get_packed_ref()Michael Haggerty, Apr 22, 2013
  10. 09/33 repack_without_ref(): use function get_packed_ref()Michael Haggerty, Apr 22, 2013
  11. 10/33 refs: extract a function ref_resolves_to_object()Michael Haggerty, Apr 22, 2013
  12. 11/33 refs: extract function peel_object()Michael Haggerty, Apr 22, 2013
  13. 12/33 peel_object(): give more specific information in return valueMichael Haggerty, Apr 22, 2013
  14. 13/33 peel_ref(): fix return value for non-peelable, not-current referenceMichael Haggerty, Apr 22, 2013
  15. 14/33 refs: extract a function peel_entry()Michael Haggerty, Apr 22, 2013
  16. 15/33 refs: change the internal reference-iteration APIMichael Haggerty, Apr 22, 2013
  17. 16/33 t3210: test for spurious error messages for dangling packed refsMichael Haggerty, Apr 22, 2013
  18. 17/33 repack_without_ref(): silence errors for dangling packed refsMichael Haggerty, Apr 22, 2013
  19. Junio C HamanoApr 22, 2013
  20. 18/33 search_ref_dir(): return an index rather than a pointerMichael Haggerty, Apr 22, 2013
  21. 19/33 refs: change how packed refs are deletedMichael Haggerty, Apr 22, 2013
  22. 20/33 t3211: demonstrate loss of peeled refs if a packed ref is deletedMichael Haggerty, Apr 22, 2013
  23. 21/33 repack_without_ref(): write peeled refs in the rewritten fileMichael Haggerty, Apr 22, 2013
  24. 22/33 refs: extract a function write_packed_entry()Michael Haggerty, Apr 22, 2013
  25. 23/33 pack-refs: rename handle_one_ref() to pack_one_ref()Michael Haggerty, Apr 22, 2013
  26. 24/33 pack-refs: merge code from pack-refs.{c,h} into refs.{c,h}Michael Haggerty, Apr 22, 2013
  27. 25/33 pack_one_ref(): rename "path" parameter to "refname"Michael Haggerty, Apr 22, 2013
  28. 26/33 refs: use same lock_file object for both ref-packing functionsMichael Haggerty, Apr 22, 2013
  29. 27/33 pack_refs(): change to use do_for_each_entry()Michael Haggerty, Apr 22, 2013
  30. 28/33 refs: inline function do_not_prune()Michael Haggerty, Apr 22, 2013
  31. 29/33 pack_one_ref(): use function peel_entry()Michael Haggerty, Apr 22, 2013
  32. 30/33 pack_one_ref(): use write_packed_entry() to do the writingMichael Haggerty, Apr 22, 2013
  33. 31/33 pack_one_ref(): do some cheap tests before a more expensive oneMichael Haggerty, Apr 22, 2013
  34. 32/33 refs: change do_for_each_*() functions to take ref_cache argumentsMichael Haggerty, Apr 22, 2013
  35. 33/33 refs: handle the main ref_cache speciallyMichael Haggerty, Apr 22, 2013
  36. Junio C HamanoApr 22, 2013
  37. fixup! t3210: test for spurious error messages for dangling packed refsMichael Haggerty, Apr 23, 2013
  38. Junio C HamanoApr 23, 2013
  39. Michael HaggertyApr 24, 2013
  40. Michael HaggertyApr 27, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.