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 <peff@peff.net> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Mar 11, 2019 at 22:16 UTC 7db9302d7cc7affeb1d576a050407f40404bd20f
1 file changed +38 -30
builtin/stash.c
+38 -30
@@ -826,11 +826,11 @@ static int store_stash(int argc, const char **argv, const char *prefix)
826 }
827
828 static void add_pathspecs(struct argv_array *args,
829 - struct pathspec ps) {
829 + const struct pathspec *ps) {
830 int i;
831
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);
834 }
835
836 /*
@@ -840,7 +840,7 @@ static void add_pathspecs(struct argv_array *args,
840 * = 0 if there are not any untracked files
841 * > 0 if there are untracked files
842 */
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,
844 struct strbuf *untracked_files)
845 {
846 int i;
@@ -853,12 +853,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
853 if (include_untracked != INCLUDE_ALL_FILES)
854 setup_standard_excludes(&dir);
855
856 - seen = xcalloc(ps.nr, 1);
856 + seen = xcalloc(ps->nr, 1);
857
858 - max_len = fill_directory(&dir, the_repository->index, &ps);
858 + max_len = fill_directory(&dir, the_repository->index, ps);
859 for (i = 0; i < dir.nr; i++) {
860 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)) {
862 found++;
863 strbuf_addstr(untracked_files, ent->name);
864 /* NUL-terminate: will be fed to update-index -z */
@@ -881,11 +881,12 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
881 * = 0 if there are no changes.
882 * > 0 if there are changes.
883 */
884 -static int check_changes_tracked_files(struct pathspec ps)
884 +static int check_changes_tracked_files(const struct pathspec *ps)
885 {
886 int result;
887 struct rev_info rev;
888 struct object_id dummy;
889 + int ret = 0;
890
891 /* No initial commit. */
892 if (get_oid("HEAD", &dummy))
@@ -895,7 +896,7 @@ static int check_changes_tracked_files(struct pathspec ps)
896 return -1;
897
898 init_revisions(&rev, NULL);
898 - rev.prune_data = ps;
899 + copy_pathspec(&rev.prune_data, ps);
900
901 rev.diffopt.flags.quick = 1;
902 rev.diffopt.flags.ignore_submodules = 1;
@@ -905,22 +906,28 @@ static int check_changes_tracked_files(struct pathspec ps)
906 diff_setup_done(&rev.diffopt);
907
908 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 + }
913
914 object_array_clear(&rev.pending);
915 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 + }
920
916 - return 0;
921 +done:
922 + clear_pathspec(&rev.prune_data);
923 + return ret;
924 }
925
926 /*
927 * The function will fill `untracked_files` with the names of untracked files
928 * It will return 1 if there were any changes and 0 if there were not.
929 */
923 -static int check_changes(struct pathspec ps, int include_untracked,
930 +static int check_changes(const struct pathspec *ps, int include_untracked,
931 struct strbuf *untracked_files)
932 {
933 int ret = 0;
@@ -974,7 +981,7 @@ done:
981 return ret;
982 }
983
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,
985 struct strbuf *out_patch, int quiet)
986 {
987 int ret = 0;
@@ -1033,7 +1040,7 @@ done:
1040 return ret;
1041 }
1042
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)
1044 {
1045 int ret = 0;
1046 struct rev_info rev;
@@ -1042,6 +1049,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1049 struct index_state istate = { NULL };
1050
1051 init_revisions(&rev, NULL);
1052 + copy_pathspec(&rev.prune_data, ps);
1053
1054 set_alternate_index_output(stash_index_path.buf);
1055 if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1050,7 +1058,6 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1058 }
1059 set_alternate_index_output(NULL);
1060
1053 - rev.prune_data = ps;
1061 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1062 rev.diffopt.format_callback = add_diff_to_buf;
1063 rev.diffopt.format_callback_data = &diff_output;
@@ -1089,12 +1096,13 @@ done:
1096 discard_index(&istate);
1097 UNLEAK(rev);
1098 object_array_clear(&rev.pending);
1099 + clear_pathspec(&rev.prune_data);
1100 strbuf_release(&diff_output);
1101 remove_path(stash_index_path.buf);
1102 return ret;
1103 }
1104
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,
1106 int include_untracked, int patch_mode,
1107 struct stash_info *info, struct strbuf *patch,
1108 int quiet)
@@ -1226,10 +1234,10 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1234 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1235
1236 memset(&ps, 0, sizeof(ps));
1229 - if (!check_changes_tracked_files(ps))
1237 + if (!check_changes_tracked_files(&ps))
1238 return 0;
1239
1232 - ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1240 + ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1241 NULL, 0);
1242 if (!ret)
1243 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)
1246 return ret;
1247 }
1248
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,
1250 int keep_index, int patch_mode, int include_untracked)
1251 {
1252 int ret = 0;
@@ -1258,15 +1266,15 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1266 }
1267
1268 read_cache_preload(NULL);
1261 - if (!include_untracked && ps.nr) {
1269 + if (!include_untracked && ps->nr) {
1270 int i;
1263 - char *ps_matched = xcalloc(ps.nr, 1);
1271 + char *ps_matched = xcalloc(ps->nr, 1);
1272
1273 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,
1275 ps_matched);
1276
1269 - if (report_path_error(ps_matched, &ps, NULL)) {
1277 + if (report_path_error(ps_matched, ps, NULL)) {
1278 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1279 ret = -1;
1280 free(ps_matched);
@@ -1313,7 +1321,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1321 stash_msg_buf.buf);
1322
1323 if (!patch_mode) {
1316 - if (include_untracked && !ps.nr) {
1324 + if (include_untracked && !ps->nr) {
1325 struct child_process cp = CHILD_PROCESS_INIT;
1326
1327 cp.git_cmd = 1;
@@ -1327,7 +1335,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1335 }
1336 }
1337 discard_cache();
1330 - if (ps.nr) {
1338 + if (ps->nr) {
1339 struct child_process cp_add = CHILD_PROCESS_INIT;
1340 struct child_process cp_diff = CHILD_PROCESS_INIT;
1341 struct child_process cp_apply = CHILD_PROCESS_INIT;
@@ -1468,7 +1476,7 @@ static int push_stash(int argc, const char **argv, const char *prefix)
1476
1477 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1478 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,
1480 include_untracked);
1481 }
1482
@@ -1505,7 +1513,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
1513 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1514
1515 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,
1517 patch_mode, include_untracked);
1518
1519 strbuf_release(&stash_msg_buf);