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

Re: [PATCH v5 4/4] builtin/stash: provide a way to import stashes from a ref

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
May 12, 2025, 09:10 UTC
Message-ID
<94b26c62-c8da-49ba-a4f1-66da20956c0b@gmail.com>
In-Reply-To
<20250508234458.3665894-5-sandals@crustytoothpaste.net>
Hi brian
On 09/05/2025 00:44, brian m. carlson wrote:
Show 38 quoted lines
> 
> @@ -1962,6 +1971,99 @@ static int write_commit_with_parents(struct repository *r,
>   	return ret;
>   }
>   
> +static int do_import_stash(struct repository *r, const char *rev)
> +{
> +	struct object_id chain;
> +	struct oid_array items = OID_ARRAY_INIT;
> +	int res = 0;
> +	int i;
> +	const char *buffer = NULL;
> +	struct commit *this = NULL;
> +	char *msg = NULL;
> +
> +	if (repo_get_oid(r, rev, &chain))
> +		return error(_("not a valid revision: %s"), rev);
> +
> +	/*
> +	 * Walk the commit history, finding each stash entry, and load data into
> +	 * the array.
> +	 */
> +	for (i = 0;; i++) {
> +		struct object_id tree, oid;
> +		char revision[GIT_MAX_HEXSZ + 1];
> +
> +		oid_to_hex_r(revision, &chain);
> +
> +		if (get_oidf(&tree, "%s:", revision) ||
> +		    !oideq(&tree, r->hash_algo->empty_tree)) {
> +			res = error(_("%s is not a valid exported stash commit"), revision);
> +			goto out;
> +		}
> +		if (get_oidf(&chain, "%s^1", revision) ||
> +		    get_oidf(&oid, "%s^2", revision))
> +			break;
> +		oid_array_append(&items, &oid);
> +	}

This loop could use some improvement - it does not use the loop variable, it accepts any commit with an empty tree as a valid exported stash, it is pretty lax about checking that the commits in the chain have either zero or two parents, it does not check if the second parent looks like a stash and it is forever converting between strings and object_ids. I think it would be better to loop over commits rather than object ids then you can walk the history using the pointers to the parent commits. Something like

	if (repo_get_oid(r, rev, &chain))
		return error(_("not a valid revision: %s"), rev);
	this = lookup_commit_reference(r, &chain);
	if (!this)
		return error(_("not a commit: %s"), rev);
	/*
	 * Walk the commit history, finding each stash entry, and load data into
	 * the array.
	 */
	for (;;) {
		struct commit *stash;
		struct tree *tree = repo_get_commit_tree(r, this);
		if (!tree ||
		    !oideq(&tree->object.oid, r->hash_algo->empty_tree) ||
		    (this->parents &&
		     (!this->parents->next || this->parents->next->next))) {
			res = error(_("%s is not a valid exported stash commit"),
				    oid_to_hex(&this->object.oid));
			goto out;
		}
		if (!this->parents)
			break;
		stash = this->parents->next->item;
		if (repo_parse_commit(r, this->parents->item) ||
		    repo_parse_commit(r, stash)) {
			res = error(_("cannot parse parents of commit: %s"),
				     oid_to_hex(&this->object.oid));
			goto out;
		}
		if (check_stash_topology(r, stash)) {
			res = error(_("%s does not look like a stash commit"),
				    oid_to_hex(&stash->object.oid));
			goto out;
		}
		/* TODO:
		 *  - store commits, not objects
		 */
		oid_array_append(&items, &this->parents->next->item->object.oid);
		this = this->parents->item;
	}

I've appended a fixup commit below that applies on top of your patch. The fixups for this patch and the previous one can be fetched with

   git fetch https://github.com/phillipwood/git.git bc/stash-import-export-fixups
if you want to use them.
Best Wishes
Phillip
---- >8 ----
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH] fixup! builtin/stash: provide a way to import stashes from a
  ref

Strengthen the checks on imported commits to ensure that the chain of imported stashes consists of commits with exactly two parents where the first parent is either the root commit or another imported stash commit and the second parent looks like a stash commit.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
  builtin/stash.c | 43 +++++++++++++++++++++++++++++++------------
  1 file changed, 31 insertions(+), 12 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c
index 02cf344ed9..7d3a8c03a0 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -2011,25 +2011,44 @@ static int do_import_stash(struct repository *r, const char *rev)
  	if (repo_get_oid(r, rev, &chain))
  		return error(_("not a valid revision: %s"), rev);
  
+	this = lookup_commit_reference(r, &chain);
+	if (!this)
+		return error(_("not a commit: %s"), rev);
  	/*
  	 * Walk the commit history, finding each stash entry, and load data into
  	 * the array.
  	 */
