Skip to content

stash: add --include-untracked support to git stash create #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SBhojani
Copy link

This is a single-commit pull request, and the commit message already describes the changes.

I just want to add context for where I felt the need for this. While working on a logical single commit task, my working tree is often in a state where certain changes are staged and others are not. I use this staging strategy as a conceptual model of which changes I intend to mostly keep as they are and which I intend to change, rewrite, refactor, discard, or not commit. Some of the changes in the latter category might be new untracked files. Now, I often use (my git GUI tool's equivalent of) git stash --include-untracked followed by git stash apply to take a snapshot of these changes to be able to come back to the state if I mess things up too much from that point on. Doing that results in the working directory being cleaned momentarily and then being repopulated again. My development tools often detect this as the files having been changed and then prompt me to reload them or cause rebuilds and so on. git stash store $(git stash create) has the nice feature that it doesn't touch the working directory, but it was missing the ability to also stash untracked files. This change fixes that.

Copy link

gitgitgadget bot commented Mar 24, 2025

Welcome to GitGitGadget

Hi @SBhojani, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <[email protected]>, Ill Takalook <[email protected]>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join [email protected], where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

Copy link

gitgitgadget bot commented Mar 24, 2025

Invalid author email in e33d91c: "[email protected]"

@SBhojani SBhojani force-pushed the stash-create-untracked branch from e33d91c to 95adfb8 Compare March 24, 2025 17:15
Copy link

gitgitgadget bot commented Mar 24, 2025

There are issues in commit 95adfb8:
stash: add --include-untracked support to git stash create
Commit not signed off

@SBhojani SBhojani force-pushed the stash-create-untracked branch from 95adfb8 to 4d819d7 Compare March 24, 2025 17:38
@SBhojani SBhojani marked this pull request as draft March 24, 2025 18:36
builtin/stash.c Outdated
Comment on lines 1514 to 1515
if (!check_changes_tracked_files(&ps))
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBhojani I am fairly certain that this is needed, and that this accidental removal is the underlying root cause for the CI build to fail.

This should fix it:

diff --git a/builtin/stash.c b/builtin/stash.c
index e9eb15b53407..34828d1e2209 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1524,6 +1524,8 @@ static int create_stash(int argc, const char **argv, const char *prefix,
 	strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
 
 	memset(&ps, 0, sizeof(ps));
+	if (!check_changes_tracked_files(&ps))
+		return 0;
 
 	ret = do_create_stash(&ps, &stash_msg_buf, include_untracked, 0, 0, &info,
 			      NULL, 0);

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.

Signed-off-by: SBhojani <[email protected]>
@dscho dscho force-pushed the stash-create-untracked branch from 4d819d7 to d5a62cb Compare March 25, 2025 08:18
Comment on lines -1510 to +1524
/* Starting with argv[1], since argv[0] is "create" */
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
argc = parse_options(argc, argv, prefix, options, git_stash_create_usage, 0);

strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want the --include-untracked option to be included in the message? In other words:

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

@@ -632,6 +632,47 @@ test_expect_success 'stash create - no changes' '
test_must_be_empty actual
'

test_expect_success 'stash create with --include-untracked' '
git init repo &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should not be any need to initialize a repository; When test_expect_success' code runs, it does so in a Git worktree that was prepared specifically for testing.

Comment on lines +638 to +640
echo committed >file1 &&
git add file1 &&
git commit -m "initial commit" &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more concise to run test_commit file1...

Also, wouldn't it be possible to reuse what had already been set up by an earlier test case?

@dscho
Copy link
Member

dscho commented Mar 25, 2025

/allow

Copy link

gitgitgadget bot commented Mar 25, 2025

User SBhojani is now allowed to use GitGitGadget.

WARNING: SBhojani has no public email address set on GitHub; GitGitGadget needs an email address to Cc: you on your contribution, so that you receive any feedback on the Git mailing list. Go to https://github.com/settings/profile to make your preferred email public to let GitGitGadget know which email address to use.

Comment on lines +648 to +650
grep "parent" stash_commit | wc -l >parent_count &&
echo 3 >expect &&
test_cmp expect parent_count &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBhojani As pointed out by the test failures, you will want to use grep parent stash_commit >parents && test_line_count = 3 parents instead (see the documentation of test_line_count; wc -l's output is not strictly portable).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants