Skip to content

Commit 7db9302

Browse files
tgummerergitster
authored andcommitted
stash: pass pathspec as pointer
Passing the pathspec by value is potentially confusing, as the copy is only a shallow copy, so save the overhead of the copy, and pass the pathspec struct as a pointer. In addition use copy_pathspec to copy the pathspec into rev.prune_data, so the copy is a proper deep copy, and owned by the revision API, as that's what the API expects. Reported-by: Jeff King <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1366c78 commit 7db9302

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

builtin/stash.c

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,11 @@ static int store_stash(int argc, const char **argv, const char *prefix)
826826
}
827827

828828
static void add_pathspecs(struct argv_array *args,
829-
struct pathspec ps) {
829+
const struct pathspec *ps) {
830830
int i;
831831

832-
for (i = 0; i < ps.nr; i++)
833-
argv_array_push(args, ps.items[i].original);
832+
for (i = 0; i < ps->nr; i++)
833+
argv_array_push(args, ps->items[i].original);
834834
}
835835

836836
/*
@@ -840,7 +840,7 @@ static void add_pathspecs(struct argv_array *args,
840840
* = 0 if there are not any untracked files
841841
* > 0 if there are untracked files
842842
*/
843-
static int get_untracked_files(struct pathspec ps, int include_untracked,
843+
static int get_untracked_files(const struct pathspec *ps, int include_untracked,
844844
struct strbuf *untracked_files)
845845
{
846846
int i;
@@ -853,12 +853,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
853853
if (include_untracked != INCLUDE_ALL_FILES)
854854
setup_standard_excludes(&dir);
855855

856-
seen = xcalloc(ps.nr, 1);
856+
seen = xcalloc(ps->nr, 1);
857857

858-
max_len = fill_directory(&dir, the_repository->index, &ps);
858+
max_len = fill_directory(&dir, the_repository->index, ps);
859859
for (i = 0; i < dir.nr; i++) {
860860
struct dir_entry *ent = dir.entries[i];
861-
if (dir_path_match(&the_index, ent, &ps, max_len, seen)) {
861+
if (dir_path_match(&the_index, ent, ps, max_len, seen)) {
862862
found++;
863863
strbuf_addstr(untracked_files, ent->name);
864864
/* NUL-terminate: will be fed to update-index -z */
@@ -881,11 +881,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
881881
* = 0 if there are no changes.
882882
* > 0 if there are changes.
883883
*/
884-
static int check_changes_tracked_files(struct pathspec ps)
884+
static int check_changes_tracked_files(const struct pathspec *ps)
885885
{
886886
int result;
887887
struct rev_info rev;
888888
struct object_id dummy;
889+
int ret = 0;
889890

890891
/* No initial commit. */
891892
if (get_oid("HEAD", &dummy))
@@ -895,7 +896,7 @@ static int check_changes_tracked_files(struct pathspec ps)
895896
return -1;
896897

897898
init_revisions(&rev, NULL);
898-
rev.prune_data = ps;
899+
copy_pathspec(&rev.prune_data, ps);
899900

900901
rev.diffopt.flags.quick = 1;
901902
rev.diffopt.flags.ignore_submodules = 1;
@@ -905,22 +906,28 @@ static int check_changes_tracked_files(struct pathspec ps)
905906
diff_setup_done(&rev.diffopt);
906907

907908
result = run_diff_index(&rev, 1);
908-
if (diff_result_code(&rev.diffopt, result))
909-
return 1;
909+
if (diff_result_code(&rev.diffopt, result)) {
910+
ret = 1;
911+
goto done;
912+
}
910913

911914
object_array_clear(&rev.pending);
912915
result = run_diff_files(&rev, 0);
913-
if (diff_result_code(&rev.diffopt, result))
914-
return 1;
916+
if (diff_result_code(&rev.diffopt, result)) {
917+
ret = 1;
918+
goto done;
919+
}
915920

916-
return 0;
921+
done:
922+
clear_pathspec(&rev.prune_data);
923+
return ret;
917924
}
918925

919926
/*
920927
* The function will fill `untracked_files` with the names of untracked files
921928
* It will return 1 if there were any changes and 0 if there were not.
922929
*/
923-
static int check_changes(struct pathspec ps, int include_untracked,
930+
static int check_changes(const struct pathspec *ps, int include_untracked,
924931
struct strbuf *untracked_files)
925932
{
926933
int ret = 0;
@@ -974,7 +981,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
974981
return ret;
975982
}
976983

977-
static int stash_patch(struct stash_info *info, struct pathspec ps,
984+
static int stash_patch(struct stash_info *info, const struct pathspec *ps,
978985
struct strbuf *out_patch, int quiet)
979986
{
980987
int ret = 0;
@@ -1033,7 +1040,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10331040
return ret;
10341041
}
10351042

1036-
static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1043+
static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
10371044
{
10381045
int ret = 0;
10391046
struct rev_info rev;
@@ -1042,6 +1049,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10421049
struct index_state istate = { NULL };
10431050

10441051
init_revisions(&rev, NULL);
1052+
copy_pathspec(&rev.prune_data, ps);
10451053

10461054
set_alternate_index_output(stash_index_path.buf);
10471055
if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1050,7 +1058,6 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10501058
}
10511059
set_alternate_index_output(NULL);
10521060

1053-
rev.prune_data = ps;
10541061
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
10551062
rev.diffopt.format_callback = add_diff_to_buf;
10561063
rev.diffopt.format_callback_data = &diff_output;
@@ -1089,12 +1096,13 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10891096
discard_index(&istate);
10901097
UNLEAK(rev);
10911098
object_array_clear(&rev.pending);
1099+
clear_pathspec(&rev.prune_data);
10921100
strbuf_release(&diff_output);
10931101
remove_path(stash_index_path.buf);
10941102
return ret;
10951103
}
10961104

1097-
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1105+
static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
10981106
int include_untracked, int patch_mode,
10991107
struct stash_info *info, struct strbuf *patch,
11001108
int quiet)
@@ -1226,10 +1234,10 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12261234
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
12271235

12281236
memset(&ps, 0, sizeof(ps));
1229-
if (!check_changes_tracked_files(ps))
1237+
if (!check_changes_tracked_files(&ps))
12301238
return 0;
12311239

1232-
ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1240+
ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
12331241
NULL, 0);
12341242
if (!ret)
12351243
printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1238,7 +1246,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12381246
return ret;
12391247
}
12401248

1241-
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1249+
static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
12421250
int keep_index, int patch_mode, int include_untracked)
12431251
{
12441252
int ret = 0;
@@ -1258,15 +1266,15 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12581266
}
12591267

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

12651273
for (i = 0; i < active_nr; i++)
1266-
ce_path_match(&the_index, active_cache[i], &ps,
1274+
ce_path_match(&the_index, active_cache[i], ps,
12671275
ps_matched);
12681276

1269-
if (report_path_error(ps_matched, &ps, NULL)) {
1277+
if (report_path_error(ps_matched, ps, NULL)) {
12701278
fprintf_ln(stderr, _("Did you forget to 'git add'?"));
12711279
ret = -1;
12721280
free(ps_matched);
@@ -1313,7 +1321,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13131321
stash_msg_buf.buf);
13141322

13151323
if (!patch_mode) {
1316-
if (include_untracked && !ps.nr) {
1324+
if (include_untracked && !ps->nr) {
13171325
struct child_process cp = CHILD_PROCESS_INIT;
13181326

13191327
cp.git_cmd = 1;
@@ -1327,7 +1335,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13271335
}
13281336
}
13291337
discard_cache();
1330-
if (ps.nr) {
1338+
if (ps->nr) {
13311339
struct child_process cp_add = CHILD_PROCESS_INIT;
13321340
struct child_process cp_diff = CHILD_PROCESS_INIT;
13331341
struct child_process cp_apply = CHILD_PROCESS_INIT;
@@ -1468,7 +1476,7 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14681476

14691477
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
14701478
prefix, argv);
1471-
return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1479+
return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
14721480
include_untracked);
14731481
}
14741482

@@ -1505,7 +1513,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15051513
stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
15061514

15071515
memset(&ps, 0, sizeof(ps));
1508-
ret = do_push_stash(ps, stash_msg, quiet, keep_index,
1516+
ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
15091517
patch_mode, include_untracked);
15101518

15111519
strbuf_release(&stash_msg_buf);

0 commit comments

Comments
 (0)