Skip to content

Commit a2b3009

Browse files
bk2204gitster
authored andcommitted
builtin/stash: provide a way to import stashes from a ref
Now that we have a way to export stashes to a ref, let's provide a way to import them from such a ref back to the stash. This works much the way the export code does, except that we strip off the first parent chain commit and then store each resulting commit back to the stash. We don't clear the stash first and instead add the specified stashes to the top of the stash. This is because users may want to export just a few stashes, such as to share a small amount of work in progress with a colleague, and it would be undesirable for the receiving user to lose all of their data. For users who do want to replace the stash, it's easy to do to: simply run "git stash clear" first. We specifically rely on the fact that we'll produce identical stash commits on both sides in our tests. This provides a cheap, straightforward check for our tests and also makes it easy for users to see if they already have the same data in both repositories. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea96357 commit a2b3009

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

Documentation/git-stash.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SYNOPSIS
2424
'git stash' create [<message>]
2525
'git stash' store [(-m | --message) <message>] [-q | --quiet] <commit>
2626
'git stash' export (--print | --to-ref <ref>) [<stash>...]
27+
'git stash' import <commit>
2728

2829
DESCRIPTION
2930
-----------
@@ -161,6 +162,12 @@ export ( --print | --to-ref <ref> ) [<stash>...]::
161162
a chain of commits which can be transferred using the normal fetch and
162163
push mechanisms, then imported using the `import` subcommand.
163164

165+
import <commit>::
166+
167+
Import the specified stashes from the specified commit, which must have been
168+
created by `export`, and add them to the list of stashes. To replace the
169+
existing stashes, use `clear` first.
170+
164171
OPTIONS
165172
-------
166173
-a::

builtin/stash.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
N_("git stash create [<message>]")
6161
#define BUILTIN_STASH_EXPORT_USAGE \
6262
N_("git stash export (--print | --to-ref <ref>) [<stash>...]")
63+
#define BUILTIN_STASH_IMPORT_USAGE \
64+
N_("git stash import <commit>")
6365
#define BUILTIN_STASH_CLEAR_USAGE \
6466
"git stash clear"
6567

@@ -76,6 +78,7 @@ static const char * const git_stash_usage[] = {
7678
BUILTIN_STASH_CREATE_USAGE,
7779
BUILTIN_STASH_STORE_USAGE,
7880
BUILTIN_STASH_EXPORT_USAGE,
81+
BUILTIN_STASH_IMPORT_USAGE,
7982
NULL
8083
};
8184

@@ -134,6 +137,10 @@ static const char * const git_stash_export_usage[] = {
134137
NULL
135138
};
136139

140+
static const char * const git_stash_import_usage[] = {
141+
BUILTIN_STASH_IMPORT_USAGE,
142+
NULL
143+
};
137144

