-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
119 lines (100 loc) · 2.75 KB
/
Copy pathgit.go
File metadata and controls
119 lines (100 loc) · 2.75 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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
var execCommand = exec.Command
func isGitRepo() error {
cmd := execCommand("git", "rev-parse", "--is-inside-work-tree")
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("not a git repository: %s", strings.TrimSpace(string(output)))
}
return nil
}
func stageAll() error {
cmd := execCommand("git", "add", "--all", ":/")
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to stage changes: %s", strings.TrimSpace(string(output)))
}
return nil
}
func snapshotIndex() (string, error) {
cmd := execCommand("git", "diff", "--cached", "--binary")
output, err := cmd.CombinedOutput()
if err != nil {
return "", errorf("failed to snapshot staged changes: %s", strings.TrimSpace(string(output)))
}
return string(output), nil
}
func restoreIndex(snapshot string) error {
resetCmd := execCommand("git", "reset")
resetOutput, err := resetCmd.CombinedOutput()
if err != nil {
return errorf("failed to reset index: %s", strings.TrimSpace(string(resetOutput)))
}
if strings.TrimSpace(snapshot) == "" {
return nil
}
applyCmd := execCommand("git", "apply", "--cached", "--whitespace=nowarn", "-")
applyCmd.Stdin = bytes.NewBufferString(snapshot)
applyOutput, err := applyCmd.CombinedOutput()
if err != nil {
return errorf("failed to restore staged changes: %s", strings.TrimSpace(string(applyOutput)))
}
return nil
}
func getStagedDiff() (string, error) {
cmd := execCommand("git", "diff", "--cached")
output, err := cmd.CombinedOutput()
if err != nil {
return "", errorf("failed to get diff: %s", strings.TrimSpace(string(output)))
}
diff := strings.TrimSpace(string(output))
if diff == "" {
return "", errorf("no changes to commit")
}
return diff, nil
}
func commit(message string) error {
cmd := execCommand("git", "commit", "-m", message)
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to commit: %s", strings.TrimSpace(string(output)))
}
return nil
}
func commitWithEditor(message string) error {
cmd := execCommand("git", "commit", "--edit", "-m", message)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return errorf("failed to commit: %v", err)
}
return nil
}
func push() error {
cmd := execCommand("git", "push")
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to push: %s", strings.TrimSpace(string(output)))
}
return nil
}
func errorf(format string, args ...interface{}) error {
return &gitError{msg: formatArgs(format, args)}
}
func formatArgs(format string, args []interface{}) string {
return fmt.Sprintf(format, args...)
}
type gitError struct {
msg string
}
func (e *gitError) Error() string {
return e.msg
}