Skip to content

Commit f88b591

Browse files
committed
fix remote branch checkout error (resolves #112)
1 parent 3d2648a commit f88b591

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

git/git_branch_checkout.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,55 @@ package git
33
import (
44
"fmt"
55
"github.com/go-git/go-git/v5"
6-
"github.com/go-git/go-git/v5/config"
76
"github.com/go-git/go-git/v5/plumbing"
87
"github.com/neel1996/gitconvex-server/global"
98
"strings"
109
)
1110

11+
// intermediateFetch performs a remote fetch if the selected checkout branch is a remote branch
12+
func intermediateFetch(repo *git.Repository, branchName string) {
13+
logger.Log("Fetching from remote for remote branch -> "+branchName, global.StatusInfo)
14+
remoteChan := make(chan RemoteDataModel)
15+
go RemoteData(repo, remoteChan)
16+
remoteData := <-remoteChan
17+
remoteURL := remoteData.RemoteURL
18+
FetchFromRemote(repo, *remoteURL[0], branchName)
19+
}
20+
1221
// CheckoutBranch checks out the branchName received as argument
1322
func CheckoutBranch(repo *git.Repository, branchName string) string {
1423
var isRemoteBranch bool
24+
var referenceBranchName string
25+
1526
logger := global.Logger{}
1627
w, _ := repo.Worktree()
1728

1829
if strings.Contains(branchName, "remotes/") {
1930
splitRef := strings.Split(branchName, "/")
20-
branchName = "refs/heads/" + splitRef[len(splitRef)-1]
31+
branchName = splitRef[len(splitRef)-1]
32+
referenceBranchName = "refs/heads/" + branchName
2133
isRemoteBranch = true
2234
} else {
23-
branchName = "refs/heads/" + branchName
35+
referenceBranchName = "refs/heads/" + branchName
2436
}
2537

2638
// If the branch is a remote branch then a remote fetch will be performed and then the branch checkout will be initiated
2739
if isRemoteBranch {
2840
logger.Log(fmt.Sprintf("Branch - %s is a remote branch\nTrying with intermediate remote fetch!", branchName), global.StatusWarning)
29-
fetchErr := repo.Fetch(&git.FetchOptions{
30-
RefSpecs: []config.RefSpec{config.RefSpec(branchName + ":" + branchName)},
31-
})
32-
if fetchErr != nil {
33-
logger.Log("Remote fetch failed -> "+fetchErr.Error(), global.StatusWarning)
34-
}
41+
intermediateFetch(repo, branchName)
3542

3643
checkoutErr := w.Checkout(&git.CheckoutOptions{
37-
Branch: plumbing.ReferenceName(branchName),
44+
Branch: plumbing.ReferenceName(referenceBranchName),
3845
Force: true,
3946
})
4047
if checkoutErr != nil {
41-
logger.Log(checkoutErr.Error(), global.StatusError)
48+
logger.Log("Checkout failed - "+checkoutErr.Error(), global.StatusError)
4249
return global.BranchCheckoutError
4350
}
4451
}
4552

4653
checkoutErr := w.Checkout(&git.CheckoutOptions{
47-
Branch: plumbing.ReferenceName(branchName),
54+
Branch: plumbing.ReferenceName(referenceBranchName),
4855
Keep: true,
4956
})
5057
if checkoutErr != nil {

git/git_fetch.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/neel1996/gitconvex-server/graph/model"
1212
"github.com/neel1996/gitconvex-server/utils"
1313
"io"
14-
"strings"
1514
)
1615

1716
// windowsFetch is used for fetching changes using the git client if the platform is windows
@@ -21,7 +20,8 @@ func windowsFetch(repoPath string, remoteName string, branch string) *model.Fetc
2120
if remoteName == "" && branch == "" {
2221
args = []string{"fetch"}
2322
} else {
24-
args = []string{"fetch", remoteName, branch}
23+
branchReference := branch + ":" + branch
24+
args = []string{"fetch", remoteName, branchReference}
2525
}
2626
cmd := utils.GetGitClient(repoPath, args)
2727
cmdStr, cmdErr := cmd.Output()
@@ -34,7 +34,7 @@ func windowsFetch(repoPath string, remoteName string, branch string) *model.Fetc
3434
FetchedItems: nil,
3535
}
3636
} else {
37-
logger.Log(fmt.Sprintf("Changes fetched from remote - %s -> %s", remoteName, cmdStr), global.StatusInfo)
37+
logger.Log(fmt.Sprintf("Changes fetched from remote - %s\n%s", remoteName, cmdStr), global.StatusInfo)
3838

3939
msg := fmt.Sprintf("Changes fetched from remote %v", remoteName)
4040
return &model.FetchResult{
@@ -104,15 +104,9 @@ func FetchFromRemote(repo *git.Repository, remoteURL string, remoteBranch string
104104
FetchedItems: nil,
105105
}
106106
} else {
107-
if strings.Contains(fetchErr.Error(), "ssh: handshake failed: ssh:") {
108-
logger.Log("Fetch failed. Retrying fetch with git client", global.StatusWarning)
109-
return windowsFetch(w.Filesystem.Root(), remoteName, remoteBranch)
110-
}
111107
logger.Log(fetchErr.Error(), global.StatusError)
112-
return &model.FetchResult{
113-
Status: global.FetchFromRemoteError,
114-
FetchedItems: nil,
115-
}
108+
logger.Log("Fetch failed. Retrying fetch with git client", global.StatusWarning)
109+
return windowsFetch(w.Filesystem.Root(), remoteName, remoteBranch)
116110
}
117111

118112
} else {

git/git_remote_data.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ func RemoteData(repo *git.Repository, remoteChan chan RemoteDataModel) {
3737
var rUrl []*string
3838
for _, i := range remote {
3939
for _, tempUrl := range i.Config().URLs {
40+
logger.Log(fmt.Sprintf("Available remotes in repo : \n%v", tempUrl), global.StatusInfo)
4041
rUrl = append(rUrl, &tempUrl)
4142
}
4243
}
4344
return rUrl
4445
}()
4546

46-
logger.Log(fmt.Sprintf("Available remotes in repo : \n%v", remoteURL), global.StatusInfo)
47-
4847
if len(remoteURL) == 0 {
4948
nilRemote := "No Remote Host Available"
5049
nilRemoteURL := ""

0 commit comments

Comments
 (0)