1// Package repository contains helper methods for working with the Git repo.
2package repository
3
4import (
5 "bytes"
6 "errors"
7 "fmt"
8 "io"
9 "os/exec"
10 "path"
11 "strconv"
12 "strings"
13
14 "github.com/MichaelMure/git-bug/util/git"
15 "github.com/MichaelMure/git-bug/util/lamport"
16 "github.com/blang/semver"
17)
18
19const createClockFile = "/.git/git-bug/create-clock"
20const editClockFile = "/.git/git-bug/edit-clock"
21
22// ErrNotARepo is the error returned when the git repo root wan't be found
23var ErrNotARepo = errors.New("not a git repository")
24
25var _ ClockedRepo = &GitRepo{}
26
27// GitRepo represents an instance of a (local) git repository.
28type GitRepo struct {
29 Path string
30 createClock *lamport.Persisted
31 editClock *lamport.Persisted
32}
33
34// Run the given git command with the given I/O reader/writers, returning an error if it fails.
35func (repo *GitRepo) runGitCommandWithIO(stdin io.Reader, stdout, stderr io.Writer, args ...string) error {
36 // fmt.Printf("[%s] Running git %s\n", repo.Path, strings.Join(args, " "))
37
38 cmd := exec.Command("git", args...)
39 cmd.Dir = repo.Path
40 cmd.Stdin = stdin
41 cmd.Stdout = stdout
42 cmd.Stderr = stderr
43
44 return cmd.Run()
45}
46
47// Run the given git command and return its stdout, or an error if the command fails.
48func (repo *GitRepo) runGitCommandRaw(stdin io.Reader, args ...string) (string, string, error) {
49 var stdout bytes.Buffer
50 var stderr bytes.Buffer
51 err := repo.runGitCommandWithIO(stdin, &stdout, &stderr, args...)
52 return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
53}
54
55// Run the given git command and return its stdout, or an error if the command fails.
56func (repo *GitRepo) runGitCommandWithStdin(stdin io.Reader, args ...string) (string, error) {
57 stdout, stderr, err := repo.runGitCommandRaw(stdin, args...)
58 if err != nil {
59 if stderr == "" {
60 stderr = "Error running git command: " + strings.Join(args, " ")
61 }
62 err = fmt.Errorf(stderr)
63 }
64 return stdout, err
65}
66
67// Run the given git command and return its stdout, or an error if the command fails.
68func (repo *GitRepo) runGitCommand(args ...string) (string, error) {
69 return repo.runGitCommandWithStdin(nil, args...)
70}
71
72// NewGitRepo determines if the given working directory is inside of a git repository,
73// and returns the corresponding GitRepo instance if it is.
74func NewGitRepo(path string, witnesser Witnesser) (*GitRepo, error) {
75 repo := &GitRepo{Path: path}
76
77 // Check the repo and retrieve the root path
78 stdout, err := repo.runGitCommand("rev-parse", "--show-toplevel")
79
80 // for some reason, "git rev-parse --show-toplevel" return nothing
81 // and no error when inside a ".git" dir
82 if err != nil || stdout == "" {
83 return nil, ErrNotARepo
84 }
85
86 // Fix the path to be sure we are at the root
87 repo.Path = stdout
88
89 err = repo.LoadClocks()
90
91 if err != nil {
92 // No clock yet, trying to initialize them
93 err = repo.createClocks()
94 if err != nil {
95 return nil, err
96 }
97
98 err = witnesser(repo)
99 if err != nil {
100 return nil, err
101 }
102
103 err = repo.WriteClocks()
104 if err != nil {
105 return nil, err
106 }
107
108 return repo, nil
109 }
110
111 return repo, nil
112}
113
114// InitGitRepo create a new empty git repo at the given path
115func InitGitRepo(path string) (*GitRepo, error) {
116 repo := &GitRepo{Path: path}
117 err := repo.createClocks()
118 if err != nil {
119 return nil, err
120 }
121
122 _, err = repo.runGitCommand("init", path)
123 if err != nil {
124 return nil, err
125 }
126
127 return repo, nil
128}
129
130// InitBareGitRepo create a new --bare empty git repo at the given path
131func InitBareGitRepo(path string) (*GitRepo, error) {
132 repo := &GitRepo{Path: path}
133 err := repo.createClocks()
134 if err != nil {
135 return nil, err
136 }
137
138 _, err = repo.runGitCommand("init", "--bare", path)
139 if err != nil {
140 return nil, err
141 }
142
143 return repo, nil
144}
145
146// GetPath returns the path to the repo.
147func (repo *GitRepo) GetPath() string {
148 return repo.Path
149}
150
151// GetUserName returns the name the the user has used to configure git
152func (repo *GitRepo) GetUserName() (string, error) {
153 return repo.runGitCommand("config", "user.name")
154}
155
156// GetUserEmail returns the email address that the user has used to configure git.
157func (repo *GitRepo) GetUserEmail() (string, error) {
158 return repo.runGitCommand("config", "user.email")
159}
160
161// GetCoreEditor returns the name of the editor that the user has used to configure git.
162func (repo *GitRepo) GetCoreEditor() (string, error) {
163 return repo.runGitCommand("var", "GIT_EDITOR")
164}
165
166// GetRemotes returns the configured remotes repositories.
167func (repo *GitRepo) GetRemotes() (map[string]string, error) {
168 stdout, err := repo.runGitCommand("remote", "--verbose")
169 if err != nil {
170 return nil, err
171 }
172
173 lines := strings.Split(stdout, "\n")
174 remotes := make(map[string]string, len(lines))
175
176 for _, line := range lines {
177 elements := strings.Fields(line)
178 if len(elements) != 3 {
179 return nil, fmt.Errorf("unexpected output format: %s", line)
180 }
181
182 remotes[elements[0]] = elements[1]
183 }
184
185 return remotes, nil
186}
187
188// StoreConfig store a single key/value pair in the config of the repo
189func (repo *GitRepo) StoreConfig(key string, value string) error {
190 _, err := repo.runGitCommand("config", "--replace-all", key, value)
191
192 return err
193}
194
195// ReadConfigs read all key/value pair matching the key prefix
196func (repo *GitRepo) ReadConfigs(keyPrefix string) (map[string]string, error) {
197 stdout, err := repo.runGitCommand("config", "--get-regexp", keyPrefix)
198
199 // / \
200 // / ! \
201 // -------
202 //
203 // There can be a legitimate error here, but I see no portable way to
204 // distinguish them from the git error that say "no matching value exist"
205 if err != nil {
206 return nil, nil
207 }
208
209 lines := strings.Split(stdout, "\n")
210
211 result := make(map[string]string, len(lines))
212
213 for _, line := range lines {
214 if strings.TrimSpace(line) == "" {
215 continue
216 }
217
218 parts := strings.Fields(line)
219 if len(parts) != 2 {
220 return nil, fmt.Errorf("bad git config: %s", line)
221 }
222
223 result[parts[0]] = parts[1]
224 }
225
226 return result, nil
227}
228
229func (repo *GitRepo) ReadConfigBool(key string) (bool, error) {
230 val, err := repo.ReadConfigString(key)
231 if err != nil {
232 return false, err
233 }
234
235 return strconv.ParseBool(val)
236}
237
238func (repo *GitRepo) ReadConfigString(key string) (string, error) {
239 stdout, err := repo.runGitCommand("config", "--get-all", key)
240
241 // / \
242 // / ! \
243 // -------
244 //
245 // There can be a legitimate error here, but I see no portable way to
246 // distinguish them from the git error that say "no matching value exist"
247 if err != nil {
248 return "", ErrNoConfigEntry
249 }
250
251 lines := strings.Split(stdout, "\n")
252
253 if len(lines) == 0 {
254 return "", ErrNoConfigEntry
255 }
256 if len(lines) > 1 {
257 return "", ErrMultipleConfigEntry
258 }
259
260 return lines[0], nil
261}
262
263// RmConfigs remove all key/value pair matching the key prefix
264func (repo *GitRepo) RmConfigs(keyPrefix string) error {
265 // try to remove key/value pair by key
266 _, err := repo.runGitCommand("config", "--unset-all", keyPrefix)
267 if err != nil {
268 // starting from git 2.18.0 sections are automatically deleted when the last existing
269 // key/value is removed. Before 2.18.0 we should remove the section
270 // see https://github.com/git/git/blob/master/Documentation/RelNotes/2.18.0.txt#L379
271
272 var ok bool
273 ok, err = repo.gitVersionLT218()
274 if err != nil {
275 return fmt.Errorf("getting git version: %v", err)
276 }
277
278 if ok {
279 // try to remove section
280 _, err = repo.runGitCommand("config", "--remove-section", keyPrefix)
281 }
282 }
283
284 return err
285}
286
287func (repo *GitRepo) gitVersionLT218() (bool, error) {
288 versionOut, err := repo.runGitCommand("version")
289 if err != nil {
290 return false, err
291 }
292
293 versionString := strings.Fields(versionOut)[2]
294 version, err := semver.Make(versionString)
295 if err != nil {
296 return false, err
297 }
298
299 version218string := "2.18.0"
300 gitVersion218, err := semver.Make(version218string)
301 if err != nil {
302 return false, err
303 }
304
305 return version.LT(gitVersion218), nil
306}
307
308// FetchRefs fetch git refs from a remote
309func (repo *GitRepo) FetchRefs(remote, refSpec string) (string, error) {
310 stdout, err := repo.runGitCommand("fetch", remote, refSpec)
311
312 if err != nil {
313 return stdout, fmt.Errorf("failed to fetch from the remote '%s': %v", remote, err)
314 }
315
316 return stdout, err
317}
318
319// PushRefs push git refs to a remote
320func (repo *GitRepo) PushRefs(remote string, refSpec string) (string, error) {
321 stdout, stderr, err := repo.runGitCommandRaw(nil, "push", remote, refSpec)
322
323 if err != nil {
324 return stdout + stderr, fmt.Errorf("failed to push to the remote '%s': %v", remote, stderr)
325 }
326 return stdout + stderr, nil
327}
328
329// StoreData will store arbitrary data and return the corresponding hash
330func (repo *GitRepo) StoreData(data []byte) (git.Hash, error) {
331 var stdin = bytes.NewReader(data)
332
333 stdout, err := repo.runGitCommandWithStdin(stdin, "hash-object", "--stdin", "-w")
334
335 return git.Hash(stdout), err
336}
337
338// ReadData will attempt to read arbitrary data from the given hash
339func (repo *GitRepo) ReadData(hash git.Hash) ([]byte, error) {
340 var stdout bytes.Buffer
341 var stderr bytes.Buffer
342
343 err := repo.runGitCommandWithIO(nil, &stdout, &stderr, "cat-file", "-p", string(hash))
344
345 if err != nil {
346 return []byte{}, err
347 }
348
349 return stdout.Bytes(), nil
350}
351
352// StoreTree will store a mapping key-->Hash as a Git tree
353func (repo *GitRepo) StoreTree(entries []TreeEntry) (git.Hash, error) {
354 buffer := prepareTreeEntries(entries)
355
356 stdout, err := repo.runGitCommandWithStdin(&buffer, "mktree")
357
358 if err != nil {
359 return "", err
360 }
361
362 return git.Hash(stdout), nil
363}
364
365// StoreCommit will store a Git commit with the given Git tree
366func (repo *GitRepo) StoreCommit(treeHash git.Hash) (git.Hash, error) {
367 stdout, err := repo.runGitCommand("commit-tree", string(treeHash))
368
369 if err != nil {
370 return "", err
371 }
372
373 return git.Hash(stdout), nil
374}
375
376// StoreCommitWithParent will store a Git commit with the given Git tree
377func (repo *GitRepo) StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error) {
378 stdout, err := repo.runGitCommand("commit-tree", string(treeHash),
379 "-p", string(parent))
380
381 if err != nil {
382 return "", err
383 }
384
385 return git.Hash(stdout), nil
386}
387
388// UpdateRef will create or update a Git reference
389func (repo *GitRepo) UpdateRef(ref string, hash git.Hash) error {
390 _, err := repo.runGitCommand("update-ref", ref, string(hash))
391
392 return err
393}
394
395// ListRefs will return a list of Git ref matching the given refspec
396func (repo *GitRepo) ListRefs(refspec string) ([]string, error) {
397 stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refspec)
398
399 if err != nil {
400 return nil, err
401 }
402
403 split := strings.Split(stdout, "\n")
404
405 if len(split) == 1 && split[0] == "" {
406 return []string{}, nil
407 }
408
409 return split, nil
410}
411
412// RefExist will check if a reference exist in Git
413func (repo *GitRepo) RefExist(ref string) (bool, error) {
414 stdout, err := repo.runGitCommand("for-each-ref", ref)
415
416 if err != nil {
417 return false, err
418 }
419
420 return stdout != "", nil
421}
422
423// CopyRef will create a new reference with the same value as another one
424func (repo *GitRepo) CopyRef(source string, dest string) error {
425 _, err := repo.runGitCommand("update-ref", dest, source)
426
427 return err
428}
429
430// ListCommits will return the list of commit hashes of a ref, in chronological order
431func (repo *GitRepo) ListCommits(ref string) ([]git.Hash, error) {
432 stdout, err := repo.runGitCommand("rev-list", "--first-parent", "--reverse", ref)
433
434 if err != nil {
435 return nil, err
436 }
437
438 split := strings.Split(stdout, "\n")
439
440 casted := make([]git.Hash, len(split))
441 for i, line := range split {
442 casted[i] = git.Hash(line)
443 }
444
445 return casted, nil
446
447}
448
449// ListEntries will return the list of entries in a Git tree
450func (repo *GitRepo) ListEntries(hash git.Hash) ([]TreeEntry, error) {
451 stdout, err := repo.runGitCommand("ls-tree", string(hash))
452
453 if err != nil {
454 return nil, err
455 }
456
457 return readTreeEntries(stdout)
458}
459
460// FindCommonAncestor will return the last common ancestor of two chain of commit
461func (repo *GitRepo) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error) {
462 stdout, err := repo.runGitCommand("merge-base", string(hash1), string(hash2))
463
464 if err != nil {
465 return "", nil
466 }
467
468 return git.Hash(stdout), nil
469}
470
471// GetTreeHash return the git tree hash referenced in a commit
472func (repo *GitRepo) GetTreeHash(commit git.Hash) (git.Hash, error) {
473 stdout, err := repo.runGitCommand("rev-parse", string(commit)+"^{tree}")
474
475 if err != nil {
476 return "", nil
477 }
478
479 return git.Hash(stdout), nil
480}
481
482// AddRemote add a new remote to the repository
483// Not in the interface because it's only used for testing
484func (repo *GitRepo) AddRemote(name string, url string) error {
485 _, err := repo.runGitCommand("remote", "add", name, url)
486
487 return err
488}
489
490func (repo *GitRepo) createClocks() error {
491 createPath := path.Join(repo.Path, createClockFile)
492 createClock, err := lamport.NewPersisted(createPath)
493 if err != nil {
494 return err
495 }
496
497 editPath := path.Join(repo.Path, editClockFile)
498 editClock, err := lamport.NewPersisted(editPath)
499 if err != nil {
500 return err
501 }
502
503 repo.createClock = createClock
504 repo.editClock = editClock
505
506 return nil
507}
508
509// LoadClocks read the clocks values from the on-disk repo
510func (repo *GitRepo) LoadClocks() error {
511 createClock, err := lamport.LoadPersisted(repo.GetPath() + createClockFile)
512 if err != nil {
513 return err
514 }
515
516 editClock, err := lamport.LoadPersisted(repo.GetPath() + editClockFile)
517 if err != nil {
518 return err
519 }
520
521 repo.createClock = createClock
522 repo.editClock = editClock
523 return nil
524}
525
526// WriteClocks write the clocks values into the repo
527func (repo *GitRepo) WriteClocks() error {
528 err := repo.createClock.Write()
529 if err != nil {
530 return err
531 }
532
533 err = repo.editClock.Write()
534 if err != nil {
535 return err
536 }
537
538 return nil
539}
540
541// CreateTime return the current value of the creation clock
542func (repo *GitRepo) CreateTime() lamport.Time {
543 return repo.createClock.Time()
544}
545
546// CreateTimeIncrement increment the creation clock and return the new value.
547func (repo *GitRepo) CreateTimeIncrement() (lamport.Time, error) {
548 return repo.createClock.Increment()
549}
550
551// EditTime return the current value of the edit clock
552func (repo *GitRepo) EditTime() lamport.Time {
553 return repo.editClock.Time()
554}
555
556// EditTimeIncrement increment the edit clock and return the new value.
557func (repo *GitRepo) EditTimeIncrement() (lamport.Time, error) {
558 return repo.editClock.Increment()
559}
560
561// CreateWitness witness another create time and increment the corresponding clock
562// if needed.
563func (repo *GitRepo) CreateWitness(time lamport.Time) error {
564 return repo.createClock.Witness(time)
565}
566
567// EditWitness witness another edition time and increment the corresponding clock
568// if needed.
569func (repo *GitRepo) EditWitness(time lamport.Time) error {
570 return repo.editClock.Witness(time)
571}