-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
127 lines (105 loc) · 3.61 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
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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
// CheckArgs should be used to ensure the right command line arguments are
// passed before executing an example.
func CheckArgs(arg ...string) {
if len(os.Args) < len(arg)+1 {
Warning("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
os.Exit(1)
}
}
// CheckIfError should be used to naively panics if an error is not nil.
func CheckIfError(err error) {
if err == nil {
return
} else if fmt.Sprintf("%s", err) != "already up-to-date" {
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
}
// Info should be used to describe the example commands that are about to run.
func Info(format string, args ...interface{}) {
fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
// Warning should be used to display a warning
func Warning(format string, args ...interface{}) {
fmt.Printf("\x1b[36;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
func gitPullOrigin(verbose bool) {
// We instantiate a new repository targeting the given path (the .git folder)
r, err := git.PlainOpen(getCallDirectory())
CheckIfError(err)
// // Get the working directory for the repository
w, err := r.Worktree()
CheckIfError(err)
// // Pull the latest changes from the origin remote and merge into the current branch
if verbose {
Info("git pull origin")
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
CheckIfError(err)
// // Print the latest commit that was just pulled
//ref, err := r.Head()
//CheckIfError(err)
//commit, err := r.CommitObject(ref.Hash())
//CheckIfError(err)
//fmt.Println(commit)
}
func gitCommit(edited_file, commit_message string) {
// Opens an already existing repository.
r, err := git.PlainOpen(getCallDirectory()) //directory .git lives in
CheckIfError(err)
w, err := r.Worktree()
CheckIfError(err)
// Adds the new file to the staging area.
Info(fmt.Sprintf("git add %s", edited_file))
_, err = w.Add(edited_file)
CheckIfError(err)
// We can verify the current status of the worktree using the method Status.
// Info("git status --porcelain")
// status, err := w.Status()
// CheckIfError(err)
// fmt.Println(status)
// 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.
NAME, _ := os.LookupEnv("NAME")
EMAIL, _ := os.LookupEnv("EMAIL")
Info(fmt.Sprintf("git commit -m \"%s\"", commit_message))
commit, err := w.Commit(fmt.Sprintf("%s", commit_message), &git.CommitOptions{
Author: &object.Signature{
Name: NAME,
Email: EMAIL,
When: time.Now(),
},
})
CheckIfError(err)
// Prints the current HEAD to verify that all worked well.
//Info("git show -s")
obj, err := r.CommitObject(commit) //_ == obj
CheckIfError(err)
fmt.Println(obj)
}
func gitPush() {
r, err := git.PlainOpen(getCallDirectory())
CheckIfError(err)
Info("git push")
// push using default options
GITHUB_USER, _ := os.LookupEnv("GITHUB_USER")
GITHUB_PASSWORD, _ := os.LookupEnv("GITHUB_PASSWORD")
err = r.Push(&git.PushOptions{
Auth: &http.BasicAuth{
Username: GITHUB_USER,
Password: GITHUB_PASSWORD,
},
})
CheckIfError(err)
}