Skip to content

Commit eb5639d

Browse files
committed
advice: add stashBeforeCheckout advice for dirty branch switches
Add a new advice type ADVICE_STASH_BEFORE_CHECKOUT to guide users when they attempt to switch branches with local modifications that would be overwritten by the operation. This includes: > New ADVICE_STASH_BEFORE_CHECKOUT enum value in advice.h > Corresponding "stashBeforeCheckout" entry in advice_setting[] > New advise_on_checkout_dirty_files() function that lists the affected files and suggests using git stash push/pop > Documentation entry in Documentation/config/advice.txt The advice follows existing patterns established by advise_on_updating_sparse_paths() and can be silenced with: git config set advice.stashBeforeCheckout false Signed-off-by: Arsh Srivastava <arshsrivastava00@gmail.com>
1 parent d181b93 commit eb5639d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Documentation/config/advice.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ all advice messages.
126126
Shown when a sparse index is expanded to a full index, which is likely
127127
due to an unexpected set of files existing outside of the
128128
sparse-checkout.
129+
stashBeforeCheckout::
130+
Shown when the user attempts to switch branches but has
131+
local modifications that would be overwritten by the
132+
operation, to suggest using linkgit:git-stash[1] to
133+
save changes before switching.
129134
statusAheadBehind::
130135
Shown when linkgit:git-status[1] computes the ahead/behind
131136
counts for a local ref compared to its remote tracking ref,

advice.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static struct {
8181
[ADVICE_SET_UPSTREAM_FAILURE] = { "setUpstreamFailure" },
8282
[ADVICE_SKIPPED_CHERRY_PICKS] = { "skippedCherryPicks" },
8383
[ADVICE_SPARSE_INDEX_EXPANDED] = { "sparseIndexExpanded" },
84+
[ADVICE_STASH_BEFORE_CHECKOUT] = { "stashBeforeCheckout" },
8485
[ADVICE_STATUS_AHEAD_BEHIND_WARNING] = { "statusAheadBehindWarning" },
8586
[ADVICE_STATUS_HINTS] = { "statusHints" },
8687
[ADVICE_STATUS_U_OPTION] = { "statusUoption" },
@@ -312,3 +313,29 @@ void advise_on_moving_dirty_path(struct string_list *pathspec_list)
312313
"* Use \"git add --sparse <paths>\" to update the index\n"
313314
"* Use \"git sparse-checkout reapply\" to apply the sparsity rules"));
314315
}
316+
317+
void advise_on_checkout_dirty_files(struct string_list *file_list)
318+
{
319+
struct string_list_item *item;
320+
321+
if (!file_list->nr)
322+
return;
323+
324+
fprintf(stderr, _("The following files have local modifications that would\n"
325+
"be overwritten by switching branches:\n"));
326+
for_each_string_list_item(item, file_list)
327+
fprintf(stderr, "\t%s\n", item->string);
328+
329+
advise_if_enabled(ADVICE_STASH_BEFORE_CHECKOUT,
330+
_("You can save your local changes before switching by running:\n"
331+
"\n"
332+
"\tgit stash push\n"
333+
"\n"
334+
"Then restore them after switching with:\n"
335+
"\n"
336+
"\tgit stash pop\n"
337+
"\n"
338+
"Or to discard your local changes, use:\n"
339+
"\n"
340+
"\tgit checkout -- <file>"));
341+
}

advice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum advice_type {
4848
ADVICE_SET_UPSTREAM_FAILURE,
4949
ADVICE_SKIPPED_CHERRY_PICKS,
5050
ADVICE_SPARSE_INDEX_EXPANDED,
51+
ADVICE_STASH_BEFORE_CHECKOUT,
5152
ADVICE_STATUS_AHEAD_BEHIND_WARNING,
5253
ADVICE_STATUS_HINTS,
5354
ADVICE_STATUS_U_OPTION,
@@ -83,5 +84,6 @@ void NORETURN die_ff_impossible(void);
8384
void advise_on_updating_sparse_paths(struct string_list *pathspec_list);
8485
void detach_advice(const char *new_name);
8586
void advise_on_moving_dirty_path(struct string_list *pathspec_list);
87+
void advise_on_checkout_dirty_files(struct string_list *file_list);
8688

8789
#endif /* ADVICE_H */

0 commit comments

Comments
 (0)