// add appends u to file's list of units, as well as the list of units // for each of file's ancestor dirs. func (v filesToUnits) add(file string, u unit.ID2) { file = path.Clean(file) v[file] = append(v[file], u) for _, dir := range util.AncestorDirs(file, false) { v.addIfNotExists(dir, u) } }
// add appends ofs to file's list of def offsets, as well as the list // of def offsets for each of file's ancestor dirs. If an entry has // more than perFile offsets already, no more are appended. func (v filesToDefOfs) add(file string, ofs int64, perFile int) { file = path.Clean(file) if len(v[file]) >= perFile { return } v[file] = append(v[file], ofs) for _, dir := range util.AncestorDirs(file, false) { if len(v[dir]) < perFile { v[dir] = append(v[dir], ofs) } } }
func TestAncestorDirsExceptRoot(t *testing.T) { tests := map[string][]string{ ".": nil, "/": nil, "": nil, "a": nil, "a/": []string{"a"}, // maybe we don't want this behavior "a/b": []string{"a"}, "a/b/c": []string{"a", filepath.FromSlash("a/b")}, } for p, want := range tests { dirs := util.AncestorDirs(p, false) if !reflect.DeepEqual(dirs, want) { t.Errorf("%v: got %v, want %v", p, dirs, want) } } }
func getRootDir(dir string) (rootDir string, vcsType string, err error) { dir, err = filepath.Abs(dir) if err != nil { return "", "", err } ancestors := util.AncestorDirs(dir, true) vcsTypes := []string{"git", "hg"} for i := len(ancestors) - 1; i >= 0; i-- { ancDir := ancestors[i] for _, vt := range vcsTypes { // Don't check that the FileInfo is a dir because git // submodules have a .git file. if _, err := os.Stat(filepath.Join(ancDir, "."+vt)); err == nil { return ancDir, vt, nil } } } return "", "", nil }