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

[PATCH 05/14] lock_packed_refs: pass error message back through a strbuf

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Dec 3, 2014, 05:16 UTC
Message-ID
<20141203051615.GO6527@google.com>
In-Reply-To
<20141203050217.GJ6527@google.com>

This saves us from having to be careful about preserving errno and makes it more explicit in the die-on-error case that the caller is exiting without a chance to clean up.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/clone.c |  6 +++++-
 refs.c          | 17 ++++++++++-------
 refs.h          |  8 ++------
 3 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index d5e7532..b07d740 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -495,8 +495,10 @@ static struct ref *wanted_peer_refs(const struct ref *refs,
 static void write_remote_refs(const struct ref *local_refs)
 {
 	const struct ref *r;
+	struct strbuf err = STRBUF_INIT;
 
-	lock_packed_refs(LOCK_DIE_ON_ERROR);
+	if (lock_packed_refs(&err))
+		die("%s", err.buf);
 
 	for (r = local_refs; r; r = r->next) {
 		if (!r->peer_ref)
@@ -506,6 +508,8 @@ static void write_remote_refs(const struct ref *local_refs)
 
 	if (commit_packed_refs())
 		die_errno("unable to overwrite old ref-pack file");
+
+	strbuf_release(&err);
 }
 
 static void write_followtags(const struct ref *refs, const char *msg)
diff --git a/refs.c b/refs.c
index 5ff457e..917f8fc 100644
--- a/refs.c
+++ b/refs.c
@@ -2371,13 +2371,15 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
 	return 0;
 }
 
-/* This should return a meaningful errno on failure */
-int lock_packed_refs(int flags)
+int lock_packed_refs(struct strbuf *err)
 {
 	struct packed_ref_cache *packed_ref_cache;
 
-	if (hold_lock_file_for_update(&packlock, git_path("packed-refs"), flags) < 0)
+	if (hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0) < 0) {
+		unable_to_lock_message(git_path("packed-refs"), errno, err);
 		return -1;
+	}
+
 	/*
 	 * Get the current packed-refs while holding the lock.  If the
 	 * packed-refs file has been modified since we last read it,
@@ -2565,11 +2567,13 @@ static void prune_refs(struct ref_to_prune *r)
 int pack_refs(unsigned int flags)
 {
 	struct pack_refs_cb_data cbdata;
+	struct strbuf err = STRBUF_INIT;
 
 	memset(&cbdata, 0, sizeof(cbdata));
 	cbdata.flags = flags;
 
-	lock_packed_refs(LOCK_DIE_ON_ERROR);
+	if (lock_packed_refs(&err))
+		die("%s", err.buf);
 	cbdata.packed_refs = get_packed_refs(&ref_cache);
 
 	do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,
@@ -2579,6 +2583,7 @@ int pack_refs(unsigned int flags)
 		die_errno("unable to overwrite old ref-pack file");
 
 	prune_refs(cbdata.ref_to_prune);
+	strbuf_release(&err);
 	return 0;
 }
 
@@ -2657,10 +2662,8 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
 	if (i == n)
 		return 0; /* no refname exists in packed refs */
 
-	if (lock_packed_refs(0)) {
-		unable_to_lock_message(git_path("packed-refs"), errno, err);
+	if (lock_packed_refs(err))
 		return -1;
-	}
 	packed = get_packed_refs(&ref_cache);
 
 	/* Remove refnames from the cache */
diff --git a/refs.h b/refs.h
index 2bc3556..ca6a567 100644
--- a/refs.h
+++ b/refs.h
@@ -119,12 +119,8 @@ extern int for_each_rawref(each_ref_fn, void *);
 extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
 extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames);
 
