git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	kyle@kyleam.com, Heiko.Boettger@karlstorz.com,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH/RFC] get_oid: new extended SHA-1 syntax to control resolution process
Date: Wed, 26 Jun 2019 15:41:39 +0700	[thread overview]
Message-ID: <20190626084139.30246-1-pclouds@gmail.com> (raw)
In-Reply-To: <8C0042D8869AEA4AA334B49AFBBCEF820243C01B6A@TUT-EX01-PV.KSTG.corp>

[I feel like this is something we should do, but I'm not sure. Hence
RFC. The patch is mostly to play with if you're curious. The die() thing
there is definitely wrong. And yeah all the syntax names are
placeholders.]

get_oid() is flexible and accepts multiple SHA-1 sources. Sometimes this
flexibility is actually unwanted, especially for scripts. Let's talk
problems:

- Ambiguity aside, a script may want to check if branch A exists. Of
  course "git rev-parse A" won't cut it. But even "git rev-parse
  refs/heads/A" may fail: if you have refs/heads/refs/heads/A for
  whatever reason and the real branch "A" does not exist, the rev-parse
  rules allow to expand "refs/heads/A" to the long ref.

- And then there's problem with using the wrong rule. 9309ba may look
  like a short hash. But if such short hash does not match any object,
  and there is refs/heads/9309ba, you'll have a little surprise.

- The same for blahblah-g9309ba which could either be expanded to
  refs/heads/blahblah-g9309ba, or interpreted as git-describe output.

- Ambiguation will also cause problems, but I don't think we need to
  get into that. Ambiguation may be addressed separately actually.

There could be existing mitigation (e.g. maybe you can resolve
blahblah-g9309ba and see if it's a ref or not). But it feels like we
work around the problem than addressing it.

The problem is we try every possible way to resolve a rev. Let's have
some annotation to express that we only want to resolve a rev in a
certain way:

- <hash>@{hash} only accepts a full hash or a short hash. If it's a
  short hash, it cannot be ambiguous.

- <ref>@{literal} only accepts full ref. No turning "master" into
  "refs/heads/master".

- <output>@{describe} interprets <output> as git-describe output
  only, not an object name or a reference.

This gives scripts much better control over get_oid(), which
translates to rev-parse and a bunch other commands.

PS. The new syntax can stack with existing ones. E.g. you could write
refs/heads/master@{literal}@{yesterday} or <hash>@{hash}^{tree}.
Perhaps I should allow these tags at the end too, so you can enforce a
variable like "$REV"@{literal} where $REV could be even HEAD~123

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/revisions.txt | 10 ++++++++
 refs.c                      | 41 +++++++++++++++++++++++++++++--
 refs.h                      |  2 ++
 sha1-name.c                 | 48 +++++++++++++++++++++++++++++++++++--
 4 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/Documentation/revisions.txt b/Documentation/revisions.txt
index 82c1e5754e..93eb278743 100644
--- a/Documentation/revisions.txt
+++ b/Documentation/revisions.txt
@@ -86,6 +86,16 @@ some output processing may assume ref names in UTF-8.
   immediately following a ref name and the ref must have an existing
   log ('$GIT_DIR/logs/<refname>').
 
+'<refname>@{literal}'::
+  The ref is not expanded by Git. In other words, if '$GIT_DIR/<refname>'
+  does not exist, the ref is not valid.
+
+'<sha1>@{hash}'::
+  '<sha1>' must be an unambiguous (short or full) object name.
+
+'<describeOutput>@{describe}'::
+  ...
+
 '@{<n>}', e.g. '@\{1\}'::
   You can use the '@' construct with an empty ref part to get at a
   reflog entry of the current branch. For example, if you are on
diff --git a/refs.c b/refs.c
index b8a8430c96..2ee33257fd 100644
--- a/refs.c
+++ b/refs.c
@@ -634,6 +634,27 @@ int repo_dwim_ref(struct repository *r, const char *str, int len,
 	return refs_found;
 }
 
+int repo_dwim_ref_strict(struct repository *r,
+			 const char *str, int len,
+			 struct object_id *oid,
+			 char **ref)
+{
+	char *last_branch = substitute_branch_name(r, &str, &len);
+	struct strbuf sb = STRBUF_INIT;
+	int flag;
+
+	FREE_AND_NULL(last_branch);
+	strbuf_add(&sb, str, len);
+	*ref = xstrdup_or_null(
+		refs_resolve_ref_unsafe(get_main_ref_store(r),
+					sb.buf,
+					RESOLVE_REF_READING,
+					oid,
+					&flag));
+	strbuf_release(&sb);
+	return *ref != NULL;
+}
+
 int dwim_ref(const char *str, int len, struct object_id *oid, char **ref)
 {
 	return repo_dwim_ref(the_repository, str, len, oid, ref);
@@ -673,8 +694,9 @@ int expand_ref(struct repository *repo, const char *str, int len,
 	return refs_found;
 }
 
-int repo_dwim_log(struct repository *r, const char *str, int len,
-		  struct object_id *oid, char **log)
+static int do_dwim_log(struct repository *r, const char *str, int len,
+		       struct object_id *oid, char **log,
+		       int ignore_rev_parse_rules)
 {
 	struct ref_store *refs = get_main_ref_store(r);
 	char *last_branch = substitute_branch_name(r, &str, &len);
@@ -687,6 +709,9 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
 		struct object_id hash;
 		const char *ref, *it;
 
+		if (ignore_rev_parse_rules && p != ref_rev_parse_rules)
+			break;
+
 		strbuf_reset(&path);
 		strbuf_addf(&path, *p, len, str);
 		ref = refs_resolve_ref_unsafe(refs, path.buf,
@@ -713,6 +738,18 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
 	return logs_found;
 }
 
+int repo_dwim_log(struct repository *r, const char *str, int len,
+		  struct object_id *oid, char **log)
+{
+	return do_dwim_log(r, str, len, oid, log, 0);
+}
+
+int repo_dwim_log_strict(struct repository *r, const char *str, int len,
+			 struct object_id *oid, char **log)
+{
+	return do_dwim_log(r, str, len, oid, log, 1);
+}
+
 int dwim_log(const char *str, int len, struct object_id *oid, char **log)
 {
 	return repo_dwim_log(the_repository, str, len, oid, log);
diff --git a/refs.h b/refs.h
index 730d05ad91..9395a2e708 100644
--- a/refs.h
+++ b/refs.h
@@ -151,6 +151,8 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix);
 int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
 int repo_dwim_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
+int repo_dwim_ref_strict(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
+int repo_dwim_log_strict(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
 int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
 int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
 
diff --git a/sha1-name.c b/sha1-name.c
index 728e6f1f61..2d05414140 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -789,6 +789,25 @@ static inline int push_mark(const char *string, int len)
 	return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
 }
 
+static inline int literal_mark(const char *string, int len)
+{
+	const char *suffix[] = { "@{literal}" };
+	return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
+}
+
+static inline int find_mark(const char *mark, const char *string, int len, int *len_p)
+{
+	int mark_len = strlen(mark);
+
+	if (mark_len <= len &&
+	    !strncasecmp(string + len - mark_len, mark, mark_len)) {
+		if (len_p)
+			*len_p -= mark_len;
+		return 1;
+	}
+	return 0;
+}
+
 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
 
@@ -811,6 +830,18 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 	int refs_found = 0;
 	int at, reflog_len, nth_prior = 0;
 
+	if (find_mark("@{hash}", str, len, &len)) {
+		struct strbuf sb = STRBUF_INIT;
+
+		strbuf_add(&sb, str, len);
+		if (!get_oid_hex(sb.buf, oid) ||
+		    !get_short_oid(r, sb.buf, sb.len, oid, flags)) {
+			strbuf_release(&sb);
+			return 0;
+		}
+		die("Not a hash. Yes I know this should be fatal");
+	}
+
 	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);
@@ -837,7 +868,8 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 					continue;
 				}
 				if (!upstream_mark(str + at, len - at) &&
-				    !push_mark(str + at, len - at)) {
+				    !push_mark(str + at, len - at) &&
+				    !literal_mark(str + at, len - at)) {
 					reflog_len = (len-1) - (at+2);
 					len = at;
 				}
@@ -862,7 +894,14 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 		}
 	}
 
