diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f702525..3ad08c3 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -47,7 +47,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Upload a Build Artifact - if: startsWith(github.ref, 'refs/tags/') + if: github.event_name == 'pull_request' || startsWith(github.ref, 'refs/tags/') uses: actions/upload-artifact@v7 with: name: gickup diff --git a/zip/zip.go b/zip/zip.go index a876508..986f613 100644 --- a/zip/zip.go +++ b/zip/zip.go @@ -30,19 +30,24 @@ func Zip(repository string, tozip []string) error { if err != nil { return err } + abspath, err := filepath.Rel(parentDir, path) + if err != nil { + return err + } + abspath = filepath.ToSlash(abspath) if info.IsDir() { - return nil + // Packed refs can leave refs/ empty, but Git still requires the + // directory to recognize an extracted bare repository. + _, err := w.Create(abspath + "/") + return err } + file, err := os.Open(path) if err != nil { return err } defer file.Close() - abspath, err := filepath.Rel(parentDir, path) - if err != nil { - return err - } f, err := w.Create(abspath) if err != nil { return err diff --git a/zip/zip_test.go b/zip/zip_test.go index 61254d1..a2a84e4 100644 --- a/zip/zip_test.go +++ b/zip/zip_test.go @@ -2,9 +2,12 @@ package zip import ( archivezip "archive/zip" + "context" "io" "os" + "os/exec" "path/filepath" + "strings" "testing" ) @@ -21,6 +24,9 @@ func TestZipCreatesArchiveAndRemovesSources(t *testing.T) { if err := os.MkdirAll(issuesDir, 0o755); err != nil { t.Fatalf("mkdir issues: %v", err) } + if err := os.MkdirAll(filepath.Join(repoDir, "refs", "heads"), 0o755); err != nil { + t.Fatalf("mkdir empty directory: %v", err) + } if err := os.WriteFile(filepath.Join(repoDir, "README.md"), []byte("repo data"), 0o644); err != nil { t.Fatalf("write repo file: %v", err) @@ -49,6 +55,9 @@ func TestZipCreatesArchiveAndRemovesSources(t *testing.T) { entries := map[string]string{} for _, file := range reader.File { + if strings.HasSuffix(file.Name, "/") && !file.FileInfo().IsDir() { + t.Fatalf("entry %q is not marked as a directory", file.Name) + } opened, err := file.Open() if err != nil { t.Fatalf("open entry %s: %v", file.Name, err) @@ -70,4 +79,82 @@ func TestZipCreatesArchiveAndRemovesSources(t *testing.T) { if entries["repo.issues/1.json"] != `{"id":1}` { t.Fatalf("unexpected issue entry contents: %#v", entries) } + for _, name := range []string{"repo/", "repo/refs/", "repo/refs/heads/", "repo.issues/"} { + if _, ok := entries[name]; !ok { + t.Errorf("missing directory entry %q", name) + } + } +} + +func TestZipRestoresBareRepositoryWithPackedRefs(t *testing.T) { + t.Parallel() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git is required for the restore test") + } + root := t.TempDir() + runGit := func(args ...string) string { + t.Helper() + cmd := exec.CommandContext(context.TODO(), "git", args...) + cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL="+os.DevNull) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + return strings.TrimSpace(string(out)) + } + source := filepath.Join(root, "source") + repo := filepath.Join(root, "repo.git") + runGit("init", "--initial-branch=main", source) + runGit("-C", source, "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "-c", "commit.gpgsign=false", "commit", "--allow-empty", "-m", "test") + runGit("-C", source, "tag", "v1") + runGit("clone", "--mirror", source, repo) + runGit("-C", repo, "pack-refs", "--all", "--prune") + wantRefs := runGit("-C", repo, "show-ref") + for _, name := range []string{"heads", "tags"} { + entries, err := os.ReadDir(filepath.Join(repo, "refs", name)) + if err != nil || len(entries) != 0 { + t.Fatalf("expected empty refs/%s before archiving, got %v, %v", name, entries, err) + } + } + if err := Zip(repo, []string{repo}); err != nil { + t.Fatal(err) + } + reader, err := archivezip.OpenReader(repo + ".zip") + if err != nil { + t.Fatal(err) + } + defer reader.Close() + restoreDir := filepath.Join(root, "restored") + for _, entry := range reader.File { + dest := filepath.Join(restoreDir, filepath.FromSlash(entry.Name)) + if entry.FileInfo().IsDir() { + if err := os.MkdirAll(dest, 0o755); err != nil { + t.Fatal(err) + } + continue + } + if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { + t.Fatal(err) + } + r, err := entry.Open() + if err != nil { + t.Fatal(err) + } + data, err := io.ReadAll(r) + r.Close() + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(dest, data, 0o644); err != nil { + t.Fatal(err) + } + } + restored := filepath.Join(restoreDir, "repo.git") + if got := runGit("-C", restored, "rev-parse", "--is-bare-repository"); got != "true" { + t.Fatalf("restored repository is not bare: %s", got) + } + runGit("-C", restored, "fsck", "--full") + if got := runGit("-C", restored, "show-ref"); got != wantRefs { + t.Fatalf("restored refs = %q, want %q", got, wantRefs) + } }