tenseleyflow/shithub / 93270f2

Browse files

Use source remotes for submodule backfill

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
93270f214a7178454d6af97bd0a66f14076263e5
Parents
9b4cf0d
Tree
5084267

4 changed files

StatusFile+-
M internal/repos/git/remotes.go 9 1
M internal/web/handlers/repo/code_tree_rows_test.go 15 0
A internal/web/handlers/repo/source_remote_test.go 32 0
M internal/web/handlers/repo/submodule_links.go 58 4
internal/repos/git/remotes.gomodified
@@ -6,6 +6,7 @@ import (
66
 	"context"
77
 	"errors"
88
 	"fmt"
9
+	"os"
910
 	"os/exec"
1011
 	"strings"
1112
 )
@@ -22,7 +23,9 @@ func FetchRemoteHeadsAndTags(ctx context.Context, gitDir, remoteURL string) erro
2223
 		return errors.New("git fetch: remoteURL is required")
2324
 	}
2425
 	//nolint:gosec // G204: gitDir is RepoFS-derived at call sites; remoteURL is caller-allowlisted and passed as argv, not shell.
25
-	cmd := exec.CommandContext(ctx, "git", "-C", gitDir,
26
+	cmd := exec.CommandContext(ctx, "git",
27
+		"-c", "protocol.ext.allow=never",
28
+		"-C", gitDir,
2629
 		"fetch",
2730
 		"--quiet",
2831
 		"--no-recurse-submodules",
@@ -30,6 +33,11 @@ func FetchRemoteHeadsAndTags(ctx context.Context, gitDir, remoteURL string) erro
3033
 		"refs/heads/*:refs/heads/*",
3134
 		"refs/tags/*:refs/tags/*",
3235
 	)
36
+	cmd.Env = append(os.Environ(),
37
+		"GIT_CONFIG_NOSYSTEM=1",
38
+		"GIT_CONFIG_GLOBAL=/dev/null",
39
+		"GIT_CONFIG_XDG=/dev/null",
40
+	)
3341
 	out, err := cmd.CombinedOutput()
3442
 	if err != nil {
3543
 		return fmt.Errorf("git fetch remote refs: %w (%s)", err, strings.TrimSpace(string(out)))
internal/web/handlers/repo/code_tree_rows_test.gomodified
@@ -201,3 +201,18 @@ func TestGitHubSubmoduleFetchURL_RejectsUnsupportedRemotes(t *testing.T) {
201201
 		})
202202
 	}
203203
 }
204
+
205
+func TestAppendSubmoduleBackfillCandidate_DedupesURLs(t *testing.T) {
206
+	t.Parallel()
207
+	seen := map[string]struct{}{}
208
+	var got []submoduleBackfillFetchCandidate
209
+	got = appendSubmoduleBackfillCandidate(got, seen, submoduleBackfillFetchCandidate{URL: " https://git.example.com/owner/repo.git ", SourceRepoID: 42})
210
+	got = appendSubmoduleBackfillCandidate(got, seen, submoduleBackfillFetchCandidate{URL: "https://git.example.com/owner/repo.git"})
211
+	got = appendSubmoduleBackfillCandidate(got, seen, submoduleBackfillFetchCandidate{})
212
+	if len(got) != 1 {
213
+		t.Fatalf("candidate count = %d, want 1", len(got))
214
+	}
215
+	if got[0].URL != "https://git.example.com/owner/repo.git" || got[0].SourceRepoID != 42 {
216
+		t.Fatalf("candidate = %+v", got[0])
217
+	}
218
+}
internal/web/handlers/repo/source_remote_test.goadded
@@ -0,0 +1,32 @@
1
+// SPDX-License-Identifier: AGPL-3.0-or-later
2
+
3
+package repo
4
+
5
+import (
6
+	"testing"
7
+
8
+	repogit "github.com/tenseleyFlow/shithub/internal/repos/git"
9
+)
10
+
11
+func TestChooseFetchedDefaultBranch(t *testing.T) {
12
+	t.Parallel()
13
+	branches := []repogit.RefEntry{
14
+		{Name: "feature", OID: "1111111111111111111111111111111111111111"},
15
+		{Name: "main", OID: "2222222222222222222222222222222222222222"},
16
+		{Name: "trunk", OID: "3333333333333333333333333333333333333333"},
17
+	}
18
+	name, oid := chooseFetchedDefaultBranch("trunk", branches)
19
+	if name != "trunk" || oid != branches[2].OID {
20
+		t.Fatalf("current default = %s %s, want trunk %s", name, oid, branches[2].OID)
21
+	}
22
+
23
+	name, oid = chooseFetchedDefaultBranch("missing", branches)
24
+	if name != "trunk" || oid != branches[2].OID {
25
+		t.Fatalf("fallback default = %s %s, want trunk %s", name, oid, branches[2].OID)
26
+	}
27
+
28
+	name, oid = chooseFetchedDefaultBranch("", []repogit.RefEntry{{Name: "zeta", OID: "aaaa"}})
29
+	if name != "zeta" || oid != "aaaa" {
30
+		t.Fatalf("first default = %s %s, want zeta aaaa", name, oid)
31
+	}
32
+}