-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: git package to be thread safe and not depend on types package
Signed-off-by: Philip Laine <philip.laine@gmail.com>
- Loading branch information
1 parent
6d86e48
commit 3d23cc2
Showing
17 changed files
with
505 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package git contains functions for interacting with git repositories. | ||
package git | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/go-git/go-git/v5/plumbing" | ||
|
||
"github.com/zarf-dev/zarf/src/pkg/utils/exec" | ||
) | ||
|
||
// gitCloneFallback is a fallback if go-git fails to clone a repo. | ||
func (g *Repository) gitCloneFallback(ctx context.Context, gitURL string, ref plumbing.ReferenceName, shallow bool) error { | ||
// If we can't clone with go-git, fallback to the host clone | ||
// Only support "all tags" due to the azure clone url format including a username | ||
cloneArgs := []string{"clone", "--origin", onlineRemoteName, gitURL, g.path} | ||
// Don't clone all tags / refs if we're cloning a specific tag or branch. | ||
if ref.IsTag() || ref.IsBranch() { | ||
cloneArgs = append(cloneArgs, "--no-tags") | ||
cloneArgs = append(cloneArgs, "-b", ref.Short()) | ||
cloneArgs = append(cloneArgs, "--single-branch") | ||
} | ||
// If this is a shallow clone set the depth to 1 | ||
if shallow { | ||
cloneArgs = append(cloneArgs, "--depth", "1") | ||
} | ||
cloneExecConfig := exec.Config{ | ||
Stdout: io.Discard, | ||
Stderr: io.Discard, | ||
} | ||
_, _, err := exec.CmdWithContext(ctx, cloneExecConfig, "git", cloneArgs...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// If we're cloning the whole repo, we need to also fetch the other branches besides the default. | ||
if ref == emptyRef { | ||
fetchArgs := []string{"fetch", "--tags", "--update-head-ok", onlineRemoteName, "refs/*:refs/*"} | ||
fetchExecConfig := exec.Config{ | ||
Stdout: io.Discard, | ||
Stderr: io.Discard, | ||
Dir: g.path, | ||
} | ||
_, _, err := exec.CmdWithContext(ctx, fetchExecConfig, "git", fetchArgs...) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package git contains functions for interacting with git repositories. | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
const onlineRemoteName = "online-upstream" | ||
const offlineRemoteName = "offline-downstream" | ||
const emptyRef = "" | ||
|
||
// ParseRef parses the provided ref into a ReferenceName if it's not a hash. | ||
func ParseRef(r string) plumbing.ReferenceName { | ||
// If not a full ref, assume it's a tag at this point. | ||
if !plumbing.IsHash(r) && !strings.HasPrefix(r, "refs/") { | ||
r = fmt.Sprintf("refs/tags/%s", r) | ||
} | ||
// Set the reference name to the provided ref. | ||
return plumbing.ReferenceName(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package git | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseRef(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
refPlain string | ||
expectedRef plumbing.ReferenceName | ||
}{ | ||
{ | ||
name: "basic", | ||
refPlain: "v1.0.0", | ||
expectedRef: plumbing.ReferenceName("refs/tags/v1.0.0"), | ||
}, | ||
{ | ||
name: "basic", | ||
refPlain: "refs/heads/branchname", | ||
expectedRef: plumbing.ReferenceName("refs/heads/branchname"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ref := ParseRef(tt.refPlain) | ||
require.Equal(t, tt.expectedRef, ref) | ||
}) | ||
} | ||
} |
Oops, something went wrong.