Repository navigation
Delete a cached Git repository from the UI and API #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package gitcache | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrNotCached is returned by Remove for a repository the cache does | ||
| // not hold. | ||
| ErrNotCached = errors.New("repository is not cached") | ||
| // ErrInUse is returned by Remove for a repository a clone, a fetch or | ||
| // a client is using. | ||
| ErrInUse = errors.New("repository is in use") | ||
| ) | ||
|
|
||
| // Remove deletes the cached repository under key, as eviction does, and | ||
| // returns its size. The next request for it clones it again. A repository | ||
| // in use is not removed and returns ErrInUse, so nothing reads it while it | ||
| // is deleted. | ||
| func (c *Cache) Remove(key string) (int64, error) { | ||
| c.mu.Lock() | ||
| rp := c.repos[c.repoKey(key)] | ||
| if rp == nil { | ||
| c.mu.Unlock() | ||
| return 0, ErrNotCached | ||
| } | ||
| // Before the directory check: a first clone is built elsewhere and | ||
| // moved into place once done. | ||
| if rp.users > 0 { | ||
| c.mu.Unlock() | ||
| return 0, ErrInUse | ||
| } | ||
| if !rp.exists() { | ||
| c.mu.Unlock() | ||
| return 0, ErrNotCached | ||
| } | ||
| // Moved aside under the lock, so a request that comes after it clones | ||
| // the repository again instead of reading it while it is deleted. | ||
| trash, err := os.MkdirTemp(c.tmpDir(), "removed-") | ||
| if err == nil { | ||
| err = os.Rename(rp.dir, filepath.Join(trash, "repo")) | ||
| } | ||
| if err != nil { | ||
| c.mu.Unlock() | ||
| if trash != "" { | ||
| _ = os.Remove(trash) | ||
| } | ||
| return 0, err | ||
| } | ||
| delete(c.repos, rp.key) | ||
| delete(c.tooLarge, rp.key) | ||
| // A want the old copy could not fit may fit in the next clone. | ||
| for k := range c.failedWants { | ||
| if strings.HasPrefix(k, rp.key+" ") { | ||
| delete(c.failedWants, k) | ||
| } | ||
| } | ||
| size := rp.size.Load() | ||
| c.mu.Unlock() | ||
|
|
||
| c.logger.Info("git cache: removed repository", "repository", rp.key, "size", size) | ||
| c.removeEvicted([]string{trash}) | ||
| return size, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package gitcache | ||
|
|
||
| import ( | ||
| "errors" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRemoveDeletesARepository(t *testing.T) { | ||
| upstream := newFakeUpstream(t) | ||
| upstream.newRepo("repo") | ||
| tc := newTestCache(t, upstream.server.URL, nil) | ||
| tc.clone(t, "repo") | ||
| waitIdle(t, tc.Cache) | ||
| dir := filepath.Join(tc.dir, "up", "repo.git") | ||
| want := tc.repo("up/repo.git").size.Load() | ||
|
|
||
| freed, err := tc.Remove("up/repo.git") | ||
| if err != nil { | ||
| t.Fatalf("Remove: %v", err) | ||
| } | ||
| if freed != want || freed == 0 { | ||
| t.Errorf("freed %d bytes, want %d", freed, want) | ||
| } | ||
| if fileExists(dir) || tc.repo("up/repo.git") != nil { | ||
| t.Error("the repository is still cached") | ||
| } | ||
| if len(tc.Repositories()) != 0 { | ||
| t.Errorf("Repositories() = %+v, want none", tc.Repositories()) | ||
| } | ||
| if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrNotCached) { | ||
| t.Errorf("second Remove: %v, want ErrNotCached", err) | ||
| } | ||
|
|
||
| // The next clone fetches it again. | ||
| requests := upstream.gitRequests.Load() | ||
| tc.clone(t, "repo") | ||
| if upstream.gitRequests.Load() == requests || !fileExists(dir) { | ||
| t.Error("the clone after Remove did not fetch the repository again") | ||
| } | ||
| } | ||
|
|
||
| // A repository that a clone, a fetch or a client is using is not removed, | ||
| // so nothing reads it while it is deleted. | ||
| func TestRemoveRefusesARepositoryInUse(t *testing.T) { | ||
| upstream := newFakeUpstream(t) | ||
| upstream.newRepo("repo") | ||
| tc := newTestCache(t, upstream.server.URL, nil) | ||
| tc.clone(t, "repo") | ||
| waitIdle(t, tc.Cache) | ||
|
|
||
| rp := tc.acquire("up/repo.git") | ||
| if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrInUse) { | ||
| t.Errorf("Remove while in use: %v, want ErrInUse", err) | ||
| } | ||
| tc.release(rp) | ||
| if !fileExists(filepath.Join(tc.dir, "up", "repo.git")) { | ||
| t.Fatal("the repository was removed while in use") | ||
| } | ||
| if _, err := tc.Remove("up/repo.git"); err != nil { | ||
| t.Errorf("Remove once idle: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestRemoveUnknownRepository(t *testing.T) { | ||
| upstream := newFakeUpstream(t) | ||
| tc := newTestCache(t, upstream.server.URL, nil) | ||
| for _, key := range []string{"up/missing.git", "up/../up.git", ""} { | ||
| if _, err := tc.Remove(key); !errors.Is(err, ErrNotCached) { | ||
| t.Errorf("Remove(%q): %v, want ErrNotCached", key, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A repository whose first clone is still running is in use, not missing. | ||
| func TestRemoveRefusesARepositoryBeingCloned(t *testing.T) { | ||
| upstream := newFakeUpstream(t) | ||
| upstream.newRepo("repo") | ||
| tc := newTestCache(t, upstream.server.URL, nil) | ||
| arrived, release := upstream.holdFetches() | ||
| cloned := make(chan error, 1) | ||
| go func() { | ||
| _, err := tryGit(t, "", "clone", "--quiet", tc.url("repo"), filepath.Join(t.TempDir(), "clone")) | ||
| cloned <- err | ||
| }() | ||
| <-arrived | ||
| if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrInUse) { | ||
| t.Errorf("Remove during the first clone: %v, want ErrInUse", err) | ||
| } | ||
| release() | ||
| if err := <-cloned; err != nil { | ||
| t.Fatalf("clone: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Wants a removed repository could not get may fit once it is cloned again, | ||
| // so removing it clears them, and only them. | ||
| func TestRemoveForgetsTheRepositorysFailedWants(t *testing.T) { | ||
| upstream := newFakeUpstream(t) | ||
| upstream.newRepo("repo") | ||
| tc := newTestCache(t, upstream.server.URL, nil) | ||
| tc.clone(t, "repo") | ||
| waitIdle(t, tc.Cache) | ||
| oid := "0123456789abcdef0123456789abcdef01234567" | ||
| tc.recordFailedWants("up/repo.git", []string{oid}, ErrTooLarge) | ||
| tc.recordFailedWants("up/other.git", []string{oid}, ErrTooLarge) | ||
|
|
||
| if _, err := tc.Remove("up/repo.git"); err != nil { | ||
| t.Fatalf("Remove: %v", err) | ||
| } | ||
| if got := tc.withoutFailedWants("up/repo.git", []string{oid}); len(got) != 1 { | ||
| t.Error("the removed repository's failed want is still held back") | ||
| } | ||
| if got := tc.withoutFailedWants("up/other.git", []string{oid}); len(got) != 0 { | ||
| t.Error("another repository's failed want was cleared") | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.