This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
127 lines (118 loc) · 3.46 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
)
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
var (
VerboseLogger *log.Logger
InfoLogger *log.Logger
ErrorLogger *log.Logger
USESTATE bool = true
VERBOSE bool = false
STATEFILE string = "./state.json"
CONFIGFILE string = "./config.json"
configfile string
)
// TODO: in config or to env or to cli options
func init() {
var err error
InfoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
VerboseLogger = log.New(os.Stdout, "VERBOSE: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
USESTATE, err = strconv.ParseBool(getEnv("USESTATE", "true"))
checkerr(err)
VERBOSE, err = strconv.ParseBool(getEnv("VERBOSE", "false"))
checkerr(err)
STATEFILE = getEnv("STATEFILE", "./state.json")
CONFIGFILE = getEnv("CONFIGFILE", "./config.json")
if _, err := os.Stat(STATEFILE); err == nil && USESTATE {
configfile = STATEFILE
} else {
configfile = CONFIGFILE
}
}
func checkerr(err error) {
if err != nil {
ErrorLogger.Println(err)
os.Exit(1)
}
}
func main() {
cfg, err := GetConfig(configfile)
if cfg.Verbose || VERBOSE {
VERBOSE = true
}
checkerr(err)
if VERBOSE {
VerboseLogger.Println("config loaded.", cfg)
}
for i, repo := range cfg.Repos {
if VERBOSE {
VerboseLogger.Printf("processing repos[%d] - %s", i, repo.String())
}
fs := memfs.New()
objrepo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: repo.Addr,
})
checkerr(err)
ref, err := objrepo.Reference(plumbing.ReferenceName(fmt.Sprintf("refs/remotes/origin/%s", repo.Branch)), false)
checkerr(err)
if repo.LastCommit != "" && repo.LastCommit != ref.Hash().String() {
if VERBOSE {
VerboseLogger.Println("Analyzing...")
}
hCommit, err := objrepo.CommitObject(ref.Hash())
checkerr(err)
sCommit, err := objrepo.CommitObject(plumbing.NewHash(repo.LastCommit))
checkerr(err)
comparelink := fmt.Sprintf("%s/compare/%s...%s", repo.Addr, sCommit.Hash.String()[0:7], hCommit.Hash.String()[0:7])
fmt.Printf("------\n[%s %s <- %s](%s)\n", GetRepoAuthor(repo), sCommit.Hash.String()[0:7], hCommit.Hash.String()[0:7], comparelink)
filechanges, err := Compare(hCommit, sCommit, &repo)
for i, filechange := range filechanges {
file, err := fs.Open(filechange.Path)
checkerr(err)
fileContent, err := ioutil.ReadAll(file)
checkerr(err)
filechanges[i] = EnrichFileChange(filechange, fileContent)
}
if VERBOSE {
VerboseLogger.Println(filechanges)
}
checkerr(err)
compareresult := ""
for _, filechange := range filechanges {
compareresult += fmt.Sprintf("[%s](%s)\n", filechange.BaseName, filechange.RemoteUrl)
}
if compareresult != "" {
if VERBOSE {
VerboseLogger.Println("Here is results:")
}
fmt.Print(compareresult)
}
fmt.Println()
}
if repo.LastCommit != ref.Hash().String() {
if VERBOSE {
VerboseLogger.Printf("[+] %s lastcommit - %s\n", repo.Addr, repo.LastCommit)
}
repo.LastCommit = ref.Hash().String()
cfg.Repos[i] = repo
}
file, _ := json.MarshalIndent(cfg, "", " ")
_ = ioutil.WriteFile(STATEFILE, file, 0644)
}
}