Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9acde16

Browse files
author
Vicent Martí
committed
Merge pull request #1881 from libgit2/ignore-submodules-in-stash
Never consider submodules for stashing
2 parents dc56fea + ae5a935 commit 9acde16

File tree

5 files changed

+135
-45
lines changed

5 files changed

+135
-45
lines changed

src/stash.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ static int build_workdir_tree(
316316
struct cb_data data = {0};
317317
int error;
318318

319+
opts.flags = GIT_DIFF_IGNORE_SUBMODULES;
320+
319321
if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
320322
goto cleanup;
321323

@@ -474,12 +476,14 @@ static int ensure_there_are_changes_to_stash(
474476
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
475477

476478
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
479+
opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
480+
477481
if (include_untracked_files)
478-
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
482+
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
479483
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
480484

481485
if (include_ignored_files)
482-
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED;
486+
opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED;
483487

484488
error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
485489

tests-clar/stash/save.c

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,15 @@ fatal: Path 'when' exists on disk, but not in 'stash^2'.
113113
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
114114
}
115115

116-
static void assert_status(
117-
const char *path,
118-
int status_flags)
119-
{
120-
unsigned int status;
121-
int error;
122-
123-
error = git_status_file(&status, repo, path);
124-
125-
if (status_flags < 0) {
126-
cl_assert_equal_i(status_flags, error);
127-
return;
128-
}
129-
130-
cl_assert_equal_i(0, error);
131-
cl_assert_equal_i((unsigned int)status_flags, status);
132-
}
133-
134116
void test_stash_save__can_keep_index(void)
135117
{
136118
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_KEEP_INDEX));
137119

138-
assert_status("what", GIT_STATUS_INDEX_MODIFIED);
139-
assert_status("how", GIT_STATUS_INDEX_MODIFIED);
140-
assert_status("who", GIT_STATUS_CURRENT);
141-
assert_status("when", GIT_STATUS_WT_NEW);
142-
assert_status("just.ignore", GIT_STATUS_IGNORED);
120+
assert_status(repo, "what", GIT_STATUS_INDEX_MODIFIED);
121+
assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED);
122+
assert_status(repo, "who", GIT_STATUS_CURRENT);
123+
assert_status(repo, "when", GIT_STATUS_WT_NEW);
124+
assert_status(repo, "just.ignore", GIT_STATUS_IGNORED);
143125
}
144126

145127
static void assert_commit_message_contains(const char *revision, const char *fragment)
@@ -308,25 +290,25 @@ void test_stash_save__can_stage_normal_then_stage_untracked(void)
308290
* 100644 blob b6ed15e81e2593d7bb6265eb4a991d29dc3e628b when
309291
*/
310292

311-
assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
312-
assert_status("how", GIT_STATUS_INDEX_MODIFIED);
313-
assert_status("who", GIT_STATUS_WT_MODIFIED);
314-
assert_status("when", GIT_STATUS_WT_NEW);
315-
assert_status("just.ignore", GIT_STATUS_IGNORED);
293+
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
294+
assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED);
295+
assert_status(repo, "who", GIT_STATUS_WT_MODIFIED);
296+
assert_status(repo, "when", GIT_STATUS_WT_NEW);
297+
assert_status(repo, "just.ignore", GIT_STATUS_IGNORED);
316298

317299
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
318-
assert_status("what", GIT_STATUS_CURRENT);
319-
assert_status("how", GIT_STATUS_CURRENT);
320-
assert_status("who", GIT_STATUS_CURRENT);
321-
assert_status("when", GIT_STATUS_WT_NEW);
322-
assert_status("just.ignore", GIT_STATUS_IGNORED);
300+
assert_status(repo, "what", GIT_STATUS_CURRENT);
301+
assert_status(repo, "how", GIT_STATUS_CURRENT);
302+
assert_status(repo, "who", GIT_STATUS_CURRENT);
303+
assert_status(repo, "when", GIT_STATUS_WT_NEW);
304+
assert_status(repo, "just.ignore", GIT_STATUS_IGNORED);
323305

324306
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
325-
assert_status("what", GIT_STATUS_CURRENT);
326-
assert_status("how", GIT_STATUS_CURRENT);
327-
assert_status("who", GIT_STATUS_CURRENT);
328-
assert_status("when", GIT_ENOTFOUND);
329-
assert_status("just.ignore", GIT_STATUS_IGNORED);
307+
assert_status(repo, "what", GIT_STATUS_CURRENT);
308+
assert_status(repo, "how", GIT_STATUS_CURRENT);
309+
assert_status(repo, "who", GIT_STATUS_CURRENT);
310+
assert_status(repo, "when", GIT_ENOTFOUND);
311+
assert_status(repo, "just.ignore", GIT_STATUS_IGNORED);
330312

