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

v4: use repo default branch instead of master #451

Merged
merged 1 commit into from
Nov 9, 2021
Merged
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ docker run -d \
-u$(id -u):$(id -g) \
registry/git-sync:tag \
--repo=https://github.com/kubernetes/git-sync \
--branch=master \
--root=/tmp/git/root \
--period=30s

Expand Down Expand Up @@ -113,7 +112,7 @@ OPTIONS
"username=<value>" and "password=<value>".

--branch <string>, $GIT_SYNC_BRANCH
The git branch to check out. (default: master)
The git branch to check out. (default: <repo's default branch>)

--change-permissions <int>, $GIT_SYNC_PERMISSIONS
Optionally change permissions on the checked-out files to the
Expand Down Expand Up @@ -272,7 +271,7 @@ EXAMPLE USAGE

git-sync \
--repo=https://github.com/kubernetes/git-sync \
--branch=master \
--branch=main \
--rev=HEAD \
--period=10s \
--root=/mnt/git
Expand Down
34 changes: 21 additions & 13 deletions cmd/git-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ var flVerbose = pflag.IntP("verbose", "v", 0,

var flRepo = pflag.String("repo", envString("GIT_SYNC_REPO", ""),
"the git repository to clone")
var flBranch = pflag.String("branch", envString("GIT_SYNC_BRANCH", "master"),
"the git branch to check out")
var flBranch = pflag.String("branch", envString("GIT_SYNC_BRANCH", ""),
"the git branch to check out (defaults to repo's default branch)")
var flRev = pflag.String("rev", envString("GIT_SYNC_REV", "HEAD"),
"the git revision (tag or hash) to check out")
var flDepth = pflag.Int("depth", envInt("GIT_SYNC_DEPTH", 0),
Expand Down Expand Up @@ -124,7 +124,7 @@ var flCookieFile = pflag.Bool("cookie-file", envBool("GIT_COOKIE_FILE", false),
"use a git cookiefile (/etc/git-secret/cookie_file) for authentication")

var flAskPassURL = pflag.String("askpass-url", envString("GIT_ASKPASS_URL", ""),
"a URL to query for git credentials (username=<value> and password=<value>")
"a URL to query for git credentials (username=<value> and password=<value>)")

var flGitCmd = pflag.String("git", envString("GIT_SYNC_GIT", "git"),
"the git command to run (subject to PATH search, mostly for testing)")
Expand Down Expand Up @@ -881,7 +881,11 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
if git.depth != 0 {
args = append(args, "--depth", strconv.Itoa(git.depth))
}
args = append(args, "origin", git.branch)
fetch := "HEAD"
if git.branch != "" {
fetch = git.branch
}
args = append(args, "origin", fetch)

// Update from the remote.
if _, err := git.run.Run(ctx, git.root, git.cmd, args...); err != nil {
Expand All @@ -908,7 +912,7 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
}

_, err := git.run.Run(ctx, git.root, git.cmd, "worktree", "add", "--detach", worktreePath, hash, "--no-checkout")
git.log.V(0).Info("adding worktree", "path", worktreePath, "branch", fmt.Sprintf("origin/%s", git.branch))
git.log.V(0).Info("adding worktree", "path", worktreePath, "hash", hash)
if err != nil {
return err
}
Expand Down Expand Up @@ -1037,7 +1041,10 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error

// CloneRepo does an initial clone of the git repo.
func (git *repoSync) CloneRepo(ctx context.Context) error {
args := []string{"clone", "--no-checkout", "-b", git.branch}
args := []string{"clone", "--no-checkout"}
if git.branch != "" {
args = append(args, "-b", git.branch)
}
if git.depth != 0 {
args = append(args, "--depth", strconv.Itoa(git.depth))
}
Expand Down Expand Up @@ -1211,12 +1218,13 @@ func (git *repoSync) GetRevs(ctx context.Context) (string, string, error) {
return "", "", err
}

// Build a ref string, depending on whether the user asked to track HEAD or a tag.
ref := ""
if git.rev == "HEAD" {
ref = "refs/heads/" + git.branch
} else {
// Build a ref string, depending on whether the user asked to track HEAD or
// a tag.
ref := "HEAD"
if git.rev != "HEAD" {
ref = "refs/tags/" + git.rev
} else if git.branch != "" {
ref = "refs/heads/" + git.branch
}

// Figure out what hash the remote resolves ref to.
Expand Down Expand Up @@ -1560,7 +1568,7 @@ OPTIONS
"username=<value>" and "password=<value>".

--branch <string>, $GIT_SYNC_BRANCH
The git branch to check out. (default: master)
The git branch to check out. (default: <repo's default branch>)

--change-permissions <int>, $GIT_SYNC_PERMISSIONS
Optionally change permissions on the checked-out files to the
Expand Down Expand Up @@ -1730,7 +1738,7 @@ EXAMPLE USAGE

git-sync \
--repo=https://github.com/kubernetes/git-sync \
--branch=master \
--branch=main \
--rev=HEAD \
--period=10s \
--root=/mnt/git
Expand Down
6 changes: 3 additions & 3 deletions test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ function e2e::head_once_root_exists_but_fails_sanity() {
## FIXME: test when repo is valid git, and is already correct

##############################################
# Test default syncing (master)
# Test default-branch syncing
##############################################
function e2e::default_sync_master() {
function e2e::default_branch_sync() {
# First sync
echo "$FUNCNAME 1" > "$REPO"/file
git -C "$REPO" commit -qam "$FUNCNAME 1"
git -C "$REPO" checkout -q -b master
git -C "$REPO" checkout -q -b weird-name

GIT_SYNC \
--period=100ms \
Expand Down