This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 832
/
Copy pathgit.go
120 lines (107 loc) · 2.66 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
package core
import (
"fmt"
"io/ioutil"
"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 = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
)
func CloneRepository(url *string, branch *string, depth int) (*git.Repository, string, error) {
urlVal := *url
branchVal := *branch
dir, err := ioutil.TempDir("", "gitrob")
if err != nil {
return nil, "", err
}
repository, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: urlVal,
Depth: depth,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchVal)),
SingleBranch: true,
Tags: git.NoTags,
})
if err != nil {
return nil, dir, err
}
return repository, dir, nil
}
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
}
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
}
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
}
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"
}
}
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
} else {
return change.To.Name
}
}