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

[PATCH 1/8] refs: convert some internal functions to use object_id

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jun 9, 2015, 16:28 UTC
Message-ID
<1433867316-663554-2-git-send-email-sandals@crustytoothpaste.net>
In-Reply-To
<1433867316-663554-1-git-send-email-sandals@crustytoothpaste.net>

Convert several internal functions in refs.c to use struct object_id, and use the GIT_SHA1_HEXSZ constants in parse_ref_line.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 refs.c | 104 ++++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 52 insertions(+), 52 deletions(-)
diff --git a/refs.c b/refs.c
index a742d79..b391a0f 100644
--- a/refs.c
+++ b/refs.c
@@ -1158,7 +1158,7 @@ static const char PACKED_REFS_HEADER[] =
  * Return a pointer to the refname within the line (null-terminated),
  * or NULL if there was a problem.
  */
-static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
+static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
 {
 	const char *ref;
 
@@ -1170,15 +1170,15 @@ static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
 	 *  +1 (space in between hex and name)
 	 *  +1 (newline at the end of the line)
 	 */
-	if (line->len <= 42)
+	if (line->len <= GIT_SHA1_HEXSZ + 2)
 		return NULL;
 
-	if (get_sha1_hex(line->buf, sha1) < 0)
+	if (get_oid_hex(line->buf, oid) < 0)
 		return NULL;
-	if (!isspace(line->buf[40]))
+	if (!isspace(line->buf[GIT_SHA1_HEXSZ]))
 		return NULL;
 
-	ref = line->buf + 41;
+	ref = line->buf + GIT_SHA1_HEXSZ + 1;
 	if (isspace(*ref))
 		return NULL;
 
@@ -1223,7 +1223,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 	enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 
 	while (strbuf_getwholeline(&line, f, '\n') != EOF) {
-		unsigned char sha1[20];
+		struct object_id oid;
 		const char *refname;
 		const char *traits;
 
@@ -1236,17 +1236,17 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 			continue;
 		}
 
-		refname = parse_ref_line(&line, sha1);
+		refname = parse_ref_line(&line, &oid);
 		if (refname) {
 			int flag = REF_ISPACKED;
 
 			if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 				if (!refname_is_safe(refname))
 					die("packed refname is dangerous: %s", refname);
-				hashclr(sha1);
+				oidclr(&oid);
 				flag |= REF_BAD_NAME | REF_ISBROKEN;
 			}
-			last = create_ref_entry(refname, sha1, flag, 0);
+			last = create_ref_entry(refname, oid.hash, flag, 0);
 			if (peeled == PEELED_FULLY ||
 			    (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 				last->flag |= REF_KNOWS_PEELED;
@@ -1257,8 +1257,8 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 		    line.buf[0] == '^' &&
 		    line.len == PEELED_LINE_LENGTH &&
 		    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
-		    !get_sha1_hex(line.buf + 1, sha1)) {
-			hashcpy(last->u.value.peeled.hash, sha1);
+		    !get_oid_hex(line.buf + 1, &oid)) {
+			oidcpy(&last->u.value.peeled, &oid);
 			/*
 			 * Regardless of what the file header said,
 			 * we definitely know the value of *this*
@@ -1352,7 +1352,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
 	strbuf_add(&refname, dirname, dirnamelen);
 
 	while ((de = readdir(d)) != NULL) {
-		unsigned char sha1[20];
+		struct object_id oid;
 		struct stat st;
 		int flag;
 		const char *refdir;
@@ -1374,27 +1374,27 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
 							  refname.len, 1));
 		} else {
 			if (*refs->name) {
-				hashclr(sha1);
+				oidclr(&oid);
 				flag = 0;
-				if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
-					hashclr(sha1);
+				if (resolve_gitlink_ref(refs->name, refname.buf, oid.hash) < 0) {
+					oidclr(&oid);
 					flag |= REF_ISBROKEN;
 				}
 			} else if (read_ref_full(refname.buf,
 						 RESOLVE_REF_READING,
-						 sha1, &flag)) {
-				hashclr(sha1);
+						 oid.hash, &flag)) {
+				oidclr(&oid);
 				flag |= REF_ISBROKEN;
 			}
 			if (check_refname_format(refname.buf,
 						 REFNAME_ALLOW_ONELEVEL)) {
 				if (!refname_is_safe(refname.buf))
 					die("loose refname is dangerous: %s", refname.buf);
-				hashclr(sha1);
+				oidclr(&oid);
 				flag |= REF_BAD_NAME | REF_ISBROKEN;
 			}
 			add_entry_to_dir(dir,
-					 create_ref_entry(refname.buf, sha1, flag, 0));
+					 create_ref_entry(refname.buf, oid.hash, flag, 0));
 		}
 		strbuf_setlen(&refname, dirnamelen);
 	}
@@ -1752,8 +1752,8 @@ int read_ref(const char *refname, unsigned char *sha1)
 
 int ref_exists(const char *refname)
 {
-	unsigned char sha1[20];
-	return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
+	struct object_id oid;
+	return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, oid.hash, NULL);
 }
 
 static int filter_refs(const char *refname, const struct object_id *oid,
@@ -1857,7 +1857,7 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
 int peel_ref(const char *refname, unsigned char *sha1)
 {
 	int flag;
-	unsigned char base[20];
+	struct object_id base;
 
 	if (current_ref && (current_ref->name == refname
 			    || !strcmp(current_ref->name, refname))) {
@@ -1867,7 +1867,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
 		return 0;
 	}
 
-	if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
+	if (read_ref_full(refname, RESOLVE_REF_READING, base.hash, &flag))
 		return -1;
 
 	/*
@@ -1888,7 +1888,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
 		}
 	}
 
-	return peel_object(base, sha1);
+	return peel_object(base.hash, sha1);
 }
 
 struct warn_if_dangling_data {
@@ -2292,11 +2292,11 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 	*ref = NULL;
 	for (p = ref_rev_parse_rules; *p; p++) {
 		char fullref[PATH_MAX];
-		unsigned char sha1_from_ref[20];
+		struct object_id oid_from_ref;
 		unsigned char *this_result;
 		int flag;
 
-		this_result = refs_found ? sha1_from_ref : sha1;
+		this_result = refs_found ? oid_from_ref.hash : sha1;
 		mksnpath(fullref, sizeof(fullref), *p, len, str);
 		r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
 				       this_result, &flag);
@@ -2323,13 +2323,13 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
 
 	*log = NULL;
 	for (p = ref_rev_parse_rules; *p; p++) {
-		unsigned char hash[20];
+		struct object_id oid;
 		char path[PATH_MAX];
 		const char *ref, *it;
 
 		mksnpath(path, sizeof(path), *p, len, str);
 		ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
-					 hash, NULL);
+					 oid.hash, NULL);
 		if (!ref)
 			continue;
 		if (reflog_exists(path))
@@ -2340,7 +2340,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
 			continue;
 		if (!logs_found++) {
 			*log = xstrdup(it);
-			hashcpy(sha1, hash);
+			hashcpy(sha1, oid.hash);
 		}
 		if (!warn_ambiguous_refs)
 			break;
@@ -2479,12 +2479,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
  * Write an entry to the packed-refs file for the specified refname.
  * If peeled is non-NULL, write it as the entry's peeled value.
  */
-static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
-			       unsigned char *peeled)
+static void write_packed_entry(FILE *fh, char *refname, struct object_id *oid,
+			       struct object_id *peeled)
 {
-	fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
+	fprintf_or_die(fh, "%s %s\n", oid_to_hex(oid), refname);
 	if (peeled)
-		fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
+		fprintf_or_die(fh, "^%s\n", oid_to_hex(peeled));
 }
 
 /*
@@ -2497,9 +2497,9 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
 	if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
 		error("internal error: %s is not a valid packed reference!",
 		      entry->name);
-	write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
+	write_packed_entry(cb_data, entry->name, &entry->u.value.oid,
 			   peel_status == PEEL_PEELED ?
-			   entry->u.value.peeled.hash : NULL);
+			   &entry->u.value.peeled : NULL);
 	return 0;
 }
 
@@ -2889,7 +2889,7 @@ static int commit_ref_update(struct ref_lock *lock,
 
 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
-	unsigned char sha1[20], orig_sha1[20];
+	struct object_id oid, orig_oid;
 	int flag = 0, logmoved = 0;
 	struct ref_lock *lock;
 	struct stat loginfo;
@@ -2901,7 +2901,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 		return error("reflog for %s is a symlink", oldrefname);
 
 	symref = resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING,
-				    orig_sha1, &flag);
+				    orig_oid.hash, &flag);
 	if (flag & REF_ISSYMREF)
 		return error("refname %s is a symbolic ref, renaming it is not supported",
 			oldrefname);
@@ -2915,13 +2915,13 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 		return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
 			oldrefname, strerror(errno));
 
-	if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
+	if (delete_ref(oldrefname, orig_oid.hash, REF_NODEREF)) {
 		error("unable to delete old %s", oldrefname);
 		goto rollback;
 	}
 
-	if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) &&
-	    delete_ref(newrefname, sha1, REF_NODEREF)) {
+	if (!read_ref_full(newrefname, RESOLVE_REF_READING, oid.hash, NULL) &&
+	    delete_ref(newrefname, oid.hash, REF_NODEREF)) {
 		if (errno==EISDIR) {
 			if (remove_empty_directories(git_path("%s", newrefname))) {
 				error("Directory not empty: %s", newrefname);
@@ -2944,10 +2944,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 		strbuf_release(&err);
 		goto rollback;
 	}
-	hashcpy(lock->old_oid.hash, orig_sha1);
+	oidcpy(&lock->old_oid, &orig_oid);
 
-	if (write_ref_to_lockfile(lock, orig_sha1) ||
-	    commit_ref_update(lock, orig_sha1, logmsg)) {
+	if (write_ref_to_lockfile(lock, orig_oid.hash) ||
+	    commit_ref_update(lock, orig_oid.hash, logmsg)) {
 		error("unable to write current sha1 into %s", newrefname);
 		goto rollback;
 	}
@@ -2964,8 +2964,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
 	flag = log_all_ref_updates;
 	log_all_ref_updates = 0;
-	if (write_ref_to_lockfile(lock, orig_sha1) ||
-	    commit_ref_update(lock, orig_sha1, NULL))
+	if (write_ref_to_lockfile(lock, orig_oid.hash) ||
+	    commit_ref_update(lock, orig_oid.hash, NULL))
 		error("unable to write current sha1 into %s", oldrefname);
 	log_all_ref_updates = flag;
 
@@ -3218,11 +3218,11 @@ static int commit_ref_update(struct ref_lock *lock,
 		 * check with HEAD only which should cover 99% of all usage
 		 * scenarios (even 100% of the default ones).
 		 */
-		unsigned char head_sha1[20];
+		struct object_id head_oid;
 		int head_flag;
 		const char *head_ref;
 		head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
-					      head_sha1, &head_flag);
+					      head_oid.hash, &head_flag);
 		if (head_ref && (head_flag & REF_ISSYMREF) &&
 		    !strcmp(head_ref, lock->ref_name))
 			log_ref_write("HEAD", lock->old_oid.hash, sha1, logmsg);
