-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support git repo caching on package create (#785)
## Description Add the ability to locally cache large git repositories when pulling them with Zarf ## Related Issue Fixes #750 ## Type of change - [X] New feature (non-breaking change which adds functionality) ## Checklist before merging - [x] Tests have been added/updated as necessary (add the `needs-tests` label) - [x] Documentation has been updated as necessary (add the `needs-docs` label) - [x] An ADR has been written as necessary (add the `needs-adr` label) [ [1](https://github.com/joelparkerhenderson/architecture-decision-record) [2](https://cognitect.com/blog/2011/11/15/documenting-architecture-decisions) [3](https://adr.github.io/) ] - [x] (Optional) Changes have been linted locally with [golangci-lint](https://github.com/golangci/golangci-lint). (NOTE: We haven't turned on lint checks in the pipeline yet so linting may be hard if it shows a lot of lint errors in places that weren't touched by changes. Thus, linting is optional right now.) Co-authored-by: Jon Perry <[email protected]> Co-authored-by: Megamind <[email protected]>
- Loading branch information
1 parent
4936e24
commit 1f0dbf1
Showing
30 changed files
with
294 additions
and
125 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
27 changes: 27 additions & 0 deletions
27
docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_tools_clear-cache.md
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,27 @@ | ||
## zarf tools clear-cache | ||
|
||
Clears the configured git and image cache directory | ||
|
||
``` | ||
zarf tools clear-cache [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for clear-cache | ||
--zarf-cache string Specify the location of the Zarf artifact cache (images and git repositories) (default "~/.zarf-cache") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-a, --architecture string Architecture for OCI images | ||
-l, --log-level string Log level when running Zarf. Valid options are: warn, info, debug, trace | ||
--no-progress Disable fancy UI progress bars, spinners, logos, etc. | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [zarf tools](zarf_tools.md) - Collection of additional tools to make airgap easier | ||
|
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 |
---|---|---|
|
@@ -17,3 +17,5 @@ components: | |
- https://github.com/stefanprodan/podinfo.git | ||
# Clone an azure repo that breaks in go-git and has to fall back to the host git | ||
- https://[email protected]/me0515/zarf-public-test/_git/zarf-public-test | ||
# Clone an azure repo (w/SHA) that breaks in go-git and has to fall back to the host git | ||
- https://[email protected]/me0515/zarf-public-test/_git/zarf-public-test@524980951ff16e19dc25232e9aea8fd693989ba6 |
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,66 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/defenseunicorns/zarf/src/internal/message" | ||
"github.com/defenseunicorns/zarf/src/internal/utils" | ||
"github.com/go-git/go-git/v5" | ||
) | ||
|
||
// clone performs a `git clone` of a given repo. | ||
func clone(gitDirectory string, gitURL string, onlyFetchRef bool, spinner *message.Spinner) (*git.Repository, error) { | ||
cloneOptions := &git.CloneOptions{ | ||
URL: gitURL, | ||
Progress: spinner, | ||
RemoteName: onlineRemoteName, | ||
} | ||
|
||
if onlyFetchRef { | ||
cloneOptions.Tags = git.NoTags | ||
} | ||
|
||
gitCred := FindAuthForHost(gitURL) | ||
|
||
// Gracefully handle no git creds on the system (like our CI/CD) | ||
if gitCred.Auth.Username != "" { | ||
cloneOptions.Auth = &gitCred.Auth | ||
} | ||
|
||
// Clone the given repo | ||
repo, err := git.PlainClone(gitDirectory, false, cloneOptions) | ||
|
||
if errors.Is(err, git.ErrRepositoryAlreadyExists) { | ||
repo, err = git.PlainOpen(gitDirectory) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return repo, git.ErrRepositoryAlreadyExists | ||
} else if err != nil { | ||
spinner.Debugf("Failed to clone repo: %s", err) | ||
message.Infof("Falling back to host git for %s", gitURL) | ||
|
||
// 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 | ||
cmdArgs := []string{"clone", "--origin", onlineRemoteName, gitURL, gitDirectory} | ||
|
||
if onlyFetchRef { | ||
cmdArgs = append(cmdArgs, "--no-tags") | ||
} | ||
|
||
stdOut, stdErr, err := utils.ExecCommandWithContext(context.TODO(), false, "git", cmdArgs...) | ||
spinner.Updatef(stdOut) | ||
spinner.Debugf(stdErr) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return git.PlainOpen(gitDirectory) | ||
} else { | ||
return repo, 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
Oops, something went wrong.