Skip to content

Commit 95adfb8

Browse files
author
SBhojani
committed
stash: add --include-untracked support to git stash create
The `git stash create` command now supports the `--include-untracked` flag, allowing users to include untracked files in the stash entry. This brings parity with `git stash push`, which already supports this option. Previously, `git stash create` would only stash tracked changes. With this change, users can optionally include untracked files by specifying `--include-untracked`. The implementation involves parsing the new option in `create_stash` and passing it to `do_create_stash`, which handles the creation of the stash entry. The early check for tracked changes was removed to allow stashing when only untracked files are present. Test cases were added to `t3903-stash.sh` to ensure the correct behavior, and the documentation was updated accordingly.
1 parent 683c54c commit 95adfb8

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

Diff for: Documentation/git-stash.adoc

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SYNOPSIS
2121
'git stash' save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
2222
[-u | --include-untracked] [-a | --all] [<message>]
2323
'git stash' clear
24-
'git stash' create [<message>]
24+
'git stash' create [-u | --include-untracked] [<message>]
2525
'git stash' store [(-m | --message) <message>] [-q | --quiet] <commit>
2626

2727
DESCRIPTION
@@ -139,11 +139,13 @@ drop [-q|--quiet] [<stash>]::
139139

140140
Remove a single stash entry from the list of stash entries.
141141

142-
create::
142+
create [-u | --include-untracked] [<message>]::
143143

144144
Create a stash entry (which is a regular commit object) and
145145
return its object name, without storing it anywhere in the ref
146146
namespace.
147+
If the `--include-untracked` option is used, all untracked files are
148+
also included in the stash entry.
147149
This is intended to be useful for scripts. It is probably not
148150
the command you want to use; see "push" above.
149151

@@ -170,6 +172,9 @@ up with `git clean`.
170172
all untracked files are also stashed and then cleaned up with
171173
`git clean`.
172174
+
175+
When used with the `create` command, all untracked files are also included
176+
in the stash entry.
177+
+
173178
When used with the `show` command, show the untracked files in the stash
174179
entry as part of the diff.
175180

Diff for: builtin/stash.c

+18-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
N_("git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \
5757
" [-u | --include-untracked] [-a | --all] [<message>]")
5858
#define BUILTIN_STASH_CREATE_USAGE \
59-
N_("git stash create [<message>]")
59+
N_("git stash create [-u | --include-untracked] [<message>]")
6060
#define BUILTIN_STASH_CLEAR_USAGE \
6161
"git stash clear"
6262

@@ -110,6 +110,11 @@ static const char * const git_stash_clear_usage[] = {
110110
NULL
111111
};
112112

113+
static const char * const git_stash_create_usage[] = {
114+
BUILTIN_STASH_CREATE_USAGE,
115+
NULL
116+
};
117+
113118
static const char * const git_stash_store_usage[] = {
114119
BUILTIN_STASH_STORE_USAGE,
115120
NULL
@@ -1499,22 +1504,28 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14991504
return ret;
15001505
}
15011506

1502-
static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
1507+
static int create_stash(int argc, const char **argv, const char *prefix,
15031508
struct repository *repo UNUSED)
15041509
{
15051510
int ret;
1511+
int include_untracked = 0;
1512+
struct option options[] = {
1513+
OPT_BOOL('u', "include-untracked", &include_untracked,
1514+
N_("include untracked files")),
1515+
OPT_END()
1516+
};
1517+
1518+
argc = parse_options(argc, argv, prefix, options, git_stash_create_usage, 0);
1519+
15061520
struct strbuf stash_msg_buf = STRBUF_INIT;
15071521
struct stash_info info = STASH_INFO_INIT;
15081522
struct pathspec ps;
15091523

1510-
/* Starting with argv[1], since argv[0] is "create" */
1511-
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1524+
strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
15121525

15131526
memset(&ps, 0, sizeof(ps));
1514-
if (!check_changes_tracked_files(&ps))
1515-
return 0;
15161527

1517-
ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, 0, &info,
1528+
ret = do_create_stash(&ps, &stash_msg_buf, include_untracked, 0, 0, &info,
15181529
NULL, 0);
15191530
if (!ret)
15201531
printf_ln("%s", oid_to_hex(&info.w_commit));

Diff for: t/t3903-stash.sh

+41
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,47 @@ test_expect_success 'stash create - no changes' '
632632
test_must_be_empty actual
633633
'
634634

635+
test_expect_success 'stash create with --include-untracked' '
636+
git init repo &&
637+
cd repo &&
638+
echo committed >file1 &&
639+
git add file1 &&
640+
git commit -m "initial commit" &&
641+
echo staged >file2 &&
642+
git add file2 &&
643+
echo unstaged >file3 &&
644+
echo untracked >untracked_file &&
645+
STASH_ID=$(git stash create --include-untracked "test message") &&
646+
git cat-file -p $STASH_ID >stash_commit &&
647+
grep "test message" stash_commit &&
648+
grep "parent" stash_commit | wc -l >parent_count &&
649+
echo 3 >expect &&
650+
test_cmp expect parent_count &&
651+
UNTRACKED_TREE=$(git rev-parse $STASH_ID^3^{tree}) &&
652+
git ls-tree $UNTRACKED_TREE >files &&
653+
grep untracked_file files &&
654+
test_path_is_file untracked_file
655+
'
656+
657+
test_expect_success 'stash create without --include-untracked does not include untracked files' '
658+
git init repo2 &&
659+
cd repo2 &&
660+
echo committed >file1 &&
661+
git add file1 &&
662+
git commit -m "initial commit" &&
663+
echo staged >file2 &&
664+
git add file2 &&
665+
echo unstaged >file3 &&
666+
echo untracked >untracked_file &&
667+
STASH_ID=$(git stash create "test message") &&
668+
git cat-file -p $STASH_ID >stash_commit &&
669+
grep "test message" stash_commit &&
670+
grep "parent" stash_commit | wc -l >parent_count &&
671+
echo 2 >expect &&
672+
test_cmp expect parent_count &&
673+
test_path_is_file untracked_file
674+
'
675+
635676
test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
636677
git stash clear &&
637678
test_when_finished "git reset --hard HEAD" &&

0 commit comments

Comments
 (0)