diff mbox series

[1/4] refs: let repo_dwim_ref() learn get symref itself and ref flags

Message ID 22b69757e33d310e78232dbe520a1995486ec918.1632123476.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series ref-filter: fix %(symref) for pretty_print_ref() | expand

Commit Message

ZheNing Hu Sept. 20, 2021, 7:37 a.m. UTC
From: ZheNing Hu <adlternative@gmail.com>

expand_ref() can use to resolve a ref to its fullname, A symref will
also be resolved to the fullref name it refers to. But sometimes we
want get symref itself. So add a need_symref parameter to
repo_dwim_ref() and expand_ref(), which can help us get symref its
fullref name. At the same time, we add ref_flags parameter to
expand_ref() and repo_dwim_ref(), when it is set, it can get the
ref's flag from refs_resolve_ref_unsafe(), which can help us provide
ref's flags for interfaces like pretty_print_ref() later.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 object-name.c |  6 +++---
 refs.c        | 15 ++++++++++-----
 refs.h        |  8 +++++---
 upload-pack.c |  2 +-
 4 files changed, 19 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/object-name.c b/object-name.c
index fdff4601b2c..a8cb1d6ab14 100644
--- a/object-name.c
+++ b/object-name.c
@@ -803,7 +803,7 @@  static int get_oid_basic(struct repository *r, const char *str, int len,
 
 	if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
 		if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
-			refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
+			refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0, 0, 0);
 			if (refs_found > 0) {
 				warning(warn_msg, len, str);
 				if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
@@ -854,11 +854,11 @@  static int get_oid_basic(struct repository *r, const char *str, int len,
 
 	if (!len && reflog_len)
 		/* allow "@{...}" to mean the current branch reflog */
-		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0, 0, 0);
 	else if (reflog_len)
 		refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
 	else
-		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0, 0, 0);
 
 	if (!refs_found)
 		return -1;
diff --git a/refs.c b/refs.c
index 8b9f7c3a80a..67618a09992 100644
--- a/refs.c
+++ b/refs.c
@@ -637,17 +637,19 @@  static char *substitute_branch_name(struct repository *r,
 }
 
 int repo_dwim_ref(struct repository *r, const char *str, int len,
-		  struct object_id *oid, char **ref, int nonfatal_dangling_mark)
+		  struct object_id *oid, char **ref, int nonfatal_dangling_mark,
+		  int *ref_flags, int need_symref)
 {
 	char *last_branch = substitute_branch_name(r, &str, &len,
 						   nonfatal_dangling_mark);
-	int   refs_found  = expand_ref(r, str, len, oid, ref);
+	int   refs_found  = expand_ref(r, str, len, oid, ref, ref_flags, need_symref);
 	free(last_branch);
 	return refs_found;
 }
 
 int expand_ref(struct repository *repo, const char *str, int len,
-	       struct object_id *oid, char **ref)
+	       struct object_id *oid, char **ref, int *ref_flags,
+	       int need_symref)
 {
 	const char **p, *r;
 	int refs_found = 0;
@@ -666,8 +668,11 @@  int expand_ref(struct repository *repo, const char *str, int len,
 					    fullref.buf, RESOLVE_REF_READING,
 					    this_result, &flag);
 		if (r) {
-			if (!refs_found++)
-				*ref = xstrdup(r);
+			if (!refs_found++) {
+				*ref = xstrdup(need_symref ? fullref.buf : r);
+				if (ref_flags)
+					*ref_flags = flag;
+			}
 			if (!warn_ambiguous_refs)
 				break;
 		} else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
diff --git a/refs.h b/refs.h
index 48970dfc7e0..1f977bdb188 100644
--- a/refs.h
+++ b/refs.h
@@ -152,15 +152,17 @@  int refname_match(const char *abbrev_name, const char *full_name);
 struct strvec;
 void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
 
-int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
+int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref,
+	       int *ref_flags, int need_symref);
 int repo_dwim_ref(struct repository *r, const char *str, int len,
-		  struct object_id *oid, char **ref, int nonfatal_dangling_mark);
+		  struct object_id *oid, char **ref, int nonfatal_dangling_mark,
+		  int *ref_flags, int need_symref);
 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
 static inline int dwim_ref(const char *str, int len, struct object_id *oid,
 			   char **ref, int nonfatal_dangling_mark)
 {
 	return repo_dwim_ref(the_repository, str, len, oid, ref,
-			     nonfatal_dangling_mark);
+			     nonfatal_dangling_mark, 0, 0);
 }
 int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
 
diff --git a/upload-pack.c b/upload-pack.c
index 6ce07231d3d..dfbdd6d9466 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -975,7 +975,7 @@  static int process_deepen_not(const char *line, struct string_list *deepen_not,
 	if (skip_prefix(line, "deepen-not ", &arg)) {
 		char *ref = NULL;
 		struct object_id oid;
-		if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
+		if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref, 0, 0) != 1)
 			die("git upload-pack: ambiguous deepen-not: %s", line);
 		string_list_append(deepen_not, ref);
 		free(ref);