/
git.go
199 lines (178 loc) · 4.72 KB
/
git.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
*/
package git
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
)
const (
// EmptyTreeCommitID is a hash representing empty tree
EmptyTreeCommitID = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
)
// CloneRepository clones a repository from a remote source to local temp. dir.
func CloneRepository(url *string, branch *string, depth int, auth transport.AuthMethod) (*git.Repository, string, error) {
urlVal := *url
branchVal := *branch
dir, err := ioutil.TempDir("", "secretscanner")
if err != nil {
return nil, "", err
}
cloneOpt := &git.CloneOptions{
URL: urlVal,
Depth: depth,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchVal)),
SingleBranch: true,
Tags: git.NoTags,
}
if auth != nil {
cloneOpt.Auth = auth
}
repository, err := git.PlainClone(dir, false, cloneOpt)
if err != nil {
return nil, dir, err
}
return repository, dir, nil
}
// GetRepositoryHistory gets commit history of a git repo.
func GetRepositoryHistory(repository *git.Repository) ([]*object.Commit, error) {
var commits []*object.Commit
ref, err := repository.Head()
if err != nil {
return nil, err
}
cIter, err := repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, err
}
cIter.ForEach(func(c *object.Commit) error {
commits = append(commits, c)
return nil
})
return commits, nil
}
// GetChanges gets the changes since a commit till current HEAD
func GetChanges(commit *object.Commit, repo *git.Repository) (object.Changes, error) {
parentCommit, err := GetParentCommit(commit, repo)
if err != nil {
return nil, err
}
commitTree, err := commit.Tree()
if err != nil {
return nil, err
}
parentCommitTree, err := parentCommit.Tree()
if err != nil {
return nil, err
}
changes, err := object.DiffTree(parentCommitTree, commitTree)
if err != nil {
return nil, err
}
return changes, nil
}
// GetParentCommit gets parent commit
func GetParentCommit(commit *object.Commit, repo *git.Repository) (*object.Commit, error) {
if commit.NumParents() == 0 {
parentCommit, err := repo.CommitObject(plumbing.NewHash(EmptyTreeCommitID))
if err != nil {
return nil, err
}
return parentCommit, nil
}
parentCommit, err := commit.Parents().Next()
if err != nil {
return nil, err
}
return parentCommit, nil
}
// GetChangeAction gets change action
func GetChangeAction(change *object.Change) string {
action, err := change.Action()
if err != nil {
return "Unknown"
}
switch action {
case merkletrie.Insert:
return "Insert"
case merkletrie.Modify:
return "Modify"
case merkletrie.Delete:
return "Delete"
default:
return "Unknown"
}
}
// GetChangePath gets change path
func GetChangePath(change *object.Change) string {
action, err := change.Action()
if err != nil {
return change.To.Name
}
if action == merkletrie.Delete {
return change.From.Name
}
return change.To.Name
}
// GetPatch gets patch
func GetPatch(change *object.Change) (*object.Patch, error) {
patch, err := change.Patch()
if err != nil {
return nil, err
}
return patch, err
}
// GetLatestCommitHash runs a git cmd to return latest commit hash
func GetLatestCommitHash(dir string) (string, error) {
os.Chdir(dir)
gitcmd := "git"
task := "rev-parse"
op1 := "--verify"
op2 := "HEAD"
out, err := exec.Command(gitcmd, task, op1, op2).CombinedOutput()
if err != nil {
return "", err
}
commitHash := fmt.Sprintf("%s", strings.TrimSpace(string(out)))
return commitHash, nil
}
// GatherPaths gets all committed file paths
func GatherPaths(dir, branch string, targets []string) ([]string, error) {
os.Chdir(dir)
gitcmd := "git"
listTree := "ls-tree"
op1 := "-r"
op2 := "--name-only"
var paths []string
if len(targets) == 0 {
out, err := exec.Command(gitcmd, listTree, op1, branch, op2).CombinedOutput()
if err != nil {
return nil, err
}
cmdout := fmt.Sprintf("%s", strings.TrimSpace(string(out)))
paths = append(paths, strings.Split(cmdout, "\n")...)
}
for _, t := range targets {
out, err := exec.Command(gitcmd, listTree, op1, fmt.Sprintf("%s:%s", branch, t), op2).CombinedOutput()
if err != nil {
return nil, err
}
cmdout := fmt.Sprintf("%s", strings.TrimSpace(string(out)))
currentPaths := strings.Split(cmdout, "\n")
for i, p := range currentPaths {
currentPaths[i] = path.Join(t, p)
}
paths = append(paths, currentPaths...)
}
return paths, nil
}