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

[PATCH v2 1/2] fetch: allow asking for an explicit commit object by name

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 13, 2011, 22:28 UTC
Message-ID
<1315952896-17258-2-git-send-email-gitster@pobox.com>
In-Reply-To
<1315952896-17258-1-git-send-email-gitster@pobox.com>

This teaches "git fetch" (hence "git pull") to accept an explicit commit object name in the LHS of the refspec, as long as the named commit is at the tip of an advertised ref. E.g.

    $ git pull origin 5738c9c21e53356ab5020912116e7f82fd2d428f
    $ git fetch origin 5738c9c21e53356ab5020912116e7f82fd2d428f:refs/remotes/origin
would behave exactly as if you asked
    $ git pull origin refs/heads/master
    $ git fetch origin refs/heads/master:refs/remotes/origin

when the output from "git ls-remote origin" said the remote side has the commit object whose name is 5738c9c21e53356ab5020912116e7f82fd2d428f at the tip of refs/heads/master branch ref.

This does not allow asking for a random object that may or may not exist in the repository (this has been a longstanding security feature).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 remote.c |   25 +++++++++++++++++++++++--
 1 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/remote.c b/remote.c
index ca42a12..76c2943 100644
--- a/remote.c
+++ b/remote.c
@@ -1387,6 +1387,25 @@ struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
 	return copy_ref(ref);
 }
 
+/*
+ * Allow fetching an explicitly-named commit from the command line,
+ * but only if it exactly matches the commit at the tip of one of the
+ * advertised refs.
+ */
+static struct ref *get_remote_commit(const struct ref *remote_refs, const char *hex)
+{
+	const struct ref *ref;
+	unsigned char sha1[20];
+
+	if (get_sha1_hex(hex, sha1) || hex[40])
+		return NULL;
+
+	for (ref = remote_refs; ref; ref = ref->next)
+		if (!strchr(ref->name, '^') && !hashcmp(sha1, ref->old_sha1))
+			return copy_ref(ref);
+	return NULL;
+}
+
 static struct ref *get_local_ref(const char *name)
 {
 	if (!name || name[0] == '\0')
@@ -1416,8 +1435,10 @@ int get_fetch_map(const struct ref *remote_refs,
 		const char *name = refspec->src[0] ? refspec->src : "HEAD";
 
 		ref_map = get_remote_ref(remote_refs, name);
-		if (!missing_ok && !ref_map)
-			die("Couldn't find remote ref %s", name);
+		if (!ref_map)
+			ref_map = get_remote_commit(remote_refs, name);
+		if (!ref_map && !missing_ok)
+			die("Couldn't find remote ref that matches %s", name);
 		if (ref_map) {
 			ref_map->peer_ref = get_local_ref(refspec->dst);
 			if (ref_map->peer_ref && refspec->force)
-- 
1.7.7.rc1.1.g1e5814
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 62 in “[Survey] Signed push”
  1. Junio C HamanoSep 13, 2011
  2. 0/2 State commit name explicitly in request-pull messagesJunio C Hamano, Sep 13, 2011
  3. 1/2 fetch: allow asking for an explicit commit object by nameJunio C Hamano, Sep 13, 2011
  4. 2/2 request-pull: state exact commit object nameJunio C Hamano, Sep 13, 2011
  5. Guenter RoeckSep 13, 2011
  6. Junio C HamanoSep 13, 2011
  7. Junio C HamanoSep 14, 2011
  8. Sam VilainSep 14, 2011
  9. Shawn PearceSep 14, 2011
  10. Sam VilainSep 14, 2011
  11. Nguyen Thai Ngoc DuySep 14, 2011
  12. Jonathan NiederSep 14, 2011
  13. Nguyen Thai Ngoc DuySep 14, 2011
  14. Jeff KingSep 15, 2011
  15. Andy LutomirskiSep 14, 2011
  16. Junio C HamanoSep 14, 2011
  17. Andrew LutomirskiSep 14, 2011
  18. Fwd: [Survey] Signed pushLinus Torvalds, Sep 14, 2011
  19. Michael HaggertySep 14, 2011
  20. Matthieu MoySep 14, 2011
  21. Nguyen Thai Ngoc DuySep 14, 2011
  22. Johan HerlandSep 14, 2011
  23. Ted Ts'oSep 14, 2011
  24. Linus TorvaldsSep 14, 2011
  25. Matthieu MoySep 14, 2011
  26. Johan HerlandSep 14, 2011
  27. Philip OakleySep 14, 2011
  28. Linus TorvaldsSep 14, 2011
  29. Junio C HamanoSep 14, 2011
  30. Linus TorvaldsSep 14, 2011
  31. Junio C HamanoSep 14, 2011
  32. Linus TorvaldsSep 14, 2011
  33. Junio C HamanoSep 14, 2011
  34. Sam VilainSep 14, 2011
  35. request-pull: state what commit to expectJunio C Hamano, Sep 16, 2011
  36. Junio C HamanoSep 20, 2011
  37. 2/3 branch: teach --edit-description optionJunio C Hamano, Sep 20, 2011
  38. Andrew ArdillSep 21, 2011
  39. Junio C HamanoSep 21, 2011
  40. request-pull: use the branch descriptionJunio C Hamano, Sep 20, 2011
  41. 0/6 A handful of "branch description" patchesJunio C Hamano, Sep 22, 2011
  42. 1/6 branch: add read_branch_desc() helper functionJunio C Hamano, Sep 22, 2011
  43. 2/6 format-patch: use branch description in cover letterJunio C Hamano, Sep 22, 2011
  44. 3/6 branch: teach --edit-description optionJunio C Hamano, Sep 22, 2011
  45. Michael J GruberSep 23, 2011
  46. Nguyen Thai Ngoc DuySep 23, 2011
  47. Junio C HamanoSep 23, 2011
  48. Nguyen Thai Ngoc DuySep 25, 2011
  49. 4/6 request-pull: modernize styleJunio C Hamano, Sep 22, 2011
  50. 5/6 request-pull: state what commit to expectJunio C Hamano, Sep 22, 2011
  51. 6/6 request-pull: use the branch descriptionJunio C Hamano, Sep 22, 2011
  52. Michael J GruberSep 23, 2011
  53. Jeff KingSep 23, 2011
  54. Junio C HamanoSep 23, 2011
  55. Jeff KingSep 23, 2011
  56. Michael J GruberSep 24, 2011
  57. Jeff KingSep 27, 2011
  58. Annotated branch ≈ annotated tag?Michael Haggerty, Sep 28, 2011
  59. Andrew ArdillSep 28, 2011
  60. Michael HaggertySep 28, 2011
  61. Branch annotations [Re: Annotated branch ≈ annotated tag?]Michael J Gruber, Sep 28, 2011
  62. Jeff KingSep 29, 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.