git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: "Torsten Bögershausen" <tboegi@web.de>, phillip.wood@dunelm.org.uk
Cc: Till Friebe <friebetill@gmail.com>, git@vger.kernel.org
Subject: Re: Lost files after git stash && git stash pop
Date: Mon, 24 Jul 2023 10:59:51 +0100	[thread overview]
Message-ID: <6dbd02ca-9587-f797-f2d3-035fc2d9efc0@gmail.com> (raw)
In-Reply-To: <20230723205239.5snlakmd5ocy67q2@tb-raspi4>

Hi Torsten

On 23/07/2023 21:52, Torsten Bögershausen wrote:
>> I think whenever git overwrites an untracked file without the user passing
>> some option indicating that they want to do so it is a bug.
> 
> OK, agreed after reading the next sentence.
> 
>> For example "git
>> checkout" refuses to overwrite untracked files by default. Sadly this seems
>> to be a known bug in do_push_stash() where we are using "git reset --hard"
>> to remove the stashed changes from the working copy. This was documented in
>> 94b7f1563a (Comment important codepaths regarding nuking untracked
>> files/dirs, 2021-09-27). The stash implementation does a lot of necessary
>> forking of subprocesses, in this case I think it would be better to call
>> unpack_trees() directly with UNPACK_RESET_PROTECT_UNTRACKED.
> 
> Thanks for the fast response.
> 
> This is not an area of Git, where I have much understanding of the code.
> But is seems as if pop_stash() in builtin/stash.c
> (and the called functions) seems to be the problem here ?

Confusingly it is creating the stash that deletes the untracked file because
it recreates README/README. do_push_stash() in builtin/stash.c is the culprit
I think. I had hoped the diff below would fix the problem but it does not
seem to and breaks half a dozen test cases that seem to rely on removing
untracked files. Unfortunately I don't really have time to dig any
further at the moment.

Best Wishes

Phillip


diff --git a/builtin/stash.c b/builtin/stash.c
index fe64cde9ce3..c8bbfe56d26 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -29,6 +29,8 @@
  #include "exec-cmd.h"
  #include "reflog.h"
  #include "add-interactive.h"
+#include "reset.h"
+#include "submodule.h"
  
  #define INCLUDE_ALL_FILES 2
  
@@ -336,7 +338,7 @@ static int apply_cached(struct strbuf *out)
         return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
  }
  
-static int reset_head(void)
+static int stash_reset_head(void)
  {
         struct child_process cp = CHILD_PROCESS_INIT;
  
@@ -569,7 +571,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                                                 get_index_file(), 0, NULL))
                                 return error(_("could not save index tree"));
  
-                       reset_head();
+                       stash_reset_head();
                         discard_index(&the_index);
                         repo_read_index(the_repository);
                 }
@@ -1649,12 +1651,14 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
                                 goto done;
                         }
                 } else {
-                       struct child_process cp = CHILD_PROCESS_INIT;
-                       cp.git_cmd = 1;
-                       /* BUG: this nukes untracked files in the way */
-                       strvec_pushl(&cp.args, "reset", "--hard", "-q",
-                                    "--no-recurse-submodules", NULL);
-                       if (run_command(&cp)) {
+                       struct reset_head_opts opts = {
+                               .flags = RESET_HEAD_HARD,
+                       };
+
+                       if (should_update_submodules())
+                               BUG("stash should not update submodules");
+
+                       if (reset_head(the_repository, &opts)) {
                                 ret = -1;
                                 goto done;
                         }


  reply	other threads:[~2023-07-24 10:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 17:31 Lost files after git stash && git stash pop Till Friebe
2023-07-22 21:44 ` Torsten Bögershausen
2023-07-23 10:01   ` Phillip Wood
2023-07-23 20:52     ` Torsten Bögershausen
2023-07-24  9:59       ` Phillip Wood [this message]
2023-08-08 17:26 ` [PATCH v1 1/1] git stash needing mkdir deletes untracked file tboegi
2023-08-08 18:03   ` Torsten Bögershausen
2023-08-08 19:28   ` Eric Sunshine
2023-08-09 13:15   ` Phillip Wood
2023-08-09 18:47     ` Torsten Bögershausen
2023-08-15  9:15       ` Phillip Wood
2023-08-15 15:25         ` Torsten Bögershausen
2023-08-15 18:03         ` Junio C Hamano
2023-08-09 20:57     ` Junio C Hamano
2023-08-15  9:16       ` Phillip Wood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6dbd02ca-9587-f797-f2d3-035fc2d9efc0@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=friebetill@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=tboegi@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).