Skip to content

Commit aa94b0b

Browse files
authored
fix: correct branch name and resume session (#1116)
Fixes: * Closes #1115: parse the branch name, the git repository tracks the default remote branch * Closes #1114: correctly skip git cloning when resuming, re-set the git-proxy settings as expected * Fixes cloning an empty repository
1 parent f0807a7 commit aa94b0b

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

internal/cloner/cloner.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ func (c *Cloner) proxyURL() string {
3434
}
3535

3636
func (c *Cloner) execute(repository Repository) error {
37-
log.Println("Checking if the repo already exists.")
3837
if repository.Exists() {
39-
err := c.setupProxy(repository)
40-
if err != nil {
41-
return err
42-
}
38+
log.Println("Repository exists, skipping cloning.")
39+
return c.setupProxy(repository)
4340
}
41+
log.Println("Setting up repository.")
4442

4543
gitUser := "oauth2"
4644
var gitAccessToken string
@@ -151,7 +149,7 @@ func (c *Cloner) getAccessToken(providerId string) (string, error) {
151149
func (c *Cloner) initializeRepository(repository Repository) error {
152150
log.Println("Initializing repo")
153151

154-
_, err := repository.Cli.Init([]string{})
152+
_, err := repository.Cli.Init([]string{"--initial-branch", "main"})
155153
if err != nil {
156154
return err
157155
}
@@ -208,10 +206,12 @@ func (c *Cloner) clone(repository Repository) error {
208206
} else {
209207
branch = *repository.Branch
210208
}
211-
log.Println("Checking out branch", branch)
212-
_, err = repository.Cli.Checkout([]string{branch})
213-
if err != nil {
214-
return err
209+
if branch != "" {
210+
log.Println("Checking out branch", branch)
211+
_, err = repository.Cli.Checkout([]string{branch})
212+
if err != nil {
213+
return err
214+
}
215215
}
216216

217217
if c.config.LfsAutoFetch {

internal/cloner/repository.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,20 @@ func (r *Repository) Exists() bool {
6767
func (r *Repository) DefaultBranch(remoteName string) (string, error) {
6868
_, err := r.Cli.Remote([]string{"set-head", remoteName, "--auto"})
6969
if err != nil {
70-
return "", err
70+
// Cannot determine remote HEAD: the repository is likely empty
71+
log.Printf("Could not get remote HEAD: %s.\n", err.Error())
72+
return "", nil
7173
}
7274
ref, err := r.Cli.SymbolicRef([]string{fmt.Sprintf("refs/remotes/%v/HEAD", remoteName)})
7375
if err != nil {
7476
return "", err
7577
}
7678
rx := regexp.MustCompile(fmt.Sprintf(`refs/remotes/%v/(?P<branch>.*)`, remoteName))
77-
branch := rx.FindString(ref)
78-
79-
if branch == "" {
80-
return "", fmt.Errorf("branch does not exist")
79+
match := rx.FindStringSubmatch(ref)
80+
if match == nil {
81+
return "", fmt.Errorf("default branch does not exist")
8182
}
83+
branch := match[1]
8284
log.Println("Default branch is", branch)
8385
return branch, nil
8486
}

0 commit comments

Comments
 (0)