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

[PATCHv2 4/5] refs.c: have a write_sha1_to_lock_file wrapper

From
Stefan Beller <sbeller@google.com>
Date
Jan 22, 2015, 02:32 UTC
Message-ID
<1421893929-7447-5-git-send-email-sbeller@google.com>
In-Reply-To
<1421893929-7447-1-git-send-email-sbeller@google.com>

Now we only have one place where we need to touch the internals of the lock_file struct.

No functional changes intended.
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 refs.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/refs.c b/refs.c
index 2472e61..2013d37 100644
--- a/refs.c
+++ b/refs.c
@@ -3052,6 +3052,16 @@ int is_branch(const char *refname)
 	return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
 }
 
+static int write_sha1_to_lock_file(struct ref_lock *lock,
+				   const unsigned char *sha1)
+{
+	if (fdopen_lock_file(lock->lk, "w") < 0
+	    || fprintf(lock->lk->fp, "%s\n", sha1_to_hex(sha1)) != 41)
+		return -1;
+	else
+		return 0;
+}
+
 /*
  * Write sha1 into the ref specified by the lock. Make sure that errno
  * is sane on error.
@@ -3059,7 +3069,6 @@ int is_branch(const char *refname)
 static int write_ref_sha1(struct ref_lock *lock,
 	const unsigned char *sha1, const char *logmsg)
 {
-	static char term = '\n';
 	struct object *o;
 
 	if (!lock) {
@@ -3085,8 +3094,7 @@ static int write_ref_sha1(struct ref_lock *lock,
 		errno = EINVAL;
 		return -1;
 	}
-	if (write_in_full(lock->lk->fd, sha1_to_hex(sha1), 40) != 40 ||
-	    write_in_full(lock->lk->fd, &term, 1) != 1 ||
+	if (write_sha1_to_lock_file(lock, sha1) ||
 	    close_ref(lock) < 0) {
 		int save_errno = errno;
 		error("Couldn't write %s", lock->lk->filename.buf);
@@ -4042,9 +4050,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
 			status |= error("couldn't write %s: %s", log_file,
 					strerror(errno));
 		} else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
-			(write_in_full(lock->lk->fd,
-				sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
-			 write_str_in_full(lock->lk->fd, "\n") != 1 ||
+			(write_sha1_to_lock_file(lock, cb.last_kept_sha1) ||
 			 close_ref(lock) < 0)) {
 			status |= error("couldn't write %s",
 					lock->lk->filename.buf);
-- 
2.2.1.62.g3f15098
Previous: Stefan BellerNext: Stefan Beller
Message 7 of 45 in “[PATCHv2 0/5] Fix bug in large transactions”
  1. Stefan BellerJan 22, 2015
  2. 1/5 update-ref: test handling large transactions properlyStefan Beller, Jan 22, 2015
  3. Michael HaggertyJan 22, 2015
  4. Jeff KingJan 22, 2015
  5. 2/5 t7004: rename ULIMIT test prerequisite to ULIMIT_STACK_SIZEStefan Beller, Jan 22, 2015
  6. 3/5 refs.c: remove lock_fd from struct ref_lockStefan Beller, Jan 22, 2015
  7. 4/5 refs.c: have a write_sha1_to_lock_file wrapperStefan Beller, Jan 22, 2015
  8. 5/5 refs.c: enable large transactionsStefan Beller, Jan 22, 2015
  9. Michael HaggertyJan 22, 2015
  10. Jeff KingJan 22, 2015
  11. Michael HaggertyJan 22, 2015
  12. Stefan BellerJan 22, 2015
  13. 0/5 So you dislike the sequence of system calls?Stefan Beller, Jan 22, 2015
  14. 1/5 fixup for "refs.c: enable large transactions"Stefan Beller, Jan 22, 2015
  15. 2/5 refs.c: remove unlock_ref from write_ref_sha1Stefan Beller, Jan 22, 2015
  16. 3/5 refs.c: move static functions to close and commit refsStefan Beller, Jan 22, 2015
  17. 4/5 refs.c: remove committing the ref from write_ref_sha1Stefan Beller, Jan 22, 2015
  18. 5/5 refs.c: write values to lock files early for committingStefan Beller, Jan 22, 2015
  19. Ramsay JonesJan 22, 2015
  20. Stefan BellerJan 22, 2015
  21. Ramsay JonesJan 22, 2015
  22. Ramsay JonesJan 22, 2015
  23. Stefan BellerJan 22, 2015
  24. Ramsay JonesJan 22, 2015
  25. Michael HaggertyJan 22, 2015
  26. 0/6 Fix bug in large transactionsStefan Beller, Jan 23, 2015
  27. 1/6 update-ref: test handling large transactions properlyStefan Beller, Jan 23, 2015
  28. 2/6 t7004: rename ULIMIT test prerequisite to ULIMIT_STACK_SIZEStefan Beller, Jan 23, 2015
  29. 3/6 refs.c: remove lock_fd from struct ref_lockStefan Beller, Jan 23, 2015
  30. 4/6 refs.c: move static functions to close and commit refsStefan Beller, Jan 23, 2015
  31. 5/6 refs.c: remove unlock_ref and commit_ref from write_ref_sha1Stefan Beller, Jan 23, 2015
  32. Junio C HamanoJan 23, 2015
  33. Stefan BellerJan 24, 2015
  34. Junio C HamanoJan 24, 2015
  35. Stefan BellerJan 24, 2015
  36. Junio C HamanoJan 24, 2015
  37. 6/6 refs.c: enable large transactionsStefan Beller, Jan 23, 2015
  38. Junio C HamanoJan 24, 2015
  39. Stefan BellerJan 24, 2015
  40. Junio C HamanoJan 24, 2015
  41. Stefan BellerJan 26, 2015
  42. refs.c: clean up write_ref_sha1 returnsStefan Beller, Jan 26, 2015
  43. Junio C HamanoJan 27, 2015
  44. Stefan BellerJan 28, 2015
  45. Junio C HamanoJan 27, 2015

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.