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)
| 1513 | // Both pattern and the results are relative to the current working directory, not |
| 1514 | // the root of the repository |
| 1515 | func 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 | |
| 1555 | func sanitizePattern(pattern string) string { |
| 1556 | if strings.HasPrefix(pattern, "/") { |