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

Re: [PATCH v2] hard reset safe mode

From
Bagas Sanjaya <bagasdotme@gmail.com>
Date
Feb 14, 2022, 03:14 UTC
Message-ID
<c81e1226-2184-0f16-b81d-c11d894b4654@gmail.com>
In-Reply-To
<xmqqee4980qz.fsf@gitster.g>
On 12/02/22 06.03, Junio C Hamano wrote:
Show 45 quoted lines
> "Viaceslavus via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> From: Viacelaus <vaceslavkozin619@gmail.com>
>>
>> The power of hard reset may frequently be inconvinient for a common user. So
>> this is an implementation of safe mode for hard reset. It can be switched on
>> by setting 'reset.safe' config variable to true. When running 'reset --hard'
>> with 'reset.safe' enabled git will check if there are any staged changes
>> that may be discarded by this reset. If there is a chance of deleting the
>> changes, git will ask the user for a confirmation with Yes/No choice.
> 
> There needs an explanation on how this avoids breaking scripts that
> trust that "git reset --hard HEAD" reliably matches the index and
> the working tree files to what is recorded in HEAD without getting
> stuck waiting for any user input.  "They can turn off reset.safe" is
> not an acceptable answer.
> 
>> +static int check_commit_exists(const char *refname, const struct object_id *oid, int f, void *d)
>> +{
>> +	return is_branch(refname);
>> +}
> 
> The returned value from a for_each_ref() callback is used to tell
> the caller "stop here, no need to further iterate and call me with
> other refs".  I think this wants to say "if I ever get called even
> once, tell the caller to stop, so that it can tell its caller that
> it was stopped".
> 
>> +static void accept_discarding_changes(void) {
>> +	int answer = getc(stdin);
>> +	printf(_("Some staged changes may be discarded by this reset. Continue? [Y/n]"));
>> +
>> +	if (answer != 'y' && answer != 'Y') {
>> +		printf(_("aborted\n"));
>> +		exit(1);
>> +	}
>> +}
> 
> I'd think at least we should use git_prompt(), instead of
> hand-rolled prompt routine like this one that assumes that an
> end-user is sitting in front of the terminal waiting to be prompted.
> 
> If updating "git reset" like this patch does were a good idea to
> begin with, that is.
> 

I think it's better just to error out when there are staged changes. The message will be something like: "error: there are staged changes that will be hard-reset. Please commit or stash them before resetting. If you want to remove all changes from index, you can do so by running 'git reset'."

-- 
An old man doll... just what I always wanted! - Clara
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 6 in “forbid a hard reset before the initial commit”
  1. forbid a hard reset before the initial commitViaceslavus via GitGitGadget, Feb 2, 2022
  2. Junio C HamanoFeb 2, 2022
  3. hard reset safe modeViaceslavus via GitGitGadget, Feb 11, 2022
  4. Junio C HamanoFeb 11, 2022
  5. Bagas SanjayaFeb 14, 2022
  6. Junio C HamanoFeb 14, 2022

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.