-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitpush.go
More file actions
77 lines (72 loc) · 1.88 KB
/
gitpush.go
File metadata and controls
77 lines (72 loc) · 1.88 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
package main
import (
"os"
"os/exec"
// "github.com/go-git/go-git/v5"
// "github.com/go-git/go-git/v5/plumbing/object"
)
// CheckGit returns nil if directory contains git working git tree, error otherwise
func CheckGit(directory string) error {
current, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(current)
err = os.Chdir(directory)
if err != nil {
return err
}
_, err = exec.Command("git", "status", "--porcelain").Output()
if err != nil {
return err
}
return nil
}
// PushRemote performs git steps on supplied file in directory: stage, commit, push
func PushRemote(directory string, fileToCommit string, comments []string) (string, error) {
// Opens an already existing repository.
current, err := os.Getwd()
if err != nil {
return "", err
}
defer os.Chdir(current)
err = os.Chdir(directory)
if err != nil {
return "Cannot set directory", err
}
_, err = exec.Command("git", "status", "--porcelain").Output()
if err != nil {
return "Status error:", err
}
// Adds the new file to the staging area.
_, err = exec.Command("git", "add", fileToCommit).Output()
if err != nil {
return "Staging error", err
}
// We can verify the current status of the worktree using the method Status.
//Info("git status --porcelain")
_, err = exec.Command("git", "status", "--porcelain").Output()
if err != nil {
return "Status staging error:", err
}
// Commits the current staging area to the repository, with the new file
// just created. We should provide the object.Signature of Author of the
// commit.
comment := "-m\""
for i, c := range comments {
comment += c
if i < len(comments)-1 {
comment += ","
}
}
comment += "\""
_, err = exec.Command("git", "commit", comment).Output()
if err != nil {
return "Commit error:", err
}
_, err = exec.Command("git", "push").Output()
if err != nil {
return "Push error:", err
}
return "Success", nil
}