fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

gitindex: index symbolic links (#237)

Up to now we skipped symbolic links when compiling the list of files
during indexing. With this change we index symbolic links with the
relative path of their target file as content.

author
Stefan Hengl
committer
GitHub
date (Jan 10, 2022, 12:00 PM +0100) commit b94d682b parent 2195b41e
+110 -3
+1 -1
gitindex/tree.go
··· 178 178 } 179 179 180 180 switch e.Mode { 181 - case filemode.Regular, filemode.Executable: 181 + case filemode.Regular, filemode.Executable, filemode.Symlink: 182 182 default: 183 183 return nil 184 184 }
+109 -2
gitindex/tree_test.go
··· 29 29 "testing" 30 30 "time" 31 31 32 + "github.com/google/go-cmp/cmp" 32 33 "github.com/google/zoekt" 33 34 "github.com/google/zoekt/build" 34 35 "github.com/google/zoekt/query" ··· 54 55 cd bdir 55 56 git init -b master 56 57 echo bcont > bfile 57 - git add bfile 58 + ln -s bfile bsymlink 59 + git add bfile bsymlink 58 60 git config user.email "you@example.com" 59 61 git config user.name "Your Name" 60 62 git commit -am bmsg ··· 180 182 } 181 183 sort.Strings(paths) 182 184 183 - want := []string{".gitmodules", "afile", "bname/bfile", "subdir/sub-file"} 185 + want := []string{".gitmodules", "afile", "bname/bfile", "bname/bsymlink", "subdir/sub-file"} 184 186 if !reflect.DeepEqual(paths, want) { 185 187 t.Errorf("got %v, want %v", paths, want) 186 188 } ··· 255 257 t.Errorf("got %v, want 1 result", results.Files) 256 258 } else if f := results.Files[0]; f.Version == subVersion { 257 259 t.Errorf("version in super repo matched version is subrepo.") 260 + } 261 + } 262 + 263 + func createSymlinkRepo(dir string) error { 264 + if err := os.MkdirAll(dir, 0o755); err != nil { 265 + return err 266 + } 267 + script := `mkdir adir bdir 268 + git init 269 + git config user.email "you@example.com" 270 + git config user.name "Your Name" 271 + 272 + echo acont > adir/afile 273 + git add adir/afile 274 + 275 + echo bcont > bdir/bfile 276 + git add bdir/bfile 277 + 278 + ln -s ./adir/afile asymlink 279 + git add asymlink 280 + 281 + git commit -am amsg 282 + 283 + cat << EOF > .git/config 284 + [core] 285 + repositoryformatversion = 0 286 + filemode = true 287 + bare = true 288 + [remote "origin"] 289 + url = http://codehost.com/arepo 290 + [branch "master"] 291 + remote = origin 292 + merge = refs/heads/master 293 + EOF 294 + ` 295 + cmd := exec.Command("/bin/sh", "-euxc", script) 296 + cmd.Dir = dir 297 + if out, err := cmd.CombinedOutput(); err != nil { 298 + return fmt.Errorf("execution error: %v, output %s", err, out) 299 + } 300 + return nil 301 + } 302 + 303 + func TestSearchSymlinkByContent(t *testing.T) { 304 + dir, err := ioutil.TempDir("", "") 305 + if err != nil { 306 + t.Fatalf("TempDir: %v", err) 307 + } 308 + defer os.RemoveAll(dir) 309 + 310 + if err := createSymlinkRepo(dir); err != nil { 311 + t.Fatalf("createSubmoduleRepo: %v", err) 312 + } 313 + 314 + indexDir, err := ioutil.TempDir("", "") 315 + if err != nil { 316 + t.Fatal(err) 317 + } 318 + defer os.RemoveAll(indexDir) 319 + 320 + buildOpts := build.Options{ 321 + IndexDir: indexDir, 322 + } 323 + opts := Options{ 324 + RepoDir: filepath.Join(dir), 325 + BuildOptions: buildOpts, 326 + BranchPrefix: "refs/heads/", 327 + Branches: []string{"master"}, 328 + Submodules: true, 329 + Incremental: true, 330 + RepoCacheDir: dir, 331 + } 332 + if err := IndexGitRepo(opts); err != nil { 333 + t.Fatalf("IndexGitRepo: %v", err) 334 + } 335 + 336 + searcher, err := shards.NewDirectorySearcher(indexDir) 337 + if err != nil { 338 + t.Fatal("NewDirectorySearcher", err) 339 + } 340 + defer searcher.Close() 341 + 342 + // The content of the symlink and the file path the symlink points to both 343 + // contain the string "afile". Hence we expect 1 path match and 1 content match. 344 + results, err := searcher.Search(context.Background(), 345 + &query.Substring{Pattern: "afile"}, 346 + &zoekt.SearchOptions{}) 347 + if err != nil { 348 + t.Fatal("Search", err) 349 + } 350 + 351 + if len(results.Files) != 2 { 352 + t.Fatalf("got search result %v, want 2 files", results.Files) 353 + } 354 + 355 + got := make([]string, 0, 2) 356 + for _, file := range results.Files { 357 + got = append(got, file.FileName) 358 + } 359 + sort.Strings(got) 360 + 361 + want := []string{"adir/afile", "asymlink"} 362 + 363 + if d := cmp.Diff(want, got); d != "" { 364 + t.Fatalf("-want, +got %s\n", d) 258 365 } 259 366 } 260 367