mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
refactor(net/gitshallow): finalize release API
This commit is contained in:
parent
b67ea17776
commit
11f141cc3a
@ -101,7 +101,7 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "Syncing %s -> %s... ", url, absPath)
|
fmt.Fprintf(os.Stderr, "Syncing %s -> %s... ", url, absPath)
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
repo := gitshallow.New(url, absPath, cfg.Depth, cfg.Branch)
|
repo := gitshallow.New(url, absPath, cfg.Depth, cfg.Branch)
|
||||||
updated, err := repo.Fetch(ctx)
|
updated, err := repo.Update(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr)
|
fmt.Fprintln(os.Stderr)
|
||||||
fmt.Fprintf(os.Stderr, "error: sync: %v\n", err)
|
fmt.Fprintf(os.Stderr, "error: sync: %v\n", err)
|
||||||
|
|||||||
@ -57,10 +57,10 @@ func TestRepo_ForcePushRecovery(t *testing.T) {
|
|||||||
|
|
||||||
// gitshallow clones, reads v1.
|
// gitshallow clones, reads v1.
|
||||||
repo := gitshallow.New(upstream, clone, 1, "main")
|
repo := gitshallow.New(upstream, clone, 1, "main")
|
||||||
if updated, err := repo.Fetch(t.Context()); err != nil {
|
if updated, err := repo.Update(t.Context()); err != nil {
|
||||||
t.Fatalf("first Fetch: %v", err)
|
t.Fatalf("first Update: %v", err)
|
||||||
} else if !updated {
|
} else if !updated {
|
||||||
t.Fatal("first Fetch: expected updated=true")
|
t.Fatal("first Update: expected updated=true")
|
||||||
}
|
}
|
||||||
got, err := os.ReadFile(filepath.Join(clone, "data.txt"))
|
got, err := os.ReadFile(filepath.Join(clone, "data.txt"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -85,7 +85,7 @@ func TestRepo_ForcePushRecovery(t *testing.T) {
|
|||||||
// Use a fresh instance: the same *Repo has a 1s debounce that would
|
// Use a fresh instance: the same *Repo has a 1s debounce that would
|
||||||
// skip the follow-up fetch in this test's wall-clock window.
|
// skip the follow-up fetch in this test's wall-clock window.
|
||||||
repo2 := gitshallow.New(upstream, clone, 1, "main")
|
repo2 := gitshallow.New(upstream, clone, 1, "main")
|
||||||
updated, err := repo2.Fetch(t.Context())
|
updated, err := repo2.Update(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("post-force-push Fetch: %v", err)
|
t.Fatalf("post-force-push Fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -222,11 +222,11 @@ func (r *Repo) gc(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch clones the repo if missing, pulls otherwise, and conditionally runs
|
// Fetch clones the repo if missing, pulls otherwise, and conditionally runs
|
||||||
// GC based on GCInterval. Returns whether HEAD changed. Implements Fetcher.
|
// GC based on GCInterval. Returns whether HEAD changed. Implements Upstream.
|
||||||
// Safe to call concurrently — concurrent callers share a single in-flight
|
// Safe to call concurrently — concurrent callers share a single in-flight
|
||||||
// fetch (via singleflight) and all receive the same result. To force a
|
// fetch (via singleflight) and all receive the same result. To force a
|
||||||
// pull regardless of MaxAge, set MaxAge=-1 before calling.
|
// pull regardless of MaxAge, set MaxAge=-1 before calling.
|
||||||
func (r *Repo) Fetch(ctx context.Context) (updated bool, err error) {
|
func (r *Repo) Update(ctx context.Context) (updated bool, err error) {
|
||||||
// MaxAge: file-mtime gate (FETCH_HEAD is rewritten on every successful
|
// MaxAge: file-mtime gate (FETCH_HEAD is rewritten on every successful
|
||||||
// fetch, so its mtime is "last time we talked to the remote").
|
// fetch, so its mtime is "last time we talked to the remote").
|
||||||
if maxAge := r.effectiveMaxAge(); maxAge > 0 {
|
if maxAge := r.effectiveMaxAge(); maxAge > 0 {
|
||||||
@ -309,9 +309,9 @@ func (f *RepoFile) Path() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch syncs the repo and reports whether this file changed since last call.
|
// Fetch syncs the repo and reports whether this file changed since last call.
|
||||||
// Safe to call concurrently. Implements Fetcher.
|
// Safe to call concurrently. Implements Upstream.
|
||||||
func (f *RepoFile) Fetch(ctx context.Context) (bool, error) {
|
func (f *RepoFile) Update(ctx context.Context) (bool, error) {
|
||||||
if _, err := f.repo.Fetch(ctx); err != nil {
|
if _, err := f.repo.Update(ctx); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
info, err := os.Stat(f.Path())
|
info, err := os.Stat(f.Path())
|
||||||
|
|||||||
@ -36,7 +36,7 @@ func TestRepo_Clone(t *testing.T) {
|
|||||||
os.RemoveAll(dir)
|
os.RemoveAll(dir)
|
||||||
|
|
||||||
repo := gitshallow.New(testRepoURL, dir, 1, "")
|
repo := gitshallow.New(testRepoURL, dir, 1, "")
|
||||||
updated, err := repo.Fetch(t.Context())
|
updated, err := repo.Update(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Fetch (clone): %v", err)
|
t.Fatalf("Fetch (clone): %v", err)
|
||||||
}
|
}
|
||||||
@ -64,12 +64,12 @@ func TestRepo_Pull_SameInstance(t *testing.T) {
|
|||||||
|
|
||||||
repo := gitshallow.New(testRepoURL, dir, 1, "")
|
repo := gitshallow.New(testRepoURL, dir, 1, "")
|
||||||
// Ensure cloned.
|
// Ensure cloned.
|
||||||
if _, err := repo.Fetch(t.Context()); err != nil {
|
if _, err := repo.Update(t.Context()); err != nil {
|
||||||
t.Fatalf("initial Fetch: %v", err)
|
t.Fatalf("initial Fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second pull on the same instance — already at HEAD, should not advance.
|
// Second pull on the same instance — already at HEAD, should not advance.
|
||||||
updated, err := repo.Fetch(t.Context())
|
updated, err := repo.Update(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("second Fetch: %v", err)
|
t.Fatalf("second Fetch: %v", err)
|
||||||
}
|
}
|
||||||
@ -84,13 +84,13 @@ func TestRepo_Pull_FreshInstance(t *testing.T) {
|
|||||||
|
|
||||||
// Ensure cloned via a first instance.
|
// Ensure cloned via a first instance.
|
||||||
first := gitshallow.New(testRepoURL, dir, 1, "")
|
first := gitshallow.New(testRepoURL, dir, 1, "")
|
||||||
if _, err := first.Fetch(t.Context()); err != nil {
|
if _, err := first.Update(t.Context()); err != nil {
|
||||||
t.Fatalf("initial Fetch: %v", err)
|
t.Fatalf("initial Fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fresh instance with no in-memory state — git HEAD on disk drives the check.
|
// Fresh instance with no in-memory state — git HEAD on disk drives the check.
|
||||||
fresh := gitshallow.New(testRepoURL, dir, 1, "")
|
fresh := gitshallow.New(testRepoURL, dir, 1, "")
|
||||||
updated, err := fresh.Fetch(t.Context())
|
updated, err := fresh.Update(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("fresh-instance Fetch: %v", err)
|
t.Fatalf("fresh-instance Fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user