Skip to content

Commit d470180

Browse files
committed
refactor to return error when deletion fails
1 parent 789e292 commit d470180

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

docker-compose.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
services:
2+
gitconvex:
3+
container_name: gitconvex
4+
entrypoint:
5+
- go run /opt/workroom/gitconvex/dist/gitconvex-server
6+
environment:
7+
NODE_ENV: production
8+
expose:
9+
- 9001
10+
healthcheck:
11+
interval: 1m30s
12+
retries: 3
13+
test:
14+
- CMD
15+
- curl
16+
- -f
17+
- http://localhost
18+
timeout: 10s
19+
hostname: gitconvex-container
20+
image: itassistors/gitconvex
21+
labels:
22+
generated: by 8gwifi.org
23+
ports:
24+
- '9001'
25+
volumes:
26+
- primary:/opt
27+
working_dir: /opt/workroom/gitconvex
28+
version: '3'
29+
volumes:
30+
primary: null
31+

git/git_branch_checkout.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +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"
67
"github.com/go-git/go-git/v5/plumbing"
78
"github.com/neel1996/gitconvex-server/global"
89
"strings"
910
)
1011

1112
// CheckoutBranch checks out the branchName received as argument
1213
func CheckoutBranch(repo *git.Repository, branchName string) string {
14+
var isRemoteBranch bool
1315
logger := global.Logger{}
16+
w, _ := repo.Worktree()
1417

1518
if strings.Contains(branchName, "remotes/") {
1619
splitRef := strings.Split(branchName, "/")
1720
branchName = "refs/heads/" + splitRef[len(splitRef)-1]
21+
isRemoteBranch = true
1822
} else {
1923
branchName = "refs/heads/" + branchName
2024
}
2125

22-
w, _ := repo.Worktree()
26+
// If the branch is a remote branch then a remote fetch will be performed and then the branch checkout will be initiated
27+
if isRemoteBranch {
28+
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+
}
35+
36+
checkoutErr := w.Checkout(&git.CheckoutOptions{
37+
Branch: plumbing.ReferenceName(branchName),
38+
Force: true,
39+
})
40+
if checkoutErr != nil {
41+
logger.Log(checkoutErr.Error(), global.StatusError)
42+
return "CHECKOUT_FAILED"
43+
}
44+
}
45+
2346
checkoutErr := w.Checkout(&git.CheckoutOptions{
2447
Branch: plumbing.ReferenceName(branchName),
2548
Keep: true,
2649
})
2750
if checkoutErr != nil {
28-
logger.Log(fmt.Sprintf("Failed to checkout branch - %s --> %v\nRetrying with new branch creation!", branchName, checkoutErr.Error()), global.StatusWarning)
29-
err := w.Checkout(&git.CheckoutOptions{
30-
Branch: plumbing.ReferenceName(branchName),
31-
Create: true,
32-
Keep: true,
33-
})
34-
if err != nil {
35-
return "CHECKOUT_FAILED"
36-
}
51+
logger.Log(checkoutErr.Error(), global.StatusError)
52+
return "CHECKOUT_FAILED"
3753
}
54+
3855
logger.Log(fmt.Sprintf("Current branch checked out to -> %s", branchName), global.StatusInfo)
3956
return fmt.Sprintf("Head checked out to branch - %v", branchName)
4057
}

git/git_branch_delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func DeleteBranch(repo *git.Repository, branchName string, forceFlag bool) *mode
2424
} else {
2525
b, bErr := repo.Branch(branchName)
2626
if bErr != nil {
27-
fmt.Println(bErr.Error())
27+
logger.Log(fmt.Sprintf("Failed to delete branch %s -> %v", branchName, bErr.Error()), global.StatusError)
28+
return &model.BranchDeleteStatus{Status: global.BranchDeleteError}
2829
} else {
2930
logger.Log("Deleting branch "+b.Name, global.StatusInfo)
3031
branchErr = repo.Storer.RemoveReference(ref.Name())

git/git_pull.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ func windowsPull(repoPath string, remoteName string, branch string) *model.PullR
3030
PulledItems: nil,
3131
}
3232
} else {
33+
if strings.Contains(string(cmdStr), "Already up to date") {
34+
logger.Log(fmt.Sprintf("No new changes available -> %s", cmdStr), global.StatusInfo)
35+
36+
msg := "No changes to pull from " + remoteName
37+
return &model.PullResult{
38+
Status: "NEW CHANGES ABSENT",
39+
PulledItems: []*string{&msg},
40+
}
41+
}
3342
msg := "New Items Pulled from remote " + remoteName
3443
logger.Log(fmt.Sprintf("Changes pulled from remote -> %s", cmdStr), global.StatusInfo)
35-
3644
return &model.PullResult{
3745
Status: "PULL SUCCESS",
3846
PulledItems: []*string{&msg},

0 commit comments

Comments
 (0)