git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Warning suggestion for git stash drop

From
LHLaurent Humblet <laurent.humblet@gmail.com>
Date
Jul 18, 2017, 07:16 UTC
Message-ID
<CAFirYm8G-8DAfapeSvuRk1QghBZE1W0Pa1h+NDMvVNT5Kd354w@mail.gmail.com>
In-Reply-To
<xmqqinjdqo1i.fsf@gitster.mtv.corp.google.com>
Oh great, an answer 20 days later ... Sorry about this.

I have been reading a bit on the topic and found that for Bash we should be using shell functions over aliases so I wrote this function to be simply added to the .bashrc:

# 'git stash drop' confirm shell function
git() {
    if [[ $@ == "stash drop" ]]; then
        read -r -p "Are you sure you want to drop your stash? [y/N] " response
        case "$response" in
            [yY][eE][sS]|[yY])
                command git "$@"
                ;;
        esac
    else
        command git "$@"
    fi
}

Be careful, I'm no Bash expert and I don't believe it is fully POSIX compliant but it's a good start I think. This could also be used for the 'rm -rf' command or any other potentially dangerous command I believe. Maybe there is a place for a .bashrc_safe_shell_functions that could be loaded by the user to add confirmation before all potentially dangerous commands but it'll be for another thread.

Thank you for your help guys and keep up with your excellent work on Git!

All the best, Laurent

On 30 June 2017 at 21:21, Junio C Hamano <gitster@pobox.com> wrote:
Show 41 quoted lines
> Laurent Humblet <laurent.humblet@gmail.com> writes:
>
>> Thank you for your feedback.
>>
>> I suppose that turning a hypothetical confirmation option 'on' would
>> impact a stash pop for instance as it automatically drops the stash if
>> it was applied without conflicts.
>>
>> What about a --confirm flag?  You could then simply alias 'git stash
>> drop --confirm' locally and it wouldn't impact anything else?
>
> I think that is probably trivial to add, but how would you make sure
> you give it?  One way may be to train your fingers to type "git sd"
> with something like this in your ~/.gitconfig:
>
>         [alias] sd = "stash drop --confirm"
>
> but at that point, you could instead have something like the
> following in you ~/bin/git-sd and get the same effect:
>
>         #!/bin/sh
>         if tty -s
>         then
>                 echo >&2 "are you sure you want to drop all stash entries?"
>                 case "$(read)" in
>                 [Yy]*) ;;
>                 *) echo >&2 "ok, let's not drop 'em"; exit 0 ;;
>                 esac
>         fi
>         exec git stash drop
>
> without adding the "--confirm" option at all.
>
> So I am not sure that would get us closer to a satisfactory solution
> to your original problem.
>
> Retroactively adding an end-user safety is hard.
>
>> Have a great week-end!
>
> You too.
Previous: Junio C Hamano
Message 6 of 6 in “Warning suggestion for git stash drop”
  1. Laurent HumbletJun 29, 2017
  2. Junio C HamanoJun 29, 2017
  3. Stefan BellerJun 29, 2017
  4. Laurent HumbletJun 30, 2017
  5. Junio C HamanoJun 30, 2017
  6. Laurent HumbletJul 18, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.