This repository was archived by the owner on Jun 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
276 lines (251 loc) · 6.64 KB
/
cmd.go
File metadata and controls
276 lines (251 loc) · 6.64 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func cmdInit(input io.Reader, output io.Writer, args []string) error {
switch len(args) {
case 0:
_, err := CreateRepository(".")
return err
case 1:
_, err := CreateRepository(args[0])
return err
default:
return errors.New("usage: init [<dir>]")
}
}
func cmdHashObject(input io.Reader, output io.Writer, args []string) error {
if len(args) != 2 {
return errors.New("usage: hash-object <kind> <path>")
}
switch args[0] {
case "commit", "tree", "tag", "blob":
// All good.
default:
return fmt.Errorf("invalid object type")
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
content, err := ioutil.ReadFile(args[1])
if err != nil {
return fmt.Errorf("read file: %w", err)
}
if sha, err := repo.WriteObject(args[0], content); err != nil {
return fmt.Errorf("write object: %w", err)
} else {
fmt.Println(hex.EncodeToString(sha))
}
return nil
}
func cmdCatFile(input io.Reader, output io.Writer, args []string) error {
if len(args) != 1 {
return errors.New("usage: cat-file <sha>")
}
hash := args[0]
sha, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("invalid hash value: %w", err)
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
obj, err := repo.ReadObject(sha)
if err != nil {
return fmt.Errorf("cannot read object: %w", err)
}
fmt.Print(obj)
return nil
}
func cmdLog(input io.Reader, output io.Writer, args []string) error {
if len(args) != 1 {
return errors.New("usage: log <sha>")
}
hash := args[0]
sha, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("invalid hash value: %w", err)
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
var b bytes.Buffer
fmt.Fprintln(&b, "digraph gogitlog{")
seen := map[string]struct{}{}
if err := writeGraphviz(&b, repo, seen, sha); err != nil {
return err
}
fmt.Fprintln(&b, "}")
_, err = b.WriteTo(output)
return err
}
func writeGraphviz(w io.Writer, repo *Repository, seen map[string]struct{}, sha []byte) error {
obj, err := repo.ReadObject(sha)
if err != nil {
return fmt.Errorf("read %q object: %w", sha, err)
}
c, ok := obj.(*CommitObject)
if !ok {
return fmt.Errorf("not a commit object: %T", obj)
}
for _, parent := range c.Header["parent"] {
fmt.Fprintf(w, "\"%x\" -> \"%s\";\n", sha, parent)
parentSha, err := hex.DecodeString(parent)
if err != nil {
return fmt.Errorf("invalid %q parent sha: %w", parent, err)
}
if err := writeGraphviz(w, repo, seen, parentSha); err != nil {
return err
}
}
return nil
}
func cmdLsTree(input io.Reader, output io.Writer, args []string) error {
if len(args) != 1 {
return errors.New("usage: ls-tree <sha>")
}
hash := args[0]
sha, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("invalid hash value: %w", err)
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
obj, err := repo.ReadObject(sha)
if err != nil {
return fmt.Errorf("read %q object: %w", sha, err)
}
tr, ok := obj.(*TreeObject)
if !ok {
return fmt.Errorf("not a tree object: %T", obj)
}
for _, leaf := range tr.Leafs {
fmt.Printf("%s\t%q\t%x\n", leaf.Mode, leaf.Path, leaf.Sha)
}
return nil
}
func cmdCheckout(input io.Reader, output io.Writer, args []string) error {
if len(args) != 2 {
return errors.New("usage: checkout <commit> <path>")
}
sha, err := hex.DecodeString(args[0])
if err != nil {
return fmt.Errorf("invalid hash value: %w", err)
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
obj, err := repo.ReadObject(sha)
if err != nil {
return fmt.Errorf("read %q object: %w", sha, err)
}
var tr *TreeObject
switch obj := obj.(type) {
case *CommitObject:
sha, err := hex.DecodeString(obj.Header["tree"][0]) // Can not exist?
if err != nil {
return fmt.Errorf("invalid tree hash value: %w", err)
}
tobj, err := repo.ReadObject(sha)
if err != nil {
return fmt.Errorf("read %q tree object: %w", sha, err)
}
tr = tobj.(*TreeObject)
case *TreeObject:
tr = obj
default:
return fmt.Errorf("unexpected %T", obj)
}
destDir, err := filepath.Abs(args[1])
if err != nil {
return fmt.Errorf("absolute path for %q: %w", args[1], err)
}
_ = os.MkdirAll(destDir, newDirPerm)
// Use path instead of repo path to allow to checkout in any directory.
// This is better for testing.
return treeCheckout(repo, tr, destDir)
}
func treeCheckout(repo *Repository, tr *TreeObject, path string) error {
for _, leaf := range tr.Leafs {
obj, err := repo.ReadObject(leaf.Sha)
if err != nil {
return fmt.Errorf("read %x: %w", leaf.Sha, err)
}
dest := filepath.Join(path, leaf.Path)
switch obj := obj.(type) {
case *TreeObject:
if err := os.MkdirAll(dest, newDirPerm); err != nil {
return fmt.Errorf("mkdir %q: %w", dest, err)
}
treeCheckout(repo, obj, dest)
case *BlobObject:
if err := ioutil.WriteFile(dest, obj.Data, 0644); err != nil {
return fmt.Errorf("write %q blob: %w", dest, err)
}
default:
return fmt.Errorf("unexpected %T", obj)
}
}
return nil
}
func cmdShowRef(input io.Reader, output io.Writer, args []string) error {
if len(args) != 0 {
return errors.New("usage: show-ref")
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
var b bytes.Buffer
err = filepath.Walk(filepath.Join(repo.gitdir, "refs"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("read %q: %w", path, err)
}
ref := string(bytes.TrimSpace(content))
if _, err := fmt.Fprintf(&b, "%s %s\n", ref, path[len(repo.gitdir)+1:]); err != nil {
return fmt.Errorf("write: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("walk: %w", err)
}
if _, err := b.WriteTo(output); err != nil {
return fmt.Errorf("write to stdout: %w", err)
}
return nil
}
func cmdTag(input io.Reader, output io.Writer, args []string) error {
if len(args) != 2 {
// Only a single format is supported. Lazy.
return errors.New("usage: tag <name> <hash>")
}
repo, err := FindRepository(".")
if err != nil {
return fmt.Errorf("cannot open git repository: %w", err)
}
if err := repo.WriteFile(true, []byte(args[1]+"\n"), "refs", "tags", args[0]); err != nil {
return fmt.Errorf("write tag file: %w", err)
}
return nil
}