331313

332314
assert_blob_oid("stash@{1}^0:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449"); /* see you later */
@@ -360,11 +342,11 @@ void test_stash_save__including_untracked_without_any_untracked_file_creates_an_
360342
{
361343
cl_git_pass(p_unlink("stash/when"));
362344

363-
assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
364-
assert_status("how", GIT_STATUS_INDEX_MODIFIED);
365-
assert_status("who", GIT_STATUS_WT_MODIFIED);
366-
assert_status("when", GIT_ENOTFOUND);
367-
assert_status("just.ignore", GIT_STATUS_IGNORED);
345+
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
346+
assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED);
347+
assert_status(repo, "who", GIT_STATUS_WT_MODIFIED);
348+
assert_status(repo, "when", GIT_ENOTFOUND);
349+
assert_status(repo, "just.ignore", GIT_STATUS_IGNORED);
368350

369351
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
370352

tests-clar/stash/stash_helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@ void setup_stash(git_repository *repo, git_signature *signature)
3535

3636
git_index_free(index);
3737
}
38+
39+
void assert_status(
40+
git_repository *repo,
41+
const char *path,
42+
int status_flags)
43+
{
44+
unsigned int status;
45+
int error;
46+
47+
error = git_status_file(&status, repo, path);
48+
49+
if (status_flags < 0) {
50+
cl_assert_equal_i(status_flags, error);
51+
return;
52+
}
53+
54+
cl_assert_equal_i(0, error);
55+
cl_assert_equal_i((unsigned int)status_flags, status);
56+
}

tests-clar/stash/stash_helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
void setup_stash(
22
git_repository *repo,
33
git_signature *signature);
4+
5+
void assert_status(
6+
git_repository *repo,
7+
const char *path,
8+
int status_flags);

tests-clar/stash/submodules.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "clar_libgit2.h"
2+
#include "stash_helpers.h"
3+
#include "../submodule/submodule_helpers.h"
4+
5+
static git_repository *repo;
6+
static git_signature *signature;
7+
static git_oid stash_tip_oid;
8+
9+
static git_submodule *sm;
10+
11+
void test_stash_submodules__initialize(void)
12+
{
13+
cl_git_pass(git_signature_new(&signature, "nulltoken", "[email protected]", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
14+
15+
repo = setup_fixture_submodules();
16+
17+
cl_git_pass(git_submodule_lookup(&sm, repo, "testrepo"));
18+
}
19+
20+
void test_stash_submodules__cleanup(void)
21+
{
22+
git_signature_free(signature);
23+
signature = NULL;
24+
}
25+
26+
void test_stash_submodules__does_not_stash_modified_submodules(void)
27+
{
28+
static git_index *smindex;
29+
static git_repository *smrepo;
30+
31+
assert_status(repo, "modified", GIT_STATUS_WT_MODIFIED);
32+
33+
/* modify file in submodule */
34+
cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
35+
assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED);
36+
37+
/* add file to index in submodule */
38+
cl_git_pass(git_submodule_open(&smrepo, sm));
39+
cl_git_pass(git_repository_index(&smindex, smrepo));
40+
cl_git_pass(git_index_add_bypath(smindex, "README"));
41+
42+
/* commit changed index of submodule */
43+
cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Modify it");
44+
assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED);
45+
46+
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
47+
48+
assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED);
49+
assert_status(repo, "modified", GIT_STATUS_CURRENT);
50+
51+
git_index_free(smindex);
52+
git_repository_free(smrepo);
53+
}
54+
55+
void test_stash_submodules__stash_is_empty_with_modified_submodules(void)
56+
{
57+
static git_index *smindex;
58+
static git_repository *smrepo;
59+
60+
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
61+
assert_status(repo, "modified", GIT_STATUS_CURRENT);
62+
63+
/* modify file in submodule */
64+
cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
65+
assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED);
66+
67+
/* add file to index in submodule */
68+
cl_git_pass(git_submodule_open(&smrepo, sm));
69+
cl_git_pass(git_repository_index(&smindex, smrepo));
70+
cl_git_pass(git_index_add_bypath(smindex, "README"));
71+
72+
/* commit changed index of submodule */
73+
cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Modify it");
74+
assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED);
75+
76+
cl_git_fail_with(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT), GIT_ENOTFOUND);
77+
78+
git_index_free(smindex);
79+
git_repository_free(smrepo);
80+
}

0 commit comments

Comments
 (0)