Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Pattern support for file path? #343

Open
pval opened this issue Apr 13, 2017 · 2 comments
Open

Pattern support for file path? #343

pval opened this issue Apr 13, 2017 · 2 comments

Comments

@pval
Copy link

pval commented Apr 13, 2017

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)

@smola
Copy link
Collaborator

smola commented Apr 17, 2017

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

@ashishgalagali
Copy link

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

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants