Skip to content

Commit f84e3df

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 7e6d398 commit f84e3df

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
@@ -886,18 +886,18 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
886886
}
887887

888888
/*
889-
* The return value of `check_changes()` can be:
889+
* The return value of `check_changes_tracked_files()` can be:
890890
*
891891
* < 0 if there was an error
892892
* = 0 if there are no changes.
893893
* > 0 if there are changes.
894894
*/
895-
static int check_changes(struct pathspec ps, int include_untracked)
895+
896+
static int check_changes_tracked_files(struct pathspec ps)
896897
{
897898
int result;
898899
struct rev_info rev;
899900
struct object_id dummy;
900-
struct strbuf out = STRBUF_INIT;
901901

902902
/* No initial commit. */
903903
if (get_oid("HEAD", &dummy))
@@ -925,14 +925,26 @@ static int check_changes(struct pathspec ps, int include_untracked)
925925
if (diff_result_code(&rev.diffopt, result))
926926
return 1;
927927

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

934-
strbuf_release(&out);
935-
return 0;
947+
return ret;
936948
}
937949

938950
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1143,7 +1155,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11431155
head_commit = lookup_commit(the_repository, &info->b_commit);
11441156
}
11451157

1146-
if (!check_changes(ps, include_untracked)) {
1158+
if (!check_changes(ps, include_untracked, &untracked_files)) {
11471159
ret = 1;
11481160
goto done;
11491161
}
@@ -1168,8 +1180,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11681180
goto done;
11691181
}
11701182

1171-
if (include_untracked && get_untracked_files(ps, include_untracked,
1172-
&untracked_files)) {
1183+
if (include_untracked) {
11731184
if (save_untracked_files(info, &msg, untracked_files)) {
11741185
if (!quiet)
11751186
fprintf_ln(stderr, _("Cannot save "
@@ -1254,20 +1265,15 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12541265
0);
12551266

12561267
memset(&ps, 0, sizeof(ps));
1257-
strbuf_addstr(&stash_msg_buf, stash_msg);
1258-
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1259-
NULL, 0);
1268+
if (!check_changes_tracked_files(ps))
1269+
return 0;
12601270

1261-
if (!ret)
1271+
strbuf_addstr(&stash_msg_buf, stash_msg);
1272+
if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
12621273
printf_ln("%s", oid_to_hex(&info.w_commit));
12631274

12641275
strbuf_release(&stash_msg_buf);
1265-
1266-
/*
1267-
* ret can be 1 if there were no changes. In this case, we should
1268-
* not error out.
1269-
*/
1270-
return ret < 0;
1276+
return ret;
12711277
}
12721278

12731279
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1277,6 +1283,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12771283
struct stash_info info;
12781284
struct strbuf patch = STRBUF_INIT;
12791285
struct strbuf stash_msg_buf = STRBUF_INIT;
1286+
struct strbuf untracked_files = STRBUF_INIT;
12801287

12811288
if (patch_mode && keep_index == -1)
12821289
keep_index = 1;
@@ -1311,7 +1318,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13111318
goto done;
13121319
}
13131320

1314-
if (!check_changes(ps, include_untracked)) {
1321+
if (!check_changes(ps, include_untracked, &untracked_files)) {
13151322
if (!quiet)
13161323
printf_ln(_("No local changes to save"));
13171324
goto done;

0 commit comments

Comments
 (0)