-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
144 lines (122 loc) · 4.06 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"path/filepath"
"sync"
)
// checkIfBehind checks if the local branch is behind the remote branch.
func checkIfBehind(dir string, wg *sync.WaitGroup, results chan<- string, cfg Config) bool {
defer wg.Done()
var (
params []any
err error
)
// Find root directory of repository.
gitRoot, err := getGitRoot(dir)
if err != nil {
results <- fmt.Sprintf("%sError getting Git root for %s: %v%s", LightRed, dir, err, Reset)
return false
}
g := NewGitExecutor(cfg, gitRoot, filepath.Base(gitRoot))
// Check if the remote exists.
if !g.hasRemoteURL() {
results <- fmt.Sprintf("%sNo remote named '%s' found for %s%s", LightRed, g.RemoteName, g.RepoName, Reset)
return false
}
// Proceed with fetching the branches from the remote.
if fErr := g.fetchBranches(); err != nil {
results <- fmt.Sprintf("%sError fetching %s. %v%s", LightRed, g.RepoName, fErr, Reset)
return false
}
// Check if the branch exists locally.
if !g.branchExistsLocally() {
results <- fmt.Sprintf("Branch %s does not exist in repository %s", g.Branch, g.RepoName)
return false
}
// Check if the branch exists remotely.
if !g.branchExistsRemotely() {
results <- fmt.Sprintf("Remote branch %s does not exist in repository %s", g.Branch, g.RepoName)
return false
}
// Check if the local branch is behind the remote branch.
behindCount, err := g.commitsBehind()
if err != nil {
results <- err.Error()
return false
}
// Check if the repository is outdated.
if behindCount != "0" {
// Find the last commit for the report.
lastCommitInfo, lErr := g.lastCommit()
if lErr != nil {
results <- lErr.Error()
return false
}
params = []any{LightRed, g.RepoName, behindCount, commitText(behindCount), lastCommitInfo, Reset}
result := fmt.Sprintf("%s\n%s is %s %s behind\nLast commit by %s%s", params...)
if g.Update {
result += "\n:."
statusLines, sErr := g.status()
if sErr != nil {
results <- fmt.Sprintf("%sError checking status for %s\n%s%s", LightRed, g.RepoName, sErr, Reset)
return false
}
// Check if there is an ongoing rebase or merge conflict.
isConflict, isRebase := hasConflicts(statusLines)
if isConflict {
// Don't force the update.
if !g.Force {
errorMsg := "has merge conflicts in file(s) or there's a rebase in progress"
solution := "To update anyway use --update --force. This aborts rebase and merge conflicts"
results <- fmt.Sprintf("%s%s %s.\n%s.%s", LightRed, g.RepoName, errorMsg, solution, Reset)
return false
}
// Force the update by aborting processes.
result += fmt.Sprintf("\n%s Forcing update...%s", LightRed, Reset)
if isRebase {
if g.abortRebase() {
results <- fmt.Sprintf("%sError aborting rebase %s%s", LightRed, g.RepoName, Reset)
return false
}
} else {
if g.abortMerge() {
results <- fmt.Sprintf("%sError aborting merge %s%s", LightRed, g.RepoName, Reset)
return false
}
}
}
if hasStagedChanges(statusLines) {
result += "\n Stashing local changes"
if !g.stashChanges() {
results <- fmt.Sprintf("%sError stashing changes in %s%s", LightRed, g.RepoName, Reset)
return false
}
}
if !g.checkoutBranch() {
params = []any{LightRed, g.Branch, g.RepoName, Reset}
results <- fmt.Sprintf("%sError checking out branch %s in repository %s%s", params...)
return false
}
result += "\n Pulling latest changes"
if !g.pullLatest() {
params = []any{LightRed, g.RemoteName, g.Branch, g.RepoName, Reset}
results <- fmt.Sprintf("%sError pulling %s/%s in repository %s%s", params...)
return false
}
if g.isReporterStash() {
result += "\n Applying stashed changes"
if !g.applyStash() {
results <- fmt.Sprintf("%sError applying stash%s", LightRed, Reset)
return false
}
}
result += fmt.Sprintf("\n%s %s is up-to-date%s", LightGreen, g.RepoName, Reset)
}
// Report actions taken.
results <- result + Reset
return true
}
// Already up-to-date.
results <- fmt.Sprintf("%s%s is up-to-date%s", LightGreen, g.RepoName, Reset)
return false
}