-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
61 lines (50 loc) · 1.19 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
// pago - a command-line password manager.
//
// License: MIT.
// See the file LICENSE.
package main
import (
"fmt"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func initGitRepo(repoDir string) error {
_, err := git.PlainInit(repoDir, false)
if err != nil {
return fmt.Errorf("failed to initialize Git repository: %v", err)
}
return nil
}
func commit(repoDir, authorName, authorEmail, message string, add []string) error {
repo, err := git.PlainOpen(repoDir)
if err != nil {
return fmt.Errorf("failed to open Git repository: %v", err)
}
w, err := repo.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree: %v", err)
}
for _, name := range add {
relPath, err := filepath.Rel(repoDir, name)
if err != nil {
return fmt.Errorf("failed to get relative path: %v", err)
}
_, err = w.Add(relPath)
if err != nil {
return fmt.Errorf("failed to stage file: %v", err)
}
}
_, err = w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: authorName,
Email: authorEmail,
When: time.Now(),
},
})
if err != nil {
return fmt.Errorf("failed to commit: %v", err)
}
return nil
}