@@ -3243,10 +3243,10 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
 	char ref[1000];
 	int fd, len, written;
 	char *git_HEAD = git_pathdup("%s", ref_target);
-	unsigned char old_sha1[20], new_sha1[20];
+	struct object_id old_oid, new_oid;
 
-	if (logmsg && read_ref(ref_target, old_sha1))
-		hashclr(old_sha1);
+	if (logmsg && read_ref(ref_target, old_oid.hash))
+		oidclr(&old_oid);
 
 	if (safe_create_leading_directories(git_HEAD) < 0)
 		return error("unable to create directory for %s", git_HEAD);
@@ -3292,8 +3292,8 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
 #ifndef NO_SYMLINK_HEAD
 	done:
 #endif
-	if (logmsg && !read_ref(refs_heads_master, new_sha1))
-		log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
+	if (logmsg && !read_ref(refs_heads_master, new_oid.hash))
+		log_ref_write(ref_target, old_oid.hash, new_oid.hash, logmsg);
 
 	free(git_HEAD);
 	return 0;
-- 
2.4.0
Previous: brian m. carlsonNext: brian m. carlson
Message 2 of 23 in “object_id part 2”
  1. 0/8 object_id part 2brian m. carlson, Jun 9, 2015
  2. 1/8 refs: convert some internal functions to use object_idbrian m. carlson, Jun 9, 2015
  3. 2/8 sha1_file: introduce has_object_file helper.brian m. carlson, Jun 9, 2015
  4. Duy NguyenJun 10, 2015
  5. brian m. carlsonJun 10, 2015
  6. 3/8 Convert struct ref to use object_id.brian m. carlson, Jun 9, 2015
  7. Michael HaggertyJun 11, 2015
  8. 4/8 Add a utility function to make parsing hex values easier.brian m. carlson, Jun 9, 2015
  9. Michael HaggertyJun 11, 2015
  10. 5/8 add_sought_entry_mem: convert to struct object_idbrian m. carlson, Jun 9, 2015
  11. 6/8 parse_fetch: convert to use struct object_idbrian m. carlson, Jun 9, 2015
  12. 7/8 ref_newer: convert to use struct object_idbrian m. carlson, Jun 9, 2015
  13. Junio C HamanoJun 10, 2015
  14. brian m. carlsonJun 10, 2015
  15. brian m. carlsonJun 11, 2015
  16. Junio C HamanoJun 11, 2015
  17. brian m. carlsonJun 11, 2015
  18. Junio C HamanoJun 11, 2015
  19. brian m. carlsonJun 12, 2015
  20. Junio C HamanoJun 12, 2015
  21. brian m. carlsonJun 12, 2015
  22. Michael HaggertyJun 13, 2015
  23. brian m. carlsonJun 13, 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.