-	if (!len && reflog_len)
+	if (find_mark("@{literal}", str, len, &len)) {
+		if (reflog_len)
+			refs_found = repo_dwim_log_strict(r, str, len, oid, &real_ref);
+		else
+			refs_found = repo_dwim_ref_strict(r, str, len, oid, &real_ref);
+		if (!refs_found)
+			die("bad ref");
+	} else if (!len && reflog_len)
 		/* allow "@{...}" to mean the current branch reflog */
 		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref);
 	else if (reflog_len)
@@ -1180,6 +1219,11 @@ static enum get_oid_result get_oid_1(struct repository *r,
 	if (!ret)
 		return FOUND;
 
+	if (find_mark("@{describe}", name, len, &len)) {
+		ret = get_describe_name(r, name, len, oid);
+		return !ret ? FOUND : MISSING_OBJECT;
+	}
+
 	ret = get_oid_basic(r, name, len, oid, lookup_flags);
 	if (!ret)
 		return FOUND;
-- 
2.22.0.rc0.322.g2b0371e29a


  parent reply	other threads:[~2019-06-26  8:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-20 14:32 specifying revision - how to enforce matching a tag/branch-name or revision only Boettger, Heiko
2019-06-20 22:54 ` Kyle Meyer
2019-06-21 15:16   ` Junio C Hamano
2019-06-25 11:43     ` Duy Nguyen
2019-06-25 14:35       ` AW: " Boettger, Heiko
2019-06-26  8:41 ` Nguyễn Thái Ngọc Duy [this message]
2019-06-26 18:51   ` [PATCH/RFC] get_oid: new extended SHA-1 syntax to control resolution process Junio C Hamano
2019-06-27  2:52     ` Duy Nguyen
2019-06-27 16:47       ` Junio C Hamano
2019-06-30  9:30   ` Jakub Narebski
2019-07-01  0:30     ` Duy Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190626084139.30246-1-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=Heiko.Boettger@karlstorz.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=kyle@kyleam.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).