Skip to content

Commit 0ba1ba4

Browse files
committed
Merge branch 'js/stash-in-c-pathspec-fix'
Further fixes to "git stash" reimplemented in C. * js/stash-in-c-pathspec-fix: stash: pass pathspec as pointer built-in stash: handle :(glob) pathspecs again legacy stash: fix "rudimentary backport of -q"
2 parents 646becd + 7db9302 commit 0ba1ba4

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

builtin/stash.c

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -828,11 +828,11 @@ static int store_stash(int argc, const char **argv, const char *prefix)
828828
}
829829

830830
static void add_pathspecs(struct argv_array *args,
831-
struct pathspec ps) {
831+
const struct pathspec *ps) {
832832
int i;
833833

834-
for (i = 0; i < ps.nr; i++)
835-
argv_array_push(args, ps.items[i].match);
834+
for (i = 0; i < ps->nr; i++)
835+
argv_array_push(args, ps->items[i].original);
836836
}
837837

838838
/*
@@ -842,7 +842,7 @@ static void add_pathspecs(struct argv_array *args,
842842
* = 0 if there are not any untracked files
843843
* > 0 if there are untracked files
844844
*/
845-
static int get_untracked_files(struct pathspec ps, int include_untracked,
845+
static int get_untracked_files(const struct pathspec *ps, int include_untracked,
846846
struct strbuf *untracked_files)
847847
{
848848
int i;
@@ -855,12 +855,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
855855
if (include_untracked != INCLUDE_ALL_FILES)
856856
setup_standard_excludes(&dir);
857857

858-
seen = xcalloc(ps.nr, 1);
858+
seen = xcalloc(ps->nr, 1);
859859

860-
max_len = fill_directory(&dir, the_repository->index, &ps);
860+
max_len = fill_directory(&dir, the_repository->index, ps);
861861
for (i = 0; i < dir.nr; i++) {
862862
struct dir_entry *ent = dir.entries[i];
863-
if (dir_path_match(&the_index, ent, &ps, max_len, seen)) {
863+
if (dir_path_match(&the_index, ent, ps, max_len, seen)) {
864864
found++;
865865
strbuf_addstr(untracked_files, ent->name);
866866
/* NUL-terminate: will be fed to update-index -z */
@@ -883,11 +883,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
883883
* = 0 if there are no changes.
884884
* > 0 if there are changes.
885885
*/
886-
static int check_changes_tracked_files(struct pathspec ps)
886+
static int check_changes_tracked_files(const struct pathspec *ps)
887887
{
888888
int result;
889889
struct rev_info rev;
890890
struct object_id dummy;
891+
int ret = 0;
891892

892893
/* No initial commit. */
893894
if (get_oid("HEAD", &dummy))
@@ -897,7 +898,7 @@ static int check_changes_tracked_files(struct pathspec ps)
897898
return -1;
898899

899900
init_revisions(&rev, NULL);
900-
rev.prune_data = ps;
901+
copy_pathspec(&rev.prune_data, ps);
901902

902903
rev.diffopt.flags.quick = 1;
903904
rev.diffopt.flags.ignore_submodules = 1;
@@ -907,22 +908,28 @@ static int check_changes_tracked_files(struct pathspec ps)
907908
diff_setup_done(&rev.diffopt);
908909

909910
result = run_diff_index(&rev, 1);
910-
if (diff_result_code(&rev.diffopt, result))
911-
return 1;
911+
if (diff_result_code(&rev.diffopt, result)) {
912+
ret = 1;
913+
goto done;
914+
}
912915

913916
object_array_clear(&rev.pending);
914917
result = run_diff_files(&rev, 0);
915-
if (diff_result_code(&rev.diffopt, result))
916-
return 1;
918+
if (diff_result_code(&rev.diffopt, result)) {
919+
ret = 1;
920+
goto done;
921+
}
917922

918-
return 0;
923+
done:
924+
clear_pathspec(&rev.prune_data);
925+
return ret;
919926
}
920927

921928
/*
922929
* The function will fill `untracked_files` with the names of untracked files
923930
* It will return 1 if there were any changes and 0 if there were not.
924931
*/
925-
static int check_changes(struct pathspec ps, int include_untracked,
932+
static int check_changes(const struct pathspec *ps, int include_untracked,
926933
struct strbuf *untracked_files)
927934
{
928935
int ret = 0;
@@ -976,7 +983,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
976983
return ret;
977984
}
978985

979-
static int stash_patch(struct stash_info *info, struct pathspec ps,
986+
static int stash_patch(struct stash_info *info, const struct pathspec *ps,
980987
struct strbuf *out_patch, int quiet)
981988
{
982989
int ret = 0;
@@ -1035,7 +1042,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10351042
return ret;
10361043
}
10371044

1038-
static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1045+
static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
10391046
{
10401047
int ret = 0;
10411048
struct rev_info rev;
@@ -1044,6 +1051,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10441051
struct index_state istate = { NULL };
10451052

10461053
init_revisions(&rev, NULL);
1054+
copy_pathspec(&rev.prune_data, ps);
10471055

10481056
set_alternate_index_output(stash_index_path.buf);
10491057
if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1052,7 +1060,6 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10521060
}
10531061
set_alternate_index_output(NULL);
10541062

1055-
rev.prune_data = ps;
10561063
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
10571064
rev.diffopt.format_callback = add_diff_to_buf;
10581065
rev.diffopt.format_callback_data = &diff_output;
@@ -1091,12 +1098,13 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10911098
discard_index(&istate);
10921099
UNLEAK(rev);
10931100
object_array_clear(&rev.pending);
1101+
clear_pathspec(&rev.prune_data);
10941102
strbuf_release(&diff_output);
10951103
remove_path(stash_index_path.buf);
10961104
return ret;
10971105
}
10981106

1099-
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1107+
static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
11001108
int include_untracked, int patch_mode,
11011109
struct stash_info *info, struct strbuf *patch,
11021110
int quiet)
@@ -1228,10 +1236,10 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12281236
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
12291237

12301238
memset(&ps, 0, sizeof(ps));
1231-
if (!check_changes_tracked_files(ps))
1239+
if (!check_changes_tracked_files(&ps))
12321240
return 0;
12331241

1234-
ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1242+
ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
12351243
NULL, 0);
12361244
if (!ret)
12371245
printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1240,7 +1248,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12401248
return ret;
12411249
}
12421250

1243-
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1251+
static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
12441252
int keep_index, int patch_mode, int include_untracked)
12451253
{
12461254
int ret = 0;
@@ -1260,15 +1268,15 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12601268
}
12611269

12621270
read_cache_preload(NULL);
1263-
if (!include_untracked && ps.nr) {
1271+
if (!include_untracked && ps->nr) {
12641272
int i;
1265-
char *ps_matched = xcalloc(ps.nr, 1);
1273+
char *ps_matched = xcalloc(ps->nr, 1);
12661274

12671275
for (i = 0; i < active_nr; i++)
1268-
ce_path_match(&the_index, active_cache[i], &ps,
1276+
ce_path_match(&the_index, active_cache[i], ps,
12691277
ps_matched);
12701278

1271-
if (report_path_error(ps_matched, &ps, NULL)) {
1279+
if (report_path_error(ps_matched, ps, NULL)) {
12721280
fprintf_ln(stderr, _("Did you forget to 'git add'?"));
12731281
ret = -1;
12741282
free(ps_matched);
@@ -1315,7 +1323,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13151323
stash_msg_buf.buf);
13161324

13171325
if (!patch_mode) {
1318-
if (include_untracked && !ps.nr) {
1326+
if (include_untracked && !ps->nr) {
13191327
struct child_process cp = CHILD_PROCESS_INIT;
13201328

13211329
cp.git_cmd = 1;
@@ -1329,7 +1337,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13291337
}
13301338
}
13311339
discard_cache();
1332-
if (ps.nr) {
1340+
if (ps->nr) {
13331341
struct child_process cp_add = CHILD_PROCESS_INIT;
13341342
struct child_process cp_diff = CHILD_PROCESS_INIT;
13351343
struct child_process cp_apply = CHILD_PROCESS_INIT;
@@ -1468,8 +1476,9 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14681476
git_stash_push_usage,
14691477
0);
14701478

1471-
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1472-
return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1479+
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1480+
prefix, argv);
1481+
return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
14731482
include_untracked);
14741483
}
14751484

@@ -1506,7 +1515,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15061515
stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
15071516

15081517
memset(&ps, 0, sizeof(ps));
1509-
ret = do_push_stash(ps, stash_msg, quiet, keep_index,
1518+
ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
15101519
patch_mode, include_untracked);
15111520

15121521
strbuf_release(&stash_msg_buf);

git-legacy-stash.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ maybe_quiet () {
8686
shift
8787
if test -n "$GIT_QUIET"
8888
then
89-
eval "$@" 2>/dev/null
89+
"$@" 2>/dev/null
9090
else
91-
eval "$@"
91+
"$@"
9292
fi
9393
;;
9494
*)
9595
if test -n "$GIT_QUIET"
9696
then
97-
eval "$@" >/dev/null 2>&1
97+
"$@" >/dev/null 2>&1
9898
else
99-
eval "$@"
99+
"$@"
100100
fi
101101
;;
102102
esac

t/t3905-stash-include-untracked.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,10 @@ test_expect_success 'stash -u -- <non-existant> shows no changes when there are
283283
test_i18ncmp expect actual
284284
'
285285

286+
test_expect_success 'stash -u with globs' '
287+
>untracked.txt &&
288+
git stash -u -- ":(glob)**/*.txt" &&
289+
test_path_is_missing untracked.txt
290+
'
291+
286292
test_done

0 commit comments

Comments
 (0)