src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 542 forks source link

Worktree if support add deleted files #1268

Open magicalcosmos opened 4 years ago

magicalcosmos commented 4 years ago

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?

aRestless commented 4 years ago

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.

indeedhat commented 4 years ago

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