138145
static const char ref_stash[] = "refs/stash";
139146
static struct strbuf stash_index_path = STRBUF_INIT;
@@ -143,6 +150,7 @@ static struct strbuf stash_index_path = STRBUF_INIT;
143150
* b_commit is set to the base commit
144151
* i_commit is set to the commit containing the index tree
145152
* u_commit is set to the commit containing the untracked files tree
153+
* c_commit is set to the first parent (chain commit) when importing and is otherwise unset
146154
* w_tree is set to the working tree
147155
* b_tree is set to the base tree
148156
* i_tree is set to the index tree
@@ -153,6 +161,7 @@ struct stash_info {
153161
struct object_id b_commit;
154162
struct object_id i_commit;
155163
struct object_id u_commit;
164+
struct object_id c_commit;
156165
struct object_id w_tree;
157166
struct object_id b_tree;
158167
struct object_id i_tree;
@@ -1962,6 +1971,99 @@ static int write_commit_with_parents(struct repository *r,
19621971
return ret;
19631972
}
19641973

1974+
static int do_import_stash(struct repository *r, const char *rev)
1975+
{
1976+
struct object_id chain;
1977+
struct oid_array items = OID_ARRAY_INIT;
1978+
int res = 0;
1979+
int i;
1980+
const char *buffer = NULL;
1981+
struct commit *this = NULL;
1982+
char *msg = NULL;
1983+
1984+
if (repo_get_oid(r, rev, &chain))
1985+
return error(_("not a valid revision: %s"), rev);
1986+
1987+
/*
1988+
* Walk the commit history, finding each stash entry, and load data into
1989+
* the array.
1990+
*/
1991+
for (i = 0;; i++) {
1992+
struct object_id tree, oid;
1993+
char revision[GIT_MAX_HEXSZ + 1];
1994+
1995+
oid_to_hex_r(revision, &chain);
1996+
1997+
if (get_oidf(&tree, "%s:", revision) ||
1998+
!oideq(&tree, r->hash_algo->empty_tree)) {
1999+
res = error(_("%s is not a valid exported stash commit"), revision);
2000+
goto out;
2001+
}
2002+
if (get_oidf(&chain, "%s^1", revision) ||
2003+
get_oidf(&oid, "%s^2", revision))
2004+
break;
2005+
oid_array_append(&items, &oid);
2006+
}
2007+
2008+
/*
2009+
* Now, walk each entry, adding it to the stash as a normal stash
2010+
* commit.
2011+
*/
2012+
for (i = items.nr - 1; i >= 0; i--) {
2013+
unsigned long bufsize;
2014+
const char *p;
2015+
const struct object_id *oid = items.oid + i;
2016+
2017+
this = lookup_commit_reference(r, oid);
2018+
buffer = repo_get_commit_buffer(r, this, &bufsize);
2019+
if (!buffer) {
2020+
res = error(_("cannot read commit buffer for %s"), oid_to_hex(oid));
2021+
goto out;
2022+
}
2023+
2024+
p = strstr(buffer, "\n\n");
2025+
if (!p) {
2026+
res = error(_("cannot parse commit %s"), oid_to_hex(oid));
2027+
goto out;
2028+
}
2029+
2030+
p += 2;
2031+
msg = xmemdupz(p, bufsize - (p - buffer));
2032+
repo_unuse_commit_buffer(r, this, buffer);
2033+
buffer = NULL;
2034+
2035+
if (do_store_stash(oid, msg, 1)) {
2036+
res = error(_("cannot save the stash for %s"), oid_to_hex(oid));
2037+
goto out;
2038+
}
2039+
FREE_AND_NULL(msg);
2040+
}
2041+
out:
2042+
if (this && buffer)
2043+
repo_unuse_commit_buffer(r, this, buffer);
2044+
oid_array_clear(&items);
2045+
free(msg);
2046+
2047+
return res;
2048+
}
2049+
2050+
static int import_stash(int argc, const char **argv, const char *prefix,
2051+
struct repository *repo)
2052+
{
2053+
struct option options[] = {
2054+
OPT_END()
2055+
};
2056+
2057+
argc = parse_options(argc, argv, prefix, options,
2058+
git_stash_import_usage,
2059+
PARSE_OPT_KEEP_DASHDASH);
2060+
2061+
if (argc != 1)
2062+
usage_msg_opt("a revision is required", git_stash_import_usage, options);
2063+
2064+
return do_import_stash(repo, argv[0]);
2065+
}
2066+
19652067
static int do_export_stash(struct repository *r,
19662068
const char *ref,
19672069
int argc,
@@ -2118,6 +2220,7 @@ int cmd_stash(int argc,
21182220
OPT_SUBCOMMAND("create", &fn, create_stash),
21192221
OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
21202222
OPT_SUBCOMMAND("export", &fn, export_stash),
2223+
OPT_SUBCOMMAND("import", &fn, import_stash),
21212224
OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
21222225
OPT_END()
21232226
};

t/t3903-stash.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1111
. ./test-lib.sh
1212
. "$TEST_DIRECTORY"/lib-unique-files.sh
1313

14+
test_expect_success 'setup' '
15+
test_oid_cache <<-EOF
16+
export_base sha1:73c9bab443d1f88ac61aa533d2eeaaa15451239c
17+
export_base sha256:f210fa6346e3e2ce047bdb570426b17075980c1ac01fec8fc4b75bd3ab4bcfe4
18+
EOF
19+
'
20+
1421
test_expect_success 'usage on cmd and subcommand invalid option' '
1522
test_expect_code 129 git stash --invalid-option 2>usage &&
1623
grep "or: git stash" usage &&
@@ -1412,6 +1419,77 @@ test_expect_success 'stash --keep-index --include-untracked with empty tree' '
14121419
)
14131420
'
14141421

1422+
test_expect_success 'stash export and import round-trip stashes' '
1423+
git reset &&
1424+
>untracked &&
1425+
>tracked1 &&
1426+
>tracked2 &&
1427+
git add tracked* &&
1428+
git stash -- &&
1429+
>subdir/untracked &&
1430+
>subdir/tracked1 &&
1431+
>subdir/tracked2 &&
1432+
git add subdir/tracked* &&
1433+
git stash --include-untracked -- subdir/ &&
1434+
git tag t-stash0 stash@{0} &&
1435+
git tag t-stash1 stash@{1} &&
1436+
simple=$(git stash export --print) &&
1437+
git stash clear &&
1438+
git stash import "$simple" &&
1439+
test_cmp_rev stash@{0} t-stash0 &&
1440+
test_cmp_rev stash@{1} t-stash1 &&
1441+
git stash export --to-ref refs/heads/foo &&
1442+
test_cmp_rev "$(test_oid empty_tree)" foo: &&
1443+
test_cmp_rev "$(test_oid empty_tree)" foo^: &&
1444+
test_cmp_rev t-stash0 foo^2 &&
1445+
test_cmp_rev t-stash1 foo^^2 &&
1446+
git log --first-parent --format="%s" refs/heads/foo >log &&
1447+
grep "^git stash: " log >log2 &&
1448+
test_line_count = 13 log2 &&
1449+
git stash clear &&
1450+
git stash import foo &&
1451+
test_cmp_rev stash@{0} t-stash0 &&
1452+
test_cmp_rev stash@{1} t-stash1
1453+
'
1454+
1455+
test_expect_success 'stash import appends commits' '
1456+
git log --format=oneline -g refs/stash >out &&
1457+
cat out out >out2 &&
1458+
git stash import refs/heads/foo &&
1459+
git log --format=oneline -g refs/stash >actual &&
1460+
test_line_count = $(wc -l <out2) actual
1461+
'
1462+
1463+
test_expect_success 'stash export can accept specified stashes' '
1464+
git stash clear &&
1465+
git stash import foo &&
1466+
git stash export --to-ref bar stash@{1} stash@{0} &&
1467+
git stash clear &&
1468+
git stash import bar &&
1469+
test_cmp_rev stash@{1} t-stash0 &&
1470+
test_cmp_rev stash@{0} t-stash1 &&
1471+
git log --format=oneline -g refs/stash >actual &&
1472+
test_line_count = 2 actual
1473+
'
1474+
1475+
test_expect_success 'stash can import and export zero stashes' '
1476+
git stash clear &&
1477+
git stash export --to-ref baz &&
1478+
test_cmp_rev "$(test_oid empty_tree)" baz: &&
1479+
test_cmp_rev "$(test_oid export_base)" baz &&
1480+
test_must_fail git rev-parse baz^1 &&
1481+
git stash import baz &&
1482+
test_must_fail git rev-parse refs/stash
1483+
'
1484+
1485+
test_expect_success 'stash rejects invalid attempts to import commits' '
1486+
git stash import foo &&
1487+
test_must_fail git stash import HEAD 2>output &&
1488+
oid=$(git rev-parse HEAD) &&
1489+
grep "$oid is not a valid exported stash commit" output &&
1490+
test_cmp_rev stash@{0} t-stash0
1491+
'
1492+
14151493
test_expect_success 'stash apply should succeed with unmodified file' '
14161494
echo base >file &&
14171495
git add file &&

0 commit comments

Comments
 (0)