-	for (i = 0;; i++) {
-		struct object_id tree, oid;
-		char revision[GIT_MAX_HEXSZ + 1];
-
-		oid_to_hex_r(revision, &chain);
-
-		if (get_oidf(&tree, "%s:", revision) ||
-		    !oideq(&tree, r->hash_algo->empty_tree)) {
-			res = error(_("%s is not a valid exported stash commit"), revision);
+	for (;;) {
+		struct commit *stash;
+		struct tree *tree = repo_get_commit_tree(r, this);
+
+		if (!tree ||
+		    !oideq(&tree->object.oid, r->hash_algo->empty_tree) ||
+		    (this->parents &&
+		     (!this->parents->next || this->parents->next->next))) {
+			res = error(_("%s is not a valid exported stash commit"),
+				    oid_to_hex(&this->object.oid));
  			goto out;
  		}
-		if (get_oidf(&chain, "%s^1", revision) ||
-		    get_oidf(&oid, "%s^2", revision))
+		if (!this->parents)
  			break;
-		oid_array_append(&items, &oid);
+		stash = this->parents->next->item;
+		if (repo_parse_commit(r, this->parents->item) ||
+		    repo_parse_commit(r, stash)) {
+			res = error(_("cannot parse parents of commit: %s"),
+				     oid_to_hex(&this->object.oid));
+			goto out;
+		}
+		if (check_stash_topology(r, stash)) {
+			res = error(_("%s does not look like a stash commit"),
+				    oid_to_hex(&stash->object.oid));
+			goto out;
+		}
+		/* TODO:
+		 *  - store commits, not objects
+		 */
+		oid_array_append(&items, &this->parents->next->item->object.oid);
+		this = this->parents->item;
  	}
  
  	/*
-- 
2.49.0.300.g813f75aecee
Previous: Jeff KingNext: Junio C Hamano
Message 22 of 72 in “Importing and exporting stashes to refs”
  1. 0/4 Importing and exporting stashes to refsbrian m. carlson, May 8, 2025
  2. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, May 8, 2025
  3. Junio C HamanoMay 9, 2025
  4. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, May 8, 2025
  5. Junio C HamanoMay 9, 2025
  6. Junio C HamanoMay 9, 2025
  7. Jeff KingMay 10, 2025
  8. Phillip WoodMay 12, 2025
  9. Junio C HamanoMay 12, 2025
  10. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, May 8, 2025
  11. Junio C HamanoMay 9, 2025
  12. brian m. carlsonMay 9, 2025
  13. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, May 8, 2025
  14. Junio C HamanoMay 9, 2025
  15. brian m. carlsonMay 11, 2025
  16. Jeff KingMay 10, 2025
  17. Junio C HamanoMay 12, 2025
  18. Jeff KingMay 12, 2025
  19. Junio C HamanoMay 12, 2025
  20. brian m. carlsonMay 12, 2025
  21. Jeff KingMay 10, 2025
  22. Phillip WoodMay 12, 2025
  23. Junio C HamanoMay 9, 2025
  24. brian m. carlsonMay 9, 2025
  25. D. Ben KnobleMay 9, 2025
  26. brian m. carlsonMay 9, 2025
  27. D. Ben KnobleMay 10, 2025
  28. Makefile: avoid constant rebuilds with compilation databasebrian m. carlson, May 22, 2025
  29. 3/5 builtin/stash: factor out revision parsing into a functionbrian m. carlson, May 22, 2025
  30. Junio C HamanoMay 22, 2025
  31. brian m. carlsonMay 23, 2025
  32. Junio C HamanoMay 24, 2025
  33. brian m. carlsonMay 26, 2025
  34. 2/5 reflog-walk: expose read_complete_reflogbrian m. carlson, May 22, 2025
  35. Ramsay JonesMay 22, 2025
  36. brian m. carlsonMay 23, 2025
  37. Ramsay JonesMay 24, 2025
  38. brian m. carlsonMay 26, 2025
  39. Phillip WoodMay 29, 2025
  40. Junio C HamanoMay 29, 2025
  41. 0/5 Importing and exporting stashes to refsbrian m. carlson, May 22, 2025
  42. 0/4 Importing and exporting stashes to refsbrian m. carlson, Jun 1, 2025
  43. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, Jun 1, 2025
  44. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, Jun 1, 2025
  45. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, Jun 1, 2025
  46. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, Jun 1, 2025
  47. Phillip WoodJun 5, 2025
  48. Kristoffer HaugsbakkJun 11, 2025
  49. brian m. carlsonJun 11, 2025
  50. Phillip WoodJun 5, 2025
  51. 0/4 Importing and exporting stashes to refsbrian m. carlson, Jun 12, 2025
  52. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, Jun 12, 2025
  53. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, Jun 12, 2025
  54. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, Jun 12, 2025
  55. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, Jun 12, 2025
  56. Phillip WoodJun 25, 2025
  57. Junio C HamanoJun 25, 2025
  58. 1/5 object-name: make get_oid quietly return an errorbrian m. carlson, May 22, 2025
  59. Junio C HamanoMay 22, 2025
  60. 5/5 builtin/stash: provide a way to import stashes from a refbrian m. carlson, May 22, 2025
  61. Junio C HamanoMay 22, 2025
  62. brian m. carlsonMay 26, 2025
  63. Junio C HamanoMay 22, 2025
  64. Phillip WoodMay 23, 2025
  65. 4/5 builtin/stash: provide a way to export stashes to a refbrian m. carlson, May 22, 2025
  66. Junio C HamanoMay 22, 2025
  67. brian m. carlsonMay 26, 2025
  68. Phillip WoodMay 29, 2025
  69. brian m. carlsonMay 22, 2025
  70. Junio C HamanoMay 22, 2025
  71. Kristoffer HaugsbakkJun 11, 2025
  72. brian m. carlsonJun 12, 2025

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.