| 26 | ) |
| 27 | |
| 28 | func TestRepository(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | ctx := testutil.TestContext(t) |
| 31 | |
| 32 | cfg := gitkit.Config{ |
| 33 | Dir: t.TempDir(), |
| 34 | AutoCreate: true, |
| 35 | } |
| 36 | gitSrv := gitkit.New(cfg) |
| 37 | err := gitSrv.Setup() |
| 38 | require.NoError(t, err) |
| 39 | srv := httptest.NewServer(http.HandlerFunc(gitSrv.ServeHTTP)) |
| 40 | t.Cleanup(func() { |
| 41 | srv.Close() |
| 42 | }) |
| 43 | |
| 44 | rootPath := t.TempDir() |
| 45 | repoName := "test" |
| 46 | repoAddress := fmt.Sprintf("%s/%s.git", srv.URL, repoName) |
| 47 | checksum := helpers.GetCRCHash(repoAddress) |
| 48 | expectedPath := fmt.Sprintf("%s-%d", repoName, checksum) |
| 49 | |
| 50 | storer := memory.NewStorage() |
| 51 | fs := memfs.New() |
| 52 | options := git.InitOptions{ |
| 53 | DefaultBranch: plumbing.Main, |
| 54 | } |
| 55 | initRepo, err := git.InitWithOptions(storer, fs, options) |
| 56 | require.NoError(t, err) |
| 57 | w, err := initRepo.Worktree() |
| 58 | require.NoError(t, err) |
| 59 | filePath := "test.txt" |
| 60 | newFile, err := fs.Create(filePath) |
| 61 | require.NoError(t, err) |
| 62 | _, err = newFile.Write([]byte("Hello World")) |
| 63 | require.NoError(t, err) |
| 64 | err = newFile.Close() |
| 65 | require.NoError(t, err) |
| 66 | _, err = w.Add(filePath) |
| 67 | require.NoError(t, err) |
| 68 | _, err = w.Commit("Initial commit", &git.CommitOptions{ |
| 69 | Author: &object.Signature{ |
| 70 | Email: "example@example.com", |
| 71 | }, |
| 72 | }) |
| 73 | require.NoError(t, err) |
| 74 | _, err = initRepo.CreateRemote(&config.RemoteConfig{ |
| 75 | Name: "origin", |
| 76 | URLs: []string{repoAddress}, |
| 77 | }) |
| 78 | require.NoError(t, err) |
| 79 | err = initRepo.Push(&git.PushOptions{ |
| 80 | RemoteName: "origin", |
| 81 | }) |
| 82 | require.NoError(t, err) |
| 83 | |
| 84 | // TODO: Is there a configuration that defines contents of HEAD that isn't read from ~/.gitconfig |
| 85 | // Force-write refs/heads/main ref to HEAD to disk - Matching the above reference and decoupling from host gitconfig |