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

[PATCH 6/8] Convert resolve_ref_unsafe+xstrdup to resolve_ref

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 17, 2011, 09:32 UTC
Message-ID
<1321522335-24193-7-git-send-email-pclouds@gmail.com>
In-Reply-To
<1321522335-24193-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/branch.c       |    3 +--
 builtin/checkout.c     |   13 +++++++------
 builtin/for-each-ref.c |    7 ++-----
 builtin/show-branch.c  |    4 +---
 reflog-walk.c          |    7 +++----
 wt-status.c            |    4 +---
 6 files changed, 15 insertions(+), 23 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 633b56e..a254898 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -689,10 +689,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	track = git_branch_track;
 
-	head = resolve_ref_unsafe("HEAD", head_sha1, 0, NULL);
+	head = resolve_ref("HEAD", head_sha1, 0, NULL);
 	if (!head)
 		die(_("Failed to resolve HEAD as a valid ref."));
-	head = xstrdup(head);
 	if (!strcmp(head, "HEAD")) {
 		detached = 1;
 	} else {
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2b8e73b..6efb1cf 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -696,15 +696,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 {
 	int ret = 0;
 	struct branch_info old;
+	char *path;
 	unsigned char rev[20];
 	int flag;
 	memset(&old, 0, sizeof(old));
-	old.path = xstrdup(resolve_ref_unsafe("HEAD", rev, 0, &flag));
+	old.path = path = resolve_ref("HEAD", rev, 0, &flag);
 	old.commit = lookup_commit_reference_gently(rev, 1);
-	if (!(flag & REF_ISSYMREF)) {
-		free((char *)old.path);
+	if (!(flag & REF_ISSYMREF))
 		old.path = NULL;
-	}
 
 	if (old.path && !prefixcmp(old.path, "refs/heads/"))
 		old.name = old.path + strlen("refs/heads/");
@@ -718,8 +717,10 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 	}
 
 	ret = merge_working_tree(opts, &old, new);
-	if (ret)
+	if (ret) {
+		free(path);
 		return ret;
+	}
 
 	if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
 		orphaned_commit_warning(old.commit);
@@ -727,7 +728,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 	update_refs_for_switch(opts, &old, new);
 
 	ret = post_checkout_hook(old.commit, new->commit, 1);
-	free((char *)old.path);
+	free(path);
 	return ret || opts->writeout_error;
 }
 
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index b954ca8..dc19f3c 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -628,11 +628,8 @@ static void populate_value(struct refinfo *ref)
 
 	if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
 		unsigned char unused1[20];
-		const char *symref;
-		symref = resolve_ref_unsafe(ref->refname, unused1, 1, NULL);
-		if (symref)
-			ref->symref = xstrdup(symref);
-		else
+		ref->symref = resolve_ref(ref->refname, unused1, 1, NULL);
+		if (!ref->symref)
 			ref->symref = "";
 	}
 
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 1e7bd31..9e849c7 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -726,10 +726,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 
 		if (ac == 0) {
 			static const char *fake_av[2];
-			const char *refname;
 
-			refname = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
-			fake_av[0] = xstrdup(refname);
+			fake_av[0] = resolve_ref("HEAD", sha1, 1, NULL);
 			fake_av[1] = NULL;
 			av = fake_av;
 			ac = 1;
diff --git a/reflog-walk.c b/reflog-walk.c
index 2d5aee0..fd17e71 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -170,11 +170,10 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
 	else {
 		if (*branch == '\0') {
 			unsigned char sha1[20];
-			const char *head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
-			if (!head)
-				die ("No current branch");
 			free(branch);
-			branch = xstrdup(head);
+			branch = resolve_ref("HEAD", sha1, 0, NULL);
+			if (!branch)
+				die ("No current branch");
 		}
 		reflogs = read_complete_reflog(branch);
 		if (!reflogs || reflogs->nr == 0) {
diff --git a/wt-status.c b/wt-status.c
index cc6dad5..77adf64 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -111,7 +111,6 @@ void status_printf_more(struct wt_status *s, const char *color,
 void wt_status_prepare(struct wt_status *s)
 {
 	unsigned char sha1[20];
-	const char *head;
 
 	memset(s, 0, sizeof(*s));
 	memcpy(s->color_palette, default_wt_status_colors,
@@ -119,8 +118,7 @@ void wt_status_prepare(struct wt_status *s)
 	s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 	s->use_color = -1;
 	s->relative_paths = 1;
-	head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
-	s->branch = head ? xstrdup(head) : NULL;
+	s->branch = resolve_ref("HEAD", sha1, 0, NULL);
 	s->reference = "HEAD";
 	s->fp = stdout;
 	s->index_file = get_index_file();
-- 
1.7.4.74.g639db
Previous: Nguyễn Thái Ngọc DuyNext: Ramkumar Ramachandra
Message 9 of 27 in “nd/resolve-ref v2”
  1. 0/8 nd/resolve-ref v2Nguyễn Thái Ngọc Duy, Nov 17, 2011
  2. 1/8 Convert many resolve_ref() calls to read_ref*() and ref_exists()Nguyễn Thái Ngọc Duy, Nov 17, 2011
  3. 2/8 Rename resolve_ref() to resolve_ref_unsafe()Nguyễn Thái Ngọc Duy, Nov 17, 2011
  4. 3/8 Re-add resolve_ref() that always returns an allocated bufferNguyễn Thái Ngọc Duy, Nov 17, 2011
  5. 4/8 cmd_merge: convert to single exit pointNguyễn Thái Ngọc Duy, Nov 17, 2011
  6. Ramkumar RamachandraNov 17, 2011
  7. Jonathan NiederNov 17, 2011
  8. 5/8 Use resolve_ref() instead of resolve_ref_unsafe()Nguyễn Thái Ngọc Duy, Nov 17, 2011
  9. 6/8 Convert resolve_ref_unsafe+xstrdup to resolve_refNguyễn Thái Ngọc Duy, Nov 17, 2011
  10. Ramkumar RamachandraNov 17, 2011
  11. Nguyen Thai Ngoc DuyNov 18, 2011
  12. 7/8 Guard memory overwriting in resolve_ref_unsafe's static bufferNguyễn Thái Ngọc Duy, Nov 17, 2011
  13. 8/8 Enable GIT_DEBUG_MEMCHECK on git_pathname()Nguyễn Thái Ngọc Duy, Nov 17, 2011
  14. Ramkumar RamachandraNov 17, 2011
  15. Jeff KingNov 17, 2011
  16. Nguyen Thai Ngoc DuyNov 18, 2011
  17. Jeff KingNov 18, 2011
  18. Jonathan NiederNov 18, 2011
  19. Nguyen Thai Ngoc DuyNov 18, 2011
  20. Jeff KingNov 18, 2011
  21. Jonathan NiederNov 18, 2011
  22. Johan HerlandNov 18, 2011
  23. Jonathan NiederNov 18, 2011
  24. Junio C HamanoNov 18, 2011
  25. Bernhard R. LinkNov 18, 2011
  26. Jonathan NiederNov 17, 2011
  27. Nguyen Thai Ngoc DuyDec 6, 2011

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.