Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unsafe behaviour of func (*Worktree) Reset when commit hash is invalid #724

@piaodazhu

Description

@piaodazhu

Hi, I want to use the method Reset or Checkout to switch to my history versions. It is expected that, if I give an invalid commit Hash, Reset or Checkout will abort and return a non-nil error.

In the following code, I give Reset an invalid plumbing Hash. However, unexpected things happened:

err := wtree.Reset(&git.ResetOptions{
		Commit: plumbing.NewHash("477596ec43c530ce09519b8297f50614ac9a5675"),
		Mode:   git.HardReset,
	})
if err != nil {
         panic(err)
}

Although err != nil and there will be a panic, the worktree is switched to a bad state. Now when I send the command git log on shell, I get fatal: bad object HEAD.

That is, Reset or Checkout won't rollback when failed. The HEAD will be changed to a bad object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions