From 935af59cf64fbe49eb8baba9fe80e6b236daf593 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 19 Jan 2022 15:51:13 +0100 Subject: Repository: don't crash accessing invalid pathinfo (#443) When fs.Stat returns an error, pathinfo may be nil. In such situations the only safe response seems to be to return the error to the caller. Without this fix, accessing pathinfo.IsDir() below would lead to a crash dereferencing a nil pointer. This crash can be reproduced by trying to initialize a Git repo with an invalid path name. Also see: https://github.com/muesli/gitty/issues/36 --- repository_test.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index e284df8..7a9db15 100644 --- a/repository_test.go +++ b/repository_test.go @@ -2948,6 +2948,11 @@ func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) { c.Assert(err, IsNil) } +func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) { + _, _, err := dotGitToOSFilesystems("\000", false) + c.Assert(err, NotNil) +} + func BenchmarkObjects(b *testing.B) { defer fixtures.Clean() -- cgit From 3486338715d0c1385992c6ca8db6bd04fd0df135 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Thu, 2 Mar 2023 20:01:04 +0000 Subject: *: Fix panic for empty revisions Signed-off-by: Paulo Gomes --- repository_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 7a9db15..468ce33 100644 --- a/repository_test.go +++ b/repository_test.go @@ -2953,6 +2953,15 @@ func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) { c.Assert(err, NotNil) } +func (s *RepositorySuite) TestIssue674(c *C) { + r, _ := Init(memory.NewStorage(), nil) + h, err := r.ResolveRevision(plumbing.Revision("")) + + c.Assert(err, NotNil) + c.Assert(h, NotNil) + c.Check(h.IsZero(), Equals, true) +} + func BenchmarkObjects(b *testing.B) { defer fixtures.Clean() -- cgit From 9a5b08f5c32bad31a35a53c045ebf6c8409f8b2c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 4 Apr 2023 01:13:21 -0400 Subject: feat(clone): add mirror clone option Clone remote as a mirror. This fetches all remote refs, implies bare repository, and sets the appropriate configs. Fixes: https://github.com/go-git/go-git/issues/293 Update options.go Co-authored-by: Paulo Gomes Signed-off-by: Ayman Bagabas --- repository_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 468ce33..ed3e7e6 100644 --- a/repository_test.go +++ b/repository_test.go @@ -189,6 +189,35 @@ func (s *RepositorySuite) TestCloneContext(c *C) { c.Assert(err, Equals, context.Canceled) } +func (s *RepositorySuite) TestCloneMirror(c *C) { + r, err := Clone(memory.NewStorage(), nil, &CloneOptions{ + URL: fixtures.Basic().One().URL, + Mirror: true, + }) + + c.Assert(err, IsNil) + + refs, err := r.References() + var count int + refs.ForEach(func(r *plumbing.Reference) error { c.Log(r); count++; return nil }) + c.Assert(err, IsNil) + // 6 refs total from github.com/git-fixtures/basic.git: + // - HEAD + // - refs/heads/master + // - refs/heads/branch + // - refs/pull/1/head + // - refs/pull/2/head + // - refs/pull/2/merge + c.Assert(count, Equals, 6) + + cfg, err := r.Config() + c.Assert(err, IsNil) + + c.Assert(cfg.Core.IsBare, Equals, true) + c.Assert(cfg.Remotes[DefaultRemoteName].Validate(), IsNil) + c.Assert(cfg.Remotes[DefaultRemoteName].Mirror, Equals, true) +} + func (s *RepositorySuite) TestCloneWithTags(c *C) { url := s.GetLocalRepositoryURL( fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), -- cgit From a4b11abc55bf88fbd07a00a5985a34750bee1d72 Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Wed, 3 May 2023 14:01:24 +1000 Subject: git: fix cloning with branch name Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> --- repository_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index ed3e7e6..0080a83 100644 --- a/repository_test.go +++ b/repository_test.go @@ -1020,6 +1020,14 @@ func (s *RepositorySuite) TestCloneConfig(c *C) { } func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { + s.testCloneSingleBranchAndNonHEADReference(c, "refs/heads/branch") +} + +func (s *RepositorySuite) TestCloneSingleBranchAndNonHEADAndNonFull(c *C) { + s.testCloneSingleBranchAndNonHEADReference(c, "branch") +} + +func (s *RepositorySuite) testCloneSingleBranchAndNonHEADReference(c *C, ref string) { r, _ := Init(memory.NewStorage(), nil) head, err := r.Head() @@ -1028,7 +1036,7 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { err = r.clone(context.Background(), &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), - ReferenceName: plumbing.ReferenceName("refs/heads/branch"), + ReferenceName: plumbing.ReferenceName(ref), SingleBranch: true, }) -- cgit From b8dd39abcc9dd35808ec625a90a68c0ace4973e4 Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Thu, 11 May 2023 11:17:04 +1000 Subject: git: Testing, Fix tests not cleaning temp folders Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> --- repository_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 0080a83..45af396 100644 --- a/repository_test.go +++ b/repository_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -60,17 +59,18 @@ func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { fs := osfs.New(dir) dot, _ := fs.Chroot("storage") - storage := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) + st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) wt, _ := fs.Chroot("worktree") - r, err := Init(storage, wt) + r, err := Init(st, wt) c.Assert(err, IsNil) c.Assert(r, NotNil) f, err := fs.Open(fs.Join("worktree", ".git")) c.Assert(err, IsNil) + defer func() { _ = f.Close() }() - all, err := ioutil.ReadAll(f) + all, err := io.ReadAll(f) c.Assert(err, IsNil) c.Assert(string(all), Equals, fmt.Sprintf("gitdir: %s\n", filepath.Join("..", "storage"))) @@ -85,9 +85,9 @@ func (s *RepositorySuite) TestInitStandardDotGit(c *C) { fs := osfs.New(dir) dot, _ := fs.Chroot(".git") - storage := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) + st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) - r, err := Init(storage, fs) + r, err := Init(st, fs) c.Assert(err, IsNil) c.Assert(r, NotNil) @@ -3009,14 +3009,14 @@ func BenchmarkObjects(b *testing.B) { b.Run(f.URL, func(b *testing.B) { fs := f.DotGit() - storer := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) + st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) worktree, err := fs.Chroot(filepath.Dir(fs.Root())) if err != nil { b.Fatal(err) } - repo, err := Open(storer, worktree) + repo, err := Open(st, worktree) if err != nil { b.Fatal(err) } @@ -3046,7 +3046,7 @@ func BenchmarkObjects(b *testing.B) { func BenchmarkPlainClone(b *testing.B) { for i := 0; i < b.N; i++ { - t, err := ioutil.TempDir("", "") + t, err := os.MkdirTemp("", "") if err != nil { b.Fatal(err) } -- cgit From 1aa8e8940336aa80eccdd8dd9b46b0e6547e7127 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Sun, 21 May 2023 02:24:18 -0400 Subject: git: Allow Initial Branch to be configurable --- repository_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 0080a83..f992672 100644 --- a/repository_test.go +++ b/repository_test.go @@ -52,6 +52,54 @@ func (s *RepositorySuite) TestInit(c *C) { cfg, err := r.Config() c.Assert(err, IsNil) c.Assert(cfg.Core.IsBare, Equals, false) + + // check the HEAD to see what the default branch is + createCommit(c, r) + ref, err := r.Head() + c.Assert(err, IsNil) + c.Assert(ref.Name().String(), Equals, plumbing.Master.String()) +} + +func (s *RepositorySuite) TestInitWithOptions(c *C) { + r, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{ + DefaultBranch: "refs/heads/foo", + }) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + createCommit(c, r) + + ref, err := r.Head() + c.Assert(err, IsNil) + c.Assert(ref.Name().String(), Equals, "refs/heads/foo") + +} + +func createCommit(c *C, r *Repository) { + // Create a commit so there is a HEAD to check + wt, err := r.Worktree() + c.Assert(err, IsNil) + + rm, err := wt.Filesystem.Create("foo.txt") + c.Assert(err, IsNil) + + _, err = rm.Write([]byte("foo text")) + c.Assert(err, IsNil) + + _, err = wt.Add("foo.txt") + c.Assert(err, IsNil) + + author := object.Signature{ + Name: "go-git", + Email: "go-git@fake.local", + When: time.Now(), + } + _, err = wt.Commit("test commit message", &CommitOptions{ + All: true, + Author: &author, + Committer: &author, + }) + c.Assert(err, IsNil) + } func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { -- cgit From 5889b758b390cba1b01db6684eb83760fd7fb58c Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Sun, 4 Jun 2023 11:28:26 +1000 Subject: git: Clone HEAD should not force master. Fixes #363 Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> --- repository_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 965f028..bcfaa93 100644 --- a/repository_test.go +++ b/repository_test.go @@ -1119,6 +1119,49 @@ func (s *RepositorySuite) testCloneSingleBranchAndNonHEADReference(c *C, ref str c.Assert(branch.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") } +func (s *RepositorySuite) TestCloneSingleBranchHEADMain(c *C) { + r, _ := Init(memory.NewStorage(), nil) + + head, err := r.Head() + c.Assert(err, Equals, plumbing.ErrReferenceNotFound) + c.Assert(head, IsNil) + + err = r.clone(context.Background(), &CloneOptions{ + URL: s.GetLocalRepositoryURL(fixtures.ByTag("no-master-head").One()), + SingleBranch: true, + }) + + c.Assert(err, IsNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) + + cfg, err := r.Config() + c.Assert(err, IsNil) + c.Assert(cfg.Branches, HasLen, 1) + c.Assert(cfg.Branches["main"].Name, Equals, "main") + c.Assert(cfg.Branches["main"].Remote, Equals, "origin") + c.Assert(cfg.Branches["main"].Merge, Equals, plumbing.ReferenceName("refs/heads/main")) + + head, err = r.Reference(plumbing.HEAD, false) + c.Assert(err, IsNil) + c.Assert(head, NotNil) + c.Assert(head.Type(), Equals, plumbing.SymbolicReference) + c.Assert(head.Target().String(), Equals, "refs/heads/main") + + branch, err := r.Reference(head.Target(), false) + c.Assert(err, IsNil) + c.Assert(branch, NotNil) + c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f") + + branch, err = r.Reference("refs/remotes/origin/HEAD", false) + c.Assert(err, IsNil) + c.Assert(branch, NotNil) + c.Assert(branch.Type(), Equals, plumbing.HashReference) + c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f") +} + func (s *RepositorySuite) TestCloneSingleBranch(c *C) { r, _ := Init(memory.NewStorage(), nil) @@ -1154,12 +1197,6 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) { c.Assert(err, IsNil) c.Assert(branch, NotNil) c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") - - branch, err = r.Reference("refs/remotes/origin/master", false) - c.Assert(err, IsNil) - c.Assert(branch, NotNil) - c.Assert(branch.Type(), Equals, plumbing.HashReference) - c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } func (s *RepositorySuite) TestCloneSingleTag(c *C) { -- cgit From 2d6af16bf6d051cb3014c9970f3ea813e54f73b0 Mon Sep 17 00:00:00 2001 From: "matej.risek" Date: Tue, 9 May 2023 11:54:55 +0200 Subject: git: add a clone option to allow for shallow cloning of submodules This option matches the git clone option --shallow-submodules. https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---no-shallow-submodules --- repository_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 0080a83..79a884b 100644 --- a/repository_test.go +++ b/repository_test.go @@ -884,6 +884,43 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) { c.Assert(cfg.Submodules, HasLen, 2) } +func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) { + if testing.Short() { + c.Skip("skipping test in short mode.") + } + + dir, clean := s.TemporalDir() + defer clean() + + path := fixtures.ByTag("submodule").One().Worktree().Root() + mainRepo, err := PlainClone(dir, false, &CloneOptions{ + URL: path, + RecurseSubmodules: 1, + ShallowSubmodules: true, + }) + c.Assert(err, IsNil) + + mainWorktree, err := mainRepo.Worktree() + c.Assert(err, IsNil) + + submodule, err := mainWorktree.Submodule("basic") + c.Assert(err, IsNil) + + subRepo, err := submodule.Repository() + c.Assert(err, IsNil) + + lr, err := subRepo.Log(&LogOptions{}) + c.Assert(err, IsNil) + + commitCount := 0 + for _, err := lr.Next(); err == nil; _, err = lr.Next() { + commitCount++ + } + c.Assert(err, IsNil) + + c.Assert(commitCount, Equals, 1) +} + func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) { dir, clean := s.TemporalDir() defer clean() -- cgit From a9a658ee4ecb0c2f0fcb1962b7c6a408a8116309 Mon Sep 17 00:00:00 2001 From: ricci2511 Date: Fri, 7 Jul 2023 14:44:54 +0200 Subject: *: Add test for paths starting with tilde --- repository_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index d18816f..50384e7 100644 --- a/repository_test.go +++ b/repository_test.go @@ -543,6 +543,21 @@ func (s *RepositorySuite) TestPlainOpen(c *C) { c.Assert(r, NotNil) } +func (s *RepositorySuite) TestPlainOpenTildePath(c *C) { + dir, clean := s.TemporalHomeDir() + defer clean() + + r, err := PlainInit(dir, false) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~/", 1) + + r, err = PlainOpen(path) + c.Assert(err, IsNil) + c.Assert(r, NotNil) +} + func (s *RepositorySuite) TestPlainOpenBare(c *C) { dir, clean := s.TemporalDir() defer clean() -- cgit From 5dad9b23030e344a4fd1458df0c50e6ada55a01a Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Sun, 9 Jul 2023 17:08:04 +1000 Subject: *: Handle paths starting with ~username Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> --- repository_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'repository_test.go') diff --git a/repository_test.go b/repository_test.go index 50384e7..9e000a3 100644 --- a/repository_test.go +++ b/repository_test.go @@ -8,6 +8,7 @@ import ( "io" "os" "os/exec" + "os/user" "path/filepath" "regexp" "strings" @@ -551,11 +552,19 @@ func (s *RepositorySuite) TestPlainOpenTildePath(c *C) { c.Assert(err, IsNil) c.Assert(r, NotNil) - path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~/", 1) - - r, err = PlainOpen(path) + currentUser, err := user.Current() c.Assert(err, IsNil) - c.Assert(r, NotNil) + // remove domain for windows + username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:] + + homes := []string{"~/", "~" + username + "/"} + for _, home := range homes { + path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], home, 1) + + r, err = PlainOpen(path) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + } } func (s *RepositorySuite) TestPlainOpenBare(c *C) { -- cgit