-/*
- * Lock the packed-refs file for writing.  Flags is passed to
- * hold_lock_file_for_update().  Return 0 on success.
- * Errno is set to something meaningful on error.
- */
-extern int lock_packed_refs(int flags);
+/* Lock the packed-refs file for writing.  Returns 0 on success. */
+extern int lock_packed_refs(struct strbuf *err);
 
 /*
  * Add a reference to the in-memory packed reference cache.  This may
-- 
2.2.0.rc0.207.ga3a616c
Previous: Jonathan NiederNext: Jonathan Nieder
Message 53 of 83 in “copy.c: make copy_fd preserve meaningful errno”
  1. copy.c: make copy_fd preserve meaningful errnoStefan Beller, Nov 17, 2014
  2. Junio C HamanoNov 17, 2014
  3. Stefan BellerNov 17, 2014
  4. Jonathan NiederNov 17, 2014
  5. Stefan BellerNov 18, 2014
  6. Jonathan NiederNov 18, 2014
  7. Stefan BellerNov 18, 2014
  8. 0/14 Re: copy.c: make copy_fd preserve meaningful errnoJonathan Nieder, Dec 3, 2014
  9. 01/14 strbuf: introduce strbuf_prefixf()Jonathan Nieder, Dec 3, 2014
  10. Eric SunshineDec 3, 2014
  11. Jonathan NiederDec 3, 2014
  12. Junio C HamanoDec 3, 2014
  13. Jonathan NiederDec 3, 2014
  14. Jonathan NiederDec 3, 2014
  15. Junio C HamanoDec 3, 2014
  16. 02/14 add_to_alternates_file: respect GIT_OBJECT_DIRECTORYJonathan Nieder, Dec 3, 2014
  17. Junio C HamanoDec 3, 2014
  18. 03/14 copy_fd: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  19. Junio C HamanoDec 3, 2014
  20. Jonathan NiederDec 3, 2014
  21. Jeff KingDec 3, 2014
  22. Jonathan NiederDec 3, 2014
  23. Jeff KingDec 4, 2014
  24. Stefan BellerDec 4, 2014
  25. Jeff KingDec 4, 2014
  26. Michael HaggertyDec 10, 2014
  27. Junio C HamanoDec 10, 2014
  28. Jeff KingDec 10, 2014
  29. doc: document error handling functions and conventions (Re: [PATCH 03/14] copy_fd: pass error message back through a strbuf)Jonathan Nieder, Dec 4, 2014
  30. Junio C HamanoDec 4, 2014
  31. Jonathan NiederDec 4, 2014
  32. Jonathan NiederDec 4, 2014
  33. Jeff KingDec 4, 2014
  34. Junio C HamanoDec 4, 2014
  35. Jeff KingDec 5, 2014
  36. Junio C HamanoDec 5, 2014
  37. Jeff KingDec 7, 2014
  38. Junio C HamanoDec 9, 2014
  39. Jeff KingDec 9, 2014
  40. Junio C HamanoFeb 12, 2015
  41. Michael HaggertyFeb 17, 2015
  42. Junio C HamanoFeb 17, 2015
  43. Jeff KingFeb 17, 2015
  44. Junio C HamanoFeb 17, 2015
  45. Junio C HamanoDec 3, 2014
  46. Jonathan NiederDec 3, 2014
  47. Junio C HamanoDec 3, 2014
  48. Jonathan NiederDec 3, 2014
  49. Stefan BellerDec 3, 2014
  50. 04/14 hold_lock_file_for_append: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  51. Torsten BögershausenDec 3, 2014
  52. Jonathan NiederDec 3, 2014
  53. 05/14 lock_packed_refs: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  54. 06/14 lockfile: introduce flag for locks outside .gitJonathan Nieder, Dec 3, 2014
  55. Junio C HamanoDec 3, 2014
  56. Jonathan NiederDec 3, 2014
  57. Junio C HamanoDec 3, 2014
  58. Jonathan NiederDec 3, 2014
  59. Junio C HamanoDec 3, 2014
  60. Jonathan NiederDec 3, 2014
  61. Junio C HamanoDec 3, 2014
  62. Jeff KingDec 3, 2014
  63. Junio C HamanoDec 4, 2014
  64. Jonathan NiederDec 4, 2014
  65. 07/14 fast-import: use message from lockfile API when writing marks failsJonathan Nieder, Dec 3, 2014
  66. 08/14 credentials: use message from lockfile API when locking ~/.git-credentials failsJonathan Nieder, Dec 3, 2014
  67. 09/14 config: use message from lockfile API when locking config file failsJonathan Nieder, Dec 3, 2014
  68. Junio C HamanoDec 3, 2014
  69. Jonathan NiederDec 3, 2014
  70. 10/14 rerere: error out on autoupdate failureJonathan Nieder, Dec 3, 2014
  71. 11/14 hold_locked_index: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  72. 12/14 hold_lock_file_for_update: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  73. Jonathan NiederDec 3, 2014
  74. 13/14 lockfile: remove unused function 'unable_to_lock_die'Jonathan Nieder, Dec 3, 2014
  75. 14/14 lockfile: make 'unable_to_lock_message' privateJonathan Nieder, Dec 3, 2014
  76. Stefan BellerDec 3, 2014
  77. Junio C HamanoNov 18, 2014
  78. Junio C HamanoNov 18, 2014
  79. Michael HaggertyNov 21, 2014
  80. Michael HaggertyNov 21, 2014
  81. Junio C HamanoNov 21, 2014
  82. Jeff KingNov 21, 2014
  83. Junio C HamanoNov 21, 2014

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.