Skip to content

Commit 15fcc3e

Browse files
ungpsdscho
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. https://public-inbox.org/git/[email protected]/ Signed-off-by: Paul-Sebastian Ungureanu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb83953 commit 15fcc3e

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

builtin/stash--helper.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -884,18 +884,18 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
884884
}
885885

886886
/*
887-
* The return value of `check_changes()` can be:
887+
* The return value of `check_changes_tracked_files()` can be:
888888
*
889889
* < 0 if there was an error
890890
* = 0 if there are no changes.
891891
* > 0 if there are changes.
892892
*/
893-
static int check_changes(struct pathspec ps, int include_untracked)
893+
894+
static int check_changes_tracked_files(struct pathspec ps)
894895
{
895896
int result;
896897
struct rev_info rev;
897898
struct object_id dummy;
898-
struct strbuf out = STRBUF_INIT;
899899

900900
/* No initial commit. */
901901
if (get_oid("HEAD", &dummy))
@@ -923,14 +923,26 @@ static int check_changes(struct pathspec ps, int include_untracked)
923923
if (diff_result_code(&rev.diffopt, result))
924924
return 1;
925925

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

932-
strbuf_release(&out);
933-
return 0;
945+
return ret;
934946
}
935947

936948
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1141,7 +1153,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11411153
head_commit = lookup_commit(the_repository, &info->b_commit);
11421154
}
11431155

1144-
if (!check_changes(ps, include_untracked)) {
1156+
if (!check_changes(ps, include_untracked, &untracked_files)) {
11451157
ret = 1;
11461158
goto done;
11471159
}
@@ -1166,8 +1178,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11661178
goto done;
11671179
}
11681180

1169-
if (include_untracked && get_untracked_files(ps, include_untracked,
1170-
&untracked_files)) {
1181+
if (include_untracked) {
11711182
if (save_untracked_files(info, &msg, untracked_files)) {
11721183
if (!quiet)
11731184
fprintf_ln(stderr, _("Cannot save "
@@ -1252,20 +1263,15 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12521263
0);
12531264

12541265
memset(&ps, 0, sizeof(ps));
1255-
strbuf_addstr(&stash_msg_buf, stash_msg);
1256-
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1257-
NULL, 0);
1266+
if (!check_changes_tracked_files(ps))
1267+
return 0;
12581268

1259-
if (!ret)
1269+
strbuf_addstr(&stash_msg_buf, stash_msg);
1270+
if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
12601271
printf_ln("%s", oid_to_hex(&info.w_commit));
12611272

12621273
strbuf_release(&stash_msg_buf);
1263-
1264-
/*
1265-
* ret can be 1 if there were no changes. In this case, we should
1266-
* not error out.
1267-
*/
1268-
return ret < 0;
1274+
return ret;
12691275
}
12701276

12711277
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1275,6 +1281,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12751281
struct stash_info info;
12761282
struct strbuf patch = STRBUF_INIT;
12771283
struct strbuf stash_msg_buf = STRBUF_INIT;
1284+
struct strbuf untracked_files = STRBUF_INIT;
12781285

12791286
if (patch_mode && keep_index == -1)
12801287
keep_index = 1;
@@ -1309,7 +1316,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13091316
goto done;
13101317
}
13111318

1312-
if (!check_changes(ps, include_untracked)) {
1319+
if (!check_changes(ps, include_untracked, &untracked_files)) {
13131320
if (!quiet)
13141321
printf_ln(_("No local changes to save"));
13151322
goto done;

0 commit comments

Comments
 (0)