Skip to content

Commit c0a0af1

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: Phillip Wood <[email protected]> Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d0f1a9 commit c0a0af1

File tree

3 files changed

+268
-0
lines changed

3 files changed

+268
-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: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
N_("git stash create [<message>]")
6262
#define BUILTIN_STASH_EXPORT_USAGE \
6363
N_("git stash export (--print | --to-ref <ref>) [<stash>...]")
64+
#define BUILTIN_STASH_IMPORT_USAGE \
65+
N_("git stash import <commit>")
6466
#define BUILTIN_STASH_CLEAR_USAGE \
6567
"git stash clear"
6668

@@ -77,6 +79,7 @@ static const char * const git_stash_usage[] = {
7779
BUILTIN_STASH_CREATE_USAGE,
7880
BUILTIN_STASH_STORE_USAGE,
7981
BUILTIN_STASH_EXPORT_USAGE,
82+
BUILTIN_STASH_IMPORT_USAGE,
8083
NULL
8184
};
8285

@@ -135,6 +138,10 @@ static const char * const git_stash_export_usage[] = {
135138
NULL
136139
};
137140

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

139146
static const char ref_stash[] = "refs/stash";
140147
static struct strbuf stash_index_path = STRBUF_INIT;
@@ -144,6 +151,7 @@ static struct strbuf stash_index_path = STRBUF_INIT;
144151
* b_commit is set to the base commit
145152
* i_commit is set to the commit containing the index tree
146153
* u_commit is set to the commit containing the untracked files tree
154+
* c_commit is set to the first parent (chain commit) when importing and is otherwise unset
147155
* w_tree is set to the working tree
148156
* b_tree is set to the base tree
149157
* i_tree is set to the index tree
@@ -154,6 +162,7 @@ struct stash_info {
154162
struct object_id b_commit;
155163
struct object_id i_commit;
156164
struct object_id u_commit;
165+
struct object_id c_commit;
157166
struct object_id w_tree;
158167
struct object_id b_tree;
159168
struct object_id i_tree;
@@ -1992,6 +2001,163 @@ static int write_commit_with_parents(struct repository *r,
19922001
return ret;
19932002
}
19942003

2004+
static int do_import_stash(struct repository *r, const char *rev)
2005+
{
2006+
struct object_id chain;
2007+
int res = 0;
2008+
const char *buffer = NULL;
2009+
unsigned long bufsize;
2010+
struct commit *this = NULL;
2011+
struct commit_list *items = NULL, *cur;
2012+
char *msg = NULL;
2013+
2014+
if (repo_get_oid(r, rev, &chain))
2015+
return error(_("not a valid revision: %s"), rev);
2016+
2017+
this = lookup_commit_reference(r, &chain);
2018+
if (!this)
2019+
return error(_("not a commit: %s"), rev);
2020+
2021+
/*
2022+
* Walk the commit history, finding each stash entry, and load data into
2023+
* the array.
2024+
*/
2025+
for (;;) {
2026+
const char *author, *committer;
2027+
size_t author_len, committer_len;
2028+
const char *p;
2029+
const char *expected = "git stash <git@stash> 1000684800 +0000";
2030+
const char *prefix = "git stash: ";
2031+
struct commit *stash;
2032+
struct tree *tree = repo_get_commit_tree(r, this);
2033+
2034+
if (!tree ||
2035+
!oideq(&tree->object.oid, r->hash_algo->empty_tree) ||
2036+
(this->parents &&
2037+
(!this->parents->next || this->parents->next->next))) {
2038+
res = error(_("%s is not a valid exported stash commit"),
2039+
oid_to_hex(&this->object.oid));
2040+
goto out;
2041+
}
2042+
2043+
buffer = repo_get_commit_buffer(r, this, &bufsize);
2044+
2045+
if (!this->parents) {
2046+
/*
2047+
* We don't have any parents. Make sure this is our
2048+
* root commit.
2049+
*/
2050+
author = find_commit_header(buffer, "author", &author_len);
2051+
committer = find_commit_header(buffer, "committer", &committer_len);
2052+
2053+
if (!author || !committer) {
2054+
error(_("cannot parse commit %s"), oid_to_hex(&this->object.oid));
2055+
goto out;
2056+
}
2057+
2058+
if (author_len != strlen(expected) ||
2059+
committer_len != strlen(expected) ||
2060+
memcmp(author, expected, author_len) ||
2061+
memcmp(committer, expected, committer_len)) {
2062+
res = error(_("found root commit %s with invalid data"), oid_to_hex(&this->object.oid));
2063+
goto out;
2064+
}
2065+
break;
2066+
}
2067+
2068+
p = strstr(buffer, "\n\n");
2069+
if (!p) {
2070+
res = error(_("cannot parse commit %s"), oid_to_hex(&this->object.oid));
2071+
goto out;
2072+
}
2073+
2074+
p += 2;
2075+
if (((size_t)(bufsize - (p - buffer)) < strlen(prefix)) ||
2076+
memcmp(prefix, p, strlen(prefix))) {
2077+
res = error(_("found stash commit %s without expected prefix"), oid_to_hex(&this->object.oid));
2078+
goto out;
2079+
}
2080+
2081+
stash = this->parents->next->item;
2082+
2083+
if (repo_parse_commit(r, this->parents->item) ||
2084+
repo_parse_commit(r, stash)) {
2085+
res = error(_("cannot parse parents of commit: %s"),
2086+
oid_to_hex(&this->object.oid));
2087+
goto out;
2088+
}
2089+
2090+
if (check_stash_topology(r, stash)) {
2091+
res = error(_("%s does not look like a stash commit"),
2092+
oid_to_hex(&stash->object.oid));
2093+
goto out;
2094+
}
2095+
2096+
repo_unuse_commit_buffer(r, this, buffer);
2097+
buffer = NULL;
2098+
items = commit_list_insert(stash, &items);
2099+
this = this->parents->item;
2100+
}
2101+
2102+
/*
2103+
* Now, walk each entry, adding it to the stash as a normal stash
2104+
* commit.
2105+
*/
2106+
for (cur = items; cur; cur = cur->next) {
2107+
const char *p;
2108+
struct object_id *oid;
2109+
2110+
this = cur->item;
2111+
oid = &this->object.oid;
2112+
buffer = repo_get_commit_buffer(r, this, &bufsize);
2113+
if (!buffer) {
2114+
res = error(_("cannot read commit buffer for %s"), oid_to_hex(oid));
2115+
goto out;
2116+
}
2117+
2118+
p = strstr(buffer, "\n\n");
2119+
if (!p) {
2120+
res = error(_("cannot parse commit %s"), oid_to_hex(oid));
2121+
goto out;
2122+
}
2123+
2124+
p += 2;
2125+
msg = xmemdupz(p, bufsize - (p - buffer));
2126+
repo_unuse_commit_buffer(r, this, buffer);
2127+
buffer = NULL;
2128+
2129+
if (do_store_stash(oid, msg, 1)) {
2130+
res = error(_("cannot save the stash for %s"), oid_to_hex(oid));
2131+
goto out;
2132+
}
2133+
FREE_AND_NULL(msg);
2134+
}
2135+
out:
2136+
if (this && buffer)
2137+
repo_unuse_commit_buffer(r, this, buffer);
2138+
free_commit_list(items);
2139+
free(msg);
2140+
2141+
return res;
2142+
}
2143+
2144+
static int import_stash(int argc, const char **argv, const char *prefix,
2145+
struct repository *repo)
2146+
{
2147+
struct option options[] = {
2148+
OPT_END()
2149+
};
2150+
2151+
argc = parse_options(argc, argv, prefix, options,
2152+
git_stash_import_usage,
2153+
PARSE_OPT_KEEP_DASHDASH);
2154+
2155+
if (argc != 1)
2156+
usage_msg_opt("a revision is required", git_stash_import_usage, options);
2157+
2158+
return do_import_stash(repo, argv[0]);
2159+
}
2160+
19952161
struct stash_entry_data {
19962162
struct repository *r;
19972163
struct commit_list **items;
@@ -2175,6 +2341,7 @@ int cmd_stash(int argc,
21752341
OPT_SUBCOMMAND("create", &fn, create_stash),
21762342
OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
21772343
OPT_SUBCOMMAND("export", &fn, export_stash),
2344+
OPT_SUBCOMMAND("import", &fn, import_stash),
21782345
OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
21792346
OPT_END()
21802347
};

t/t3903-stash.sh

Lines changed: 94 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,93 @@ 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 refs/heads/bar stash@{1} stash@{0} &&
1467+
git stash clear &&
1468+
git stash import refs/heads/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 refs/heads/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+
git checkout --orphan orphan &&
1493+
git commit-tree $(test_oid empty_tree) -p "$oid" -p "$oid^" -m "" >fake-commit &&
1494+
git update-ref refs/heads/orphan "$(cat fake-commit)" &&
1495+
oid=$(git rev-parse HEAD) &&
1496+
test_must_fail git stash import orphan 2>output &&
1497+
grep "found stash commit $oid without expected prefix" output &&
1498+
test_cmp_rev stash@{0} t-stash0 &&
1499+
1500+
git checkout --orphan orphan2 &&
1501+
git commit-tree $(test_oid empty_tree) -m "" >fake-commit &&
1502+
git update-ref refs/heads/orphan2 "$(cat fake-commit)" &&
1503+
oid=$(git rev-parse HEAD) &&
1504+
test_must_fail git stash import orphan2 2>output &&
1505+
grep "found root commit $oid with invalid data" output &&
1506+
test_cmp_rev stash@{0} t-stash0
1507+
'
1508+
14151509
test_expect_success 'stash apply should succeed with unmodified file' '
14161510
echo base >file &&
14171511
git add file &&

0 commit comments

Comments
 (0)