MCPcopy
hub / github.com/git-lfs/git-lfs / GetTrackedFiles

Function GetTrackedFiles

git/git.go:1515–1553  ·  view source on GitHub ↗

GetTrackedFiles returns a list of files which are tracked in Git which match the pattern specified (standard wildcard form) Both pattern and the results are relative to the current working directory, not the root of the repository

(pattern string)

Source from the content-addressed store, hash-verified

1513// Both pattern and the results are relative to the current working directory, not
1514// the root of the repository
1515func GetTrackedFiles(pattern string) ([]string, error) {
1516 safePattern := sanitizePattern(pattern)
1517 rootWildcard := len(safePattern) < len(pattern) && strings.ContainsRune(safePattern, '*')
1518
1519 var ret []string
1520 cmd, err := gitNoLFS(
1521 "ls-files",
1522 "--ignored",
1523 "--cached", // include things which are staged but not committed right now
1524 "-z", // handle special chars in filenames
1525 "-x",
1526 safePattern)
1527 if err != nil {
1528 return nil, errors.New(tr.Tr.Get("failed to find `git ls-files`: %v", err))
1529 }
1530
1531 outp, err := cmd.StdoutPipe()
1532 if err != nil {
1533 return nil, errors.New(tr.Tr.Get("failed to call `git ls-files`: %v", err))
1534 }
1535 cmd.Start()
1536 scanner := bufio.NewScanner(outp)
1537 scanner.Split(tools.SplitOnNul)
1538 for scanner.Scan() {
1539 line := scanner.Text()
1540
1541 // If the given pattern is a root wildcard, skip all files which
1542 // are not direct descendants of the repository's root.
1543 //
1544 // This matches the behavior of how .gitattributes performs
1545 // filename matches.
1546 if rootWildcard && filepath.Dir(line) != "." {
1547 continue
1548 }
1549
1550 ret = append(ret, strings.TrimSpace(line))
1551 }
1552 return ret, cmd.Wait()
1553}
1554
1555func sanitizePattern(pattern string) string {
1556 if strings.HasPrefix(pattern, "/") {

Callers 2

trackCommandFunction · 0.92
TestGetTrackedFilesFunction · 0.85

Calls 8

NewFunction · 0.92
sanitizePatternFunction · 0.85
gitNoLFSFunction · 0.85
StdoutPipeMethod · 0.80
GetMethod · 0.65
ScanMethod · 0.65
WaitMethod · 0.65
StartMethod · 0.45

Tested by 1

TestGetTrackedFilesFunction · 0.68