mbox series

[v4,0/4] Importing and exporting stashes to refs

Message ID 20220407215352.3491567-1-sandals@crustytoothpaste.net (mailing list archive)
Headers show
Series Importing and exporting stashes to refs | expand

Message

brian m. carlson April 7, 2022, 9:53 p.m. UTC
Stashes are currently stored using the reflog in a given repository.
This is an interesting and novel way to handle them, but there is no way
to easily move a stash across machines.  For example, stashes cannot be
bundled, pushed, or fetched.

Let's solve this problem by allowing users to import and export stashes
to a chain of commits.  The commits used in a stash export contain two
parents: one which is the pointer to the next exported stash (or to an
empty commit with no parents if there are no more) and the second is the
stash commit that would normally be stored in the reflog.

Changes from v3:
* Fix strbuf handling to avoid leaks and generally be more sensible.
* Make use of the error return code more often.
* Use oid_array.
* Tidy various parts of the code and fix long lines.
* Simplify tests using git tag.
* Shorten and tidy tests.
* Add an additional test covering the base commit OID and importing and
  exporting empty stashes.

Changes from v2:
* Fix uninitialized strbuf.
* Avoid C99-style initializations.

Changes from v1:
* Change storage format as suggested by Junio.
* Rename to GIT_OID_GENTLY.
* Remove unnecessary initializations.
* Use ALLOC_GROW_BY.
* Ensure completely reproducible exports.
* Avoid size_t.
* Various other code cleanups.

brian m. carlson (4):
  object-name: make get_oid quietly return an error
  builtin/stash: factor out revision parsing into a function
  builtin/stash: provide a way to export stashes to a ref
  builtin/stash: provide a way to import stashes from a ref

 Documentation/git-stash.txt |  29 +++-
 builtin/stash.c             | 321 ++++++++++++++++++++++++++++++++++--
 cache.h                     |   1 +
 object-name.c               |   6 +-
 t/t3903-stash.sh            |  63 +++++++
 5 files changed, 406 insertions(+), 14 deletions(-)

Comments

Ævar Arnfjörð Bjarmason April 13, 2022, 3:25 p.m. UTC | #1
On Thu, Apr 07 2022, brian m. carlson wrote:

> * Use oid_array.

It looks like only 1/2 of the functions using the manual
not-quite-an-oid_array were converted. Here's a diff to squash for the
other one:

diff --git a/builtin/stash.c b/builtin/stash.c
index e4b99188836..7d5493581e6 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1925,8 +1925,7 @@ static int do_export_stash(const char *ref, int argc, const char **argv)
 	struct object_id base;
 	struct object_context unused;
 	struct commit *prev;
-	struct object_id *items = NULL;
-	int nitems = 0, nalloc = 0;
+	struct oid_array items = OID_ARRAY_INIT;
 	int res = 0;
 	int i;
 	struct strbuf revision = STRBUF_INIT;
@@ -1954,14 +1953,16 @@ static int do_export_stash(const char *ref, int argc, const char **argv)
 		 * Find each specified stash, and load data into the array.
 		 */
 		for (i = 0; i < argc; i++) {
-			ALLOC_GROW_BY(items, nitems, 1, nalloc);
+			struct object_id oid;
+
 			if (parse_revision(&revision, argv[i], 1) ||
 			    get_oid_with_context(the_repository, revision.buf,
 						 GET_OID_QUIETLY | GET_OID_GENTLY,
-						 &items[i], &unused)) {
+						 &oid, &unused)) {
 				res = error(_("unable to find stash entry %s"), argv[i]);
 				goto out;
 			}
+			oid_array_append(&items, &oid);
 		}
 	} else {
 		/*
@@ -1978,8 +1979,7 @@ static int do_export_stash(const char *ref, int argc, const char **argv)
 						 GET_OID_QUIETLY | GET_OID_GENTLY,
 						 &oid, &unused))
 				break;
-			ALLOC_GROW_BY(items, nitems, 1, nalloc);
-			oidcpy(&items[i], &oid);
+			oid_array_append(&items, &oid);
 		}
 	}
 
@@ -1988,14 +1988,15 @@ static int do_export_stash(const char *ref, int argc, const char **argv)
 	 * but where their first parents form a chain to our original empty
 	 * base commit.
 	 */
-	for (i = nitems - 1; i >= 0; i--) {
+	for (i = items.nr - 1; i >= 0; i--) {
 		struct commit_list *parents = NULL;
 		struct commit_list **next = &parents;
 		struct object_id out;
+		const struct object_id *oid = items.oid + i;
 
 		next = commit_list_append(prev, next);
-		next = commit_list_append(lookup_commit_reference(the_repository, &items[i]), next);
-		res = write_commit_with_parents(&out, &items[i], parents);
+		next = commit_list_append(lookup_commit_reference(the_repository, oid), next);
+		res = write_commit_with_parents(&out, oid, parents);
 		if (res)
 			goto out;
 		prev = lookup_commit_reference(the_repository, &out);
@@ -2006,7 +2007,7 @@ static int do_export_stash(const char *ref, int argc, const char **argv)
 		puts(oid_to_hex(&prev->object.oid));
 out:
 	strbuf_release(&revision);
-	free(items);
+	oid_array_clear(&items);
 
 	return res;
 }