go-git icon indicating copy to clipboard operation
go-git copied to clipboard

Worktree if support add deleted files

Open magicalcosmos opened this issue 5 years ago • 2 comments

Hi there,

I removed files by os.RemoveAll and then add, but that's didn't work, the code as belows:

//remove local files
os.RemoveAll(path) 

// add files, prepare commit and push
repo, err := git.PlainOpen(repository)
if err != nil {
	log.Logger.Debug("fail to init repository", zap.Error(err))
	return err
}
wt, _:= repo.Worktree()
_, err = wt.Add(".")
if err != nil {
	return err
}

What's wrong with that code, any suggestions?

magicalcosmos avatar Dec 25 '19 03:12 magicalcosmos

I think I just ran into the same issue. The problem is the implementation of Worktree.doAddDirectory: https://github.com/src-d/go-git/blob/master/worktree_status.go#L299

Add only iterates through files existing on the file system. It will therefore not stage any removed files.

aRestless avatar Dec 26 '19 14:12 aRestless

I have come across the same problem, until this is fixed i have come up with the following ~dirty hack~ solution

cmd := exec.Command("git", "add", ".")
cmd.Dir = workTree.Filesystem.Root()
err = cmd.Run()

Its not pretty but it works

indeedhat avatar Jan 08 '20 21:01 indeedhat