Skip to content

Commit 8c1e246

Browse files
ungpsGit for Windows Build Agent
authored andcommitted
stash: optimize get_untracked_files() and check_changes()
This commits introduces a optimization by avoiding calling the same functions again. For example, `git stash push -u` would call at some points the following functions: * `check_changes()` (inside `do_push_stash()`) * `do_create_stash()`, which calls: `check_changes()` and `get_untracked_files()` Note that `check_changes()` also calls `get_untracked_files()`. So, `check_changes()` is called 2 times and `get_untracked_files()` 3 times. The old function `check_changes()` now consists of two functions: `get_untracked_files()` and `check_changes_tracked_files()`. These are the call chains for `push` and `create`: * `push_stash()` -> `do_push_stash()` -> `do_create_stash()` * `create_stash()` -> `do_create_stash()` To prevent calling the same functions over and over again, `check_changes()` inside `do_create_stash()` is now placed in the caller functions (`create_stash()` and `do_push_stash()`). This way `check_changes()` and `get_untracked files()` are called only one time. Signed-off-by: Paul-Sebastian Ungureanu <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9e5409c commit 8c1e246

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

builtin/stash--helper.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -881,18 +881,17 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
881881
}
882882

883883
/*
884-
* The return value of `check_changes()` can be:
884+
* The return value of `check_changes_tracked_files()` can be:
885885
*
886886
* < 0 if there was an error
887887
* = 0 if there are no changes.
888888
* > 0 if there are changes.
889889
*/
890-
static int check_changes(struct pathspec ps, int include_untracked)
890+
static int check_changes_tracked_files(struct pathspec ps)
891891
{
892892
int result;
893893
struct rev_info rev;
894894
struct object_id dummy;
895-
struct strbuf out = STRBUF_INIT;
896895

897896
/* No initial commit. */
898897
if (get_oid("HEAD", &dummy))
@@ -920,14 +919,25 @@ static int check_changes(struct pathspec ps, int include_untracked)
920919
if (diff_result_code(&rev.diffopt, result))
921920
return 1;
922921

922+
return 0;
923+
}
924+
925+
/*
926+
* The function will fill `untracked_files` with the names of untracked files
927+
* It will return 1 if there were any changes and 0 if there were not.
928+
*/
929+
static int check_changes(struct pathspec ps, int include_untracked,
930+
struct strbuf *untracked_files)
931+
{
932+
int ret = 0;
933+
if (check_changes_tracked_files(ps))
934+
ret = 1;
935+
923936
if (include_untracked && get_untracked_files(ps, include_untracked,
924-
&out)) {
925-
strbuf_release(&out);
926-
return 1;
927-
}
937+
untracked_files))
938+
ret = 1;
928939

929-
strbuf_release(&out);
930-
return 0;
940+
return ret;
931941
}
932942

933943
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1139,7 +1149,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11391149
head_commit = lookup_commit(the_repository, &info->b_commit);
11401150
}
11411151

1142-
if (!check_changes(ps, include_untracked)) {
1152+
if (!check_changes(ps, include_untracked, &untracked_files)) {
11431153
ret = 1;
11441154
goto done;
11451155
}
@@ -1164,8 +1174,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11641174
goto done;
11651175
}
11661176

1167-
if (include_untracked && get_untracked_files(ps, include_untracked,
1168-
&untracked_files)) {
1177+
if (include_untracked) {
11691178
if (save_untracked_files(info, &msg, untracked_files)) {
11701179
if (!quiet)
11711180
fprintf_ln(stderr, _("Cannot save "
@@ -1250,19 +1259,17 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12501259
0);
12511260

12521261
memset(&ps, 0, sizeof(ps));
1262+
if (!check_changes_tracked_files(ps))
1263+
return 0;
1264+
12531265
strbuf_addstr(&stash_msg_buf, stash_msg);
12541266
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
12551267
NULL, 0);
12561268
if (!ret)
12571269
printf_ln("%s", oid_to_hex(&info.w_commit));
12581270

12591271
strbuf_release(&stash_msg_buf);
1260-
1261-
/*
1262-
* ret can be 1 if there were no changes. In this case, we should
1263-
* not error out.
1264-
*/
1265-
return ret < 0;
1272+
return ret;
12661273
}
12671274

12681275
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1272,6 +1279,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12721279
struct stash_info info;
12731280
struct strbuf patch = STRBUF_INIT;
12741281
struct strbuf stash_msg_buf = STRBUF_INIT;
1282+
struct strbuf untracked_files = STRBUF_INIT;
12751283

12761284
if (patch_mode && keep_index == -1)
12771285
keep_index = 1;
@@ -1306,7 +1314,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13061314
goto done;
13071315
}
13081316

1309-
if (!check_changes(ps, include_untracked)) {
1317+
if (!check_changes(ps, include_untracked, &untracked_files)) {
13101318
if (!quiet)
13111319
printf_ln(_("No local changes to save"));
13121320
goto done;

0 commit comments

Comments
 (0)