forked from src-d/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.65 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"gopkg.in/src-d/go-git.v4"
. "gopkg.in/src-d/go-git.v4/_examples"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Basic example of how to commit changes to the current branch to an existent
// repository.
func main() {
CheckArgs("<directory>")
directory := os.Args[1]
// Opens an already existent repository.
r, err := git.PlainOpen(directory)
CheckIfError(err)
w, err := r.Worktree()
CheckIfError(err)
// ... we need a file to commit so let's create a new file inside of the
// worktree of the project using the go standard library.
Info("echo \"hellow world!\" > example-git-file")
filename := filepath.Join(directory, "example-git-file")
err = ioutil.WriteFile(filename, []byte("hello world!"), 0644)
CheckIfError(err)
// Adds the new file to the staging area.
Info("git add example-git-file")
_, err = w.Add("example-git-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 are to the repository, with the new file
// just created. We should provide the object.Signature of Author of the
// commit.
Info("git commit -m \"example go-git commit\"")
commit, err := w.Commit("example go-git commit", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
})
CheckIfError(err)
// Prints the current HEAD to verify that all worked well.
Info("git show -s")
obj, err := r.CommitObject(commit)
CheckIfError(err)
fmt.Println(obj)
}