diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-05-08 12:40:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-08 12:40:41 +0200 |
commit | 15a23a7622978f0c5d74a381782a3c7da97016da (patch) | |
tree | ce89ebc7c1490f35e1da54e0cdff1291745b34bc | |
parent | 7cd021554eb318165dd28988fe1675a5e5c32601 (diff) | |
parent | 86abaa0527744875014097850de23606d15395e5 (diff) | |
download | go-git-15a23a7622978f0c5d74a381782a3c7da97016da.tar.gz |
Merge pull request #381 from mcuadros/example-commit
examples: commit example
-rw-r--r-- | _examples/commit/main.go | 67 | ||||
-rw-r--r-- | _examples/common_test.go | 1 |
2 files changed, 68 insertions, 0 deletions
diff --git a/_examples/commit/main.go b/_examples/commit/main.go new file mode 100644 index 0000000..aeb1d4f --- /dev/null +++ b/_examples/commit/main.go @@ -0,0 +1,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 existant +// repository. +func main() { + CheckArgs("<directory>") + directory := os.Args[1] + + // Opens an already existant 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: "john@doe.org", + 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) +} diff --git a/_examples/common_test.go b/_examples/common_test.go index ff01ba3..0a00006 100644 --- a/_examples/common_test.go +++ b/_examples/common_test.go @@ -17,6 +17,7 @@ var defaultURL = "https://github.com/git-fixtures/basic.git" var args = map[string][]string{ "checkout": []string{defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"}, "clone": []string{defaultURL, tempFolder()}, + "commit": []string{cloneRepository(defaultURL, tempFolder())}, "custom_http": []string{defaultURL}, "open": []string{cloneRepository(defaultURL, tempFolder())}, "progress": []string{defaultURL, tempFolder()}, |