Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --default-branch-only cmdline option #161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ When there are machines which have access to both the public internet and the GH

- `cache-dir` _(required)_
The directory in which to cache repositories as they are synced. This speeds up re-syncing.
- `default-branch-only` _(optional)_
Optionally if you wish to only synchronize the single default branch rather than the default behaviour of syncing all branches.
- `destination-url` _(required)_
The URL of the GHES instance to sync repositories onto.
- `destination-token` _(required)_
Expand Down Expand Up @@ -65,6 +67,8 @@ When no machine has access to both the public internet and the GHES instance:

- `cache-dir` _(required)_
The directory to cache the pulled repositories into.
- `default-branch-only` _(optional)_
Optionally if you wish to only synchronize the single default branch rather than the default behaviour of syncing all branches.
- `repo-name` _(optional)_
A single repository to be synced. In the format of `owner/repo`. Optionally if you wish the repository to be named different on your GHES instance you can provide an alias in the format: `upstream_owner/upstream_repo:destination_owner/destination_repo`
- `repo-name-list` _(optional)_
Expand Down
26 changes: 18 additions & 8 deletions src/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/spf13/cobra"
)

type PullOnlyFlags struct {
SourceURL string
DefaultBranchOnly bool
SourceURL string
}

type PullFlags struct {
Expand All @@ -27,6 +29,7 @@ func (f *PullFlags) Init(cmd *cobra.Command) {
}

func (f *PullOnlyFlags) Init(cmd *cobra.Command) {
cmd.Flags().BoolVar(&f.DefaultBranchOnly, "default-branch-only", false, "Only pull the default branch")
cmd.Flags().StringVar(&f.SourceURL, "source-url", "https://github.com", "The domain to pull from")
}

Expand All @@ -45,24 +48,25 @@ func Pull(ctx context.Context, flags *PullFlags) error {
return err
}

return PullManyWithGitImpl(ctx, flags.SourceURL, flags.CacheDir, repoNames, gitImplementation{})
return PullManyWithGitImpl(ctx, flags.SourceURL, flags.CacheDir, flags.DefaultBranchOnly, repoNames, gitImplementation{})
}

func PullManyWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoNames []string, gitimpl GitImplementation) error {
func PullManyWithGitImpl(ctx context.Context, sourceURL, cacheDir string, defaultBranchOnly bool, repoNames []string, gitimpl GitImplementation) error {
for _, repoName := range repoNames {
if err := PullWithGitImpl(ctx, sourceURL, cacheDir, repoName, gitimpl); err != nil {
if err := PullWithGitImpl(ctx, sourceURL, cacheDir, defaultBranchOnly, repoName, gitimpl); err != nil {
return err
}
}
return nil
}

func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName string, gitimpl GitImplementation) error {
func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, defaultBranchOnly bool, repoName string, gitimpl GitImplementation) error {
originRepoName, destRepoName, err := extractSourceDest(repoName)
if err != nil {
return err
}

_ = os.MkdirAll(cacheDir, 0755)
_, err = os.Stat(cacheDir)
if err != nil {
return err
Expand All @@ -73,8 +77,9 @@ func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName s
if !gitimpl.RepositoryExists(dst) {
fmt.Fprintf(os.Stdout, "pulling %s to %s ...\n", originRepoName, dst)
_, err := gitimpl.CloneRepository(dst, &git.CloneOptions{
SingleBranch: false,
URL: fmt.Sprintf("%s/%s", sourceURL, originRepoName),
ReferenceName: plumbing.HEAD,
SingleBranch: defaultBranchOnly,
URL: fmt.Sprintf("%s/%s", sourceURL, originRepoName),
})
if err != nil {
if strings.Contains(err.Error(), "authentication required") {
Expand All @@ -89,10 +94,15 @@ func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName s
return err
}

refspec := config.RefSpec("+refs/heads/*:refs/heads/*")
if defaultBranchOnly {
refspec = "+refs/tags/*:refs/tags/*"
}

fmt.Fprintf(os.Stdout, "fetching * refs for %s ...\n", originRepoName)
err = repo.FetchContext(ctx, &git.FetchOptions{
RefSpecs: []config.RefSpec{
config.RefSpec("+refs/heads/*:refs/heads/*"),
refspec,
},
Tags: git.AllTags,
})
Expand Down