-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
139 lines (117 loc) · 3.49 KB
/
Copy pathgit.go
File metadata and controls
139 lines (117 loc) · 3.49 KB
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
package main
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
var lastBranch string
var changeList []string
func git(args ...string) string {
command := exec.Command("git", args...)
if debug {
fmt.Println(command.Args)
}
outputBinary, err := command.CombinedOutput()
output := string(outputBinary)
if debug {
fmt.Println(output)
}
if err != nil {
sayError(command.Args)
sayError(err)
exit()
}
return output
}
func getGitUserName() string {
return strings.TrimSpace(git("config", "--get", "user.name"))
}
func getGitUserEmail() string {
return strings.TrimSpace(git("config", "--get", "user.email"))
}
func getBranchDetails() (string, string) {
branchDetails := strings.Split(git("rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"), "/")
return strings.Trim(branchDetails[0], "\n"), strings.Trim(branchDetails[1], "\n")
}
func getBranch() string {
return settings.RemoteName + "/" + settings.BranchName
}
func getLastCommitMessage() []string {
message := git("log", "-1", "--pretty=%B")
var files []string
if strings.Contains(message, settings.CommitMessage) {
message = strings.Replace(message, settings.CommitMessage, "", -1)
message = strings.Replace(message, "\n", "", -1)
files = strings.Split(message, " ")
for i := range files {
files[i] = getCurrentDir() + `\` + files[i]
}
}
return files
}
func getModifiedFiles() string {
fileNames := strings.Split(git("diff", "--name-only"), "\n")
fileString := ""
getLastFileChanges(fileNames)
for _, file := range fileNames {
if len(file) > 0 {
fileString = fileString + file + " "
}
}
fileString = strings.TrimSuffix(fileString, " ")
return fileString
}
func getCommitters() []string {
commits := strings.TrimSpace(git("--no-pager", "log", "-n", "10", getBranch(), "--since=1.days", "--pretty=format:%ae|%an"))
return strings.Split(strings.Replace(commits, "\r\n", "\n", -1), "\n")
}
func hasCommits() bool {
output := git("status", "--short")
isMobbing := len(strings.TrimSpace(output)) == 0
return isMobbing
}
func isMobbing() bool {
output := git("branch")
return strings.Contains(output, "* "+settings.BranchName)
}
func hasMobbingBranch() bool {
output := git("branch")
return strings.Contains(output, " "+settings.BranchName) || strings.Contains(output, "* "+settings.BranchName)
}
func hasMobbingBranchOrigin() bool {
output := git("branch", "--remotes")
return strings.Contains(output, " "+getBranch())
}
func isLastChangeSecondsAgo() bool {
recentlyUpdated := git("--no-pager", "log", getBranch(), "-1", "--pretty=format:%cr", "--abbrev-commit")
return strings.Contains(recentlyUpdated, "seconds ago") || strings.Contains(recentlyUpdated, "second ago")
}
func (settings *Settings) updateBranch() {
remoteName, branchName := getBranchDetails()
if settings.RemoteName != remoteName || settings.BranchName != branchName {
settings.RemoteName = remoteName
settings.BranchName = branchName
sayInfo(fmt.Sprintf("Now tracking changes to: %s/%s", settings.RemoteName, settings.BranchName))
saveSettings()
}
}
func getLastFileChanges(filenames []string) {
for i := range filenames {
for minute := 1; minute < settings.TimeLimit; minute++ {
blame := git("blame","--since=" + strconv.Itoa(minute) + ".seconds",filenames[i],"|","grep","-v","'^\\^'")
if len(blame) > 0 {
fmt.Println(blame)
break
}
}
}
}
func commitMessage() string {
return settings.CommitMessage + getModifiedFiles()
}
func commit() {
message := commitMessage()
git("add", "--all")
git("commit", "--message", message)
}