-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
148 lines (137 loc) · 3.42 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
//go:build !plan9
// +build !plan9
package main
import (
"fmt"
"log"
"os"
"path"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func gitInit() error {
if !*useGit {
return nil
}
_, err := git.PlainInit(*rootDir, false)
if err != nil {
return err
}
log.Printf("Git Init %q", *rootDir)
return nil
}
func gitAdd(file, user string) error {
if !*useGit {
return nil
}
gr, err := git.PlainOpen(*rootDir)
if err != nil {
return fmt.Errorf("unable to open git repo: %v", err)
}
wt, err := gr.Worktree()
if err != nil {
return fmt.Errorf("unable to get git work tree: %v", err)
}
_, err = wt.Add(file)
if err != nil {
return fmt.Errorf("unable to add git file %v: %v", file, err)
}
hash, err := wt.Commit("User "+user+" modified "+file, &git.CommitOptions{
Author: &object.Signature{
Name: user,
When: time.Now(),
}})
if err != nil {
return fmt.Errorf("unable to commit git: %v", err)
}
log.Printf("Git Add: user=%v file=%v commit=%v", user, file, hash)
return nil
}
func gitDelete(file, user string) error {
if !*useGit {
log.Printf("User %v deleted %v", user, file)
return os.Remove(path.Join(*rootDir, file))
}
gr, err := git.PlainOpen(*rootDir)
if err != nil {
return fmt.Errorf("unable to open git repo: %v", err)
}
wt, err := gr.Worktree()
if err != nil {
return fmt.Errorf("unable to get git work tree: %v", err)
}
_, err = wt.Remove(file)
if err != nil {
return fmt.Errorf("unable to delete git file %v: %v", file, err)
}
hash, err := wt.Commit("User "+user+" deleted "+file, &git.CommitOptions{
Author: &object.Signature{
Name: user,
When: time.Now(),
}})
if err != nil {
return fmt.Errorf("unable to commit git: %v", err)
}
log.Printf("Git Delete: user=%v file=%v commit=%v", user, file, hash)
return nil
}
func gitMove(old, new, user string) error {
if !*useGit {
log.Printf("User %v renamed %v to %v", user, old, new)
return os.Rename(path.Join(*rootDir, old), path.Join(*rootDir, new))
}
gr, err := git.PlainOpen(*rootDir)
if err != nil {
return fmt.Errorf("unable to open git repo: %v", err)
}
wt, err := gr.Worktree()
if err != nil {
return fmt.Errorf("unable to get git work tree: %v", err)
}
_, err = wt.Move(old, new)
if err != nil {
return fmt.Errorf("unable to move %v -> %v: %v", old, new, err)
}
hash, err := wt.Commit("User "+user+" renamed "+old+" to "+new, &git.CommitOptions{
Author: &object.Signature{
Name: user,
When: time.Now(),
}})
if err != nil {
return fmt.Errorf("unable to commit git: %v", err)
}
log.Printf("Git Rename: user=%v old=%v new=%v commit=%v", user, old, new, hash)
return nil
}
type commitList struct {
author string
time time.Time
message string
}
func gitList() ([]commitList, error) {
if !*useGit {
return nil, nil
}
gr, err := git.PlainOpen(*rootDir)
if err != nil {
return nil, fmt.Errorf("unable to open git repo: %v", err)
}
ref, err := gr.Head()
if err != nil {
return nil, fmt.Errorf("unable to get head: %v", err)
}
iter, err := gr.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, fmt.Errorf("unable to get commit log: %v", err)
}
cl := []commitList{}
err = iter.ForEach(func(c *object.Commit) error {
cl = append(cl, commitList{author: c.Author.String(), time: c.Committer.When, message: c.Message})
return nil
})
if err != nil {
return nil, fmt.Errorf("unable to iterate through commit log: %v", err)
}
return cl, nil
}