Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitea/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
cache: false
- uses: golangci/golangci-lint-action@v9
with:
version: v2.12.2
version: v2.13.2

fmt:
runs-on: ubuntu-24.04-htzhel1-ax42-a
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
cache: false
- uses: golangci/golangci-lint-action@v9
with:
version: v2.12.2
version: v2.13.2

fmt:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Stage 1 — builder
FROM golang:1.26.6-alpine AS builder
FROM golang:1.27-alpine AS builder

ARG VERSION=dev
ARG COMMIT=unknown
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/obmondo/gfetch

go 1.26.6
go 1.27

require (
github.com/go-co-op/gocron/v2 v2.22.0
Expand Down
42 changes: 34 additions & 8 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ func TestValidate_NoReposConfigured(t *testing.T) {
if err == nil {
t.Fatal("expected error for empty config, got nil")
}
var partial *PartialValidateError
if errors.As(err, &partial) {
if _, ok := errors.AsType[*PartialValidateError](err); ok {
t.Fatalf("expected plain error, got *PartialValidateError: %v", err)
}
if err.Error() != "no repos configured" {
Expand Down Expand Up @@ -647,12 +646,10 @@ func TestApplyDefaults_PruneBoolOverride(t *testing.T) {
PruneStale: tt.defaultStale,
}
repo := &RepoConfig{
Name: testRepoName,
URL: testRepoURL,
RepoDefaults: RepoDefaults{
Prune: tt.repoPrune,
PruneStale: tt.repoStale,
},
Name: testRepoName,
URL: testRepoURL,
Prune: tt.repoPrune,
PruneStale: tt.repoStale,
}
applyDefaults(repo, defaults)

Expand All @@ -676,3 +673,32 @@ func TestApplyDefaults_PruneBoolOverride(t *testing.T) {
})
}
}

func TestParseDuration(t *testing.T) {
tests := []struct {
input string
want time.Duration
wantErr bool
}{
{"", 0, false},
{"30s", 30 * time.Second, false},
{"5m", 5 * time.Minute, false},
{"2h", 2 * time.Hour, false},
{"1d", 24 * time.Hour, false},
{"0.5d", 12 * time.Hour, false},
{"invalid", 0, true},
{"10x", 0, true},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := ParseDuration(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseDuration(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
return
}
if !tt.wantErr && got != tt.want {
t.Errorf("ParseDuration(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
12 changes: 5 additions & 7 deletions pkg/daemon/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ func newTestScheduler(t *testing.T) *Scheduler {

func testRepo(name, branch string) config.RepoConfig {
return config.RepoConfig{
RepoDefaults: config.RepoDefaults{
LocalPath: "/tmp/" + name,
PollInterval: config.Duration(time.Minute),
Branches: []config.Pattern{{Raw: branch}},
},
Name: name,
URL: "https://example.com/" + name + ".git",
LocalPath: "/tmp/" + name,
PollInterval: config.Duration(time.Minute),
Branches: []config.Pattern{{Raw: branch}},
Name: name,
URL: "https://example.com/" + name + ".git",
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/gsync/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func TestSSHHostPort(t *testing.T) {
{"ssh://git@git.example.com:2223/foo/bar.git", "git.example.com:2223"},
{"ssh://git@github.com/foo/bar.git", "github.com:22"},
{"git@github.com:foo/bar.git", "github.com:22"},
{"git@192.168.1.10:repo.git", "192.168.1.10:22"},
{"ssh://git@[2001:db8::1]:2222/repo.git", "[2001:db8::1]:2222"},
}
for _, tc := range cases {
t.Run(tc.url, func(t *testing.T) {
Expand Down Expand Up @@ -193,6 +195,12 @@ func TestMergeAlgorithms(t *testing.T) {
fallback: []string{"y", "z"},
want: []string{"x", "y", "z"},
},
{
name: "multiple overlapping hostAlgos and fallback",
hostAlgos: []string{"a", "c"},
fallback: []string{"a", "b", "c", "d"},
want: []string{"a", "c", "b", "d"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
42 changes: 19 additions & 23 deletions pkg/gsync/openvox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func TestEnsureClonedOpenVox_RecreatesNonRepoDir(t *testing.T) {
}

repoCfg := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{LocalPath: localPath},
Name: DefaultTestName,
URL: "https://example.com/repo.git",
LocalPath: localPath,
Name: DefaultTestName,
URL: "https://example.com/repo.git",
}

r, err := getRepoWithSharedCache(localPath, filepath.Join(basePath, ".git", "cache.git"), "https://example.com/repo.git", nil)
Expand Down Expand Up @@ -99,12 +99,10 @@ func TestEnsureProductionAlias(t *testing.T) {
openVox := true
productionAlias := true
repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{
LocalPath: basePath,
OpenVox: &openVox,
ProductionAlias: &productionAlias,
},
Name: DefaultTestName,
LocalPath: basePath,
OpenVox: &openVox,
ProductionAlias: &productionAlias,
Name: DefaultTestName,
}

ensureProductionAlias(context.Background(), repo, testDefaultBranch, map[string]struct{}{testDefaultBranch: {}})
Expand Down Expand Up @@ -135,12 +133,10 @@ func TestEnsureProductionAlias_SkipsWhenProductionBranchExists(t *testing.T) {
openVox := true
productionAlias := true
repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{
LocalPath: basePath,
OpenVox: &openVox,
ProductionAlias: &productionAlias,
},
Name: DefaultTestName,
LocalPath: basePath,
OpenVox: &openVox,
ProductionAlias: &productionAlias,
Name: DefaultTestName,
}

ensureProductionAlias(context.Background(), repo, testDefaultBranch, map[string]struct{}{testDefaultBranch: {}, productionAliasName: {}})
Expand Down Expand Up @@ -477,8 +473,8 @@ func TestPruneStaleOpenVoxDirs(t *testing.T) {
}

repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{LocalPath: basePath},
Name: DefaultTestName,
LocalPath: basePath,
Name: DefaultTestName,
}

result := &Result{RepoName: DefaultTestName}
Expand Down Expand Up @@ -528,8 +524,8 @@ func TestPruneStaleOpenVoxDirs_DryRun(t *testing.T) {
}

repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{LocalPath: basePath},
Name: DefaultTestName,
LocalPath: basePath,
Name: DefaultTestName,
}

result := &Result{RepoName: DefaultTestName}
Expand Down Expand Up @@ -566,8 +562,8 @@ func TestPruneStaleOpenVoxDirs_LeavesLockFileForOrphanCleanup(t *testing.T) {
}

repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{LocalPath: basePath},
Name: DefaultTestName,
LocalPath: basePath,
Name: DefaultTestName,
}

result := &Result{RepoName: DefaultTestName}
Expand All @@ -592,8 +588,8 @@ func TestPruneStaleOpenVoxDirs_MissingDir(t *testing.T) {
}

repo := &config.RepoConfig{
RepoDefaults: config.RepoDefaults{LocalPath: basePath},
Name: DefaultTestName,
LocalPath: basePath,
Name: DefaultTestName,
}

result := &Result{RepoName: DefaultTestName}
Expand Down
25 changes: 25 additions & 0 deletions pkg/gsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"os"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -123,12 +124,31 @@ func (s *Syncer) SyncRepo(ctx context.Context, repo *config.RepoConfig, opts Syn

refs, err := listRemoteRefs(ctx, r, auth, repo.Name, "standard")
if err != nil {
if strings.Contains(err.Error(), "empty") {
log.Info("remote repository is empty; skipping sync and checkout")
duration := time.Since(start)
telemetry.SyncDurationSeconds.WithLabelValues(repo.Name, "total").Observe(duration.Seconds())
logSyncSuccess(ctx, result, duration)
telemetry.SyncSuccessTotal.WithLabelValues(repo.Name).Inc()
telemetry.LastSuccessTimestamp.WithLabelValues(repo.Name).Set(float64(time.Now().Unix()))
return result
}
log.Error("failed to list remote refs", "error", err)
telemetry.SyncFailuresTotal.WithLabelValues(repo.Name, "clone").Inc()
result.Err = fmt.Errorf("listing remote refs: %w", err)
return result
}

if len(refs) == 0 {
log.Info("remote repository is empty; skipping sync and checkout")
duration := time.Since(start)
telemetry.SyncDurationSeconds.WithLabelValues(repo.Name, "total").Observe(duration.Seconds())
logSyncSuccess(ctx, result, duration)
telemetry.SyncSuccessTotal.WithLabelValues(repo.Name).Inc()
telemetry.LastSuccessTimestamp.WithLabelValues(repo.Name).Set(float64(time.Now().Unix()))
return result
}

defaultBranch, _, matchedBranches, matchedTagRefs := extractRemoteRefState(refs, repo.Branches, repo.Tags)
matchedTags := make([]string, 0, len(matchedTagRefs))
for _, tagRef := range matchedTagRefs {
Expand Down Expand Up @@ -408,6 +428,11 @@ func (s *Syncer) handleCheckout(r *git.Repository, repo *config.RepoConfig, defa

err := checkoutRef(r, repo.Checkout)
if err != nil {
if _, headErr := r.Head(); headErr != nil {
slog.Info("skipping checkout: repository has no commits or HEAD yet", "ref", repo.Checkout)
return
}

if defaultBranch == "" || repo.Checkout == defaultBranch {
slog.Error("failed to checkout", "ref", repo.Checkout, "error", err)
s.setErr(result, fmt.Errorf("checkout %s: %w", repo.Checkout, err))
Expand Down
Loading