go-git
go-git copied to clipboard
Pattern support for file path?
I am trying to figure out if the library supports the equivalent to "git ls-files 'pattern'" to get references to multiple files matching eg. a '*/foo' pattern (to find all files in a repository with a certain name).
It looks like References can list paths: https://godoc.org/gopkg.in/src-d/go-git.v4#References
However, it has to be a full file path, like "src/foo" and doesn't seem to provide a way to list "all files matching foo".
Is there another way to do this, or is this a feature request? (I suppose iterating through every file in the repository is an option but not really a feasible one for large repos)
Note that References
function will probably disappear or made private once we release go-git v4 as stable.
Currently this functionality is not provided out of the box, but you can easily implement it in a couple of ways. Once you get an object.Commit
you can iterate its files with Files
or retrieve the Tree
to perform a custom walk. See the doc of the object package here: https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/object
In the _examples
directory you have an example that replicates git ls-tree -r HEAD
. You can extend it to filter files given a regular expression or glob:
https://github.com/src-d/go-git/blob/master/_examples/showcase/main.go
Here is a code snippet I used to get the LOC for a given filePath:
func FileLOCFromTree(tree *object.Tree, filePath string) int {
loc := 0
tree.Files().ForEach(func(f *object.File) error {
if f.Name == filePath {
lines, _ := f.Lines()
loc = len(lines)
}
return nil
})
return loc
}