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.9k stars 540 forks source link

Add multiple paths at once #1256

Open rnag opened 4 years ago

rnag commented 4 years ago

I was surprised that this doesn't seem to be supported... In command line i can simply do:

git add <file1> <file2> <folder1> <folder2>

but in go-git I guess i would need to do:

paths := []string{"file2", "file2", "folder1", "folder2"}
for p := range paths {
  _, err := gitRepo.Worktree().Add(p)
  CheckIfError(err)
}

is that right? It seems like there should be an easier way 🤔 Also I'm concerned that eventually this way it will end up being much slower than via the command line, if i am adding a lot of files and directories (but in the case I don't want to do a git add ., if i have other untracked files that i don't want to add). I was worried it will be slower because i notice that it's calling git status each time before committing every file or folder, as well as running checks on each path like whether it was modified, is a file or directory for example - could there be an easier way to batch add paths to the index that i'm missing?

rnag commented 4 years ago

from my personal tests, when i'm adding multiple directories and files to the index, it seems to take the same amount of time with go-git vs. calling command line git add <paths>. So I'm just running git add <paths> same as from command line, instead for now.

also, I was getting a weird error when I'm adding a file to the index that was not changed (via go-git), but is already in the index and on remote (for example, a README.md). I receive below error:

error: rename <longPath>\.git\objects\<obj>: Access is denied.

When I call git add <paths> from within command line or exec.Command(), it doesn't seem to throw me any strange error like that as well 👍