Skip to content

Instantly share code, notes, and snippets.

@r10r
Last active March 29, 2021 13:38
Show Gist options
  • Save r10r/90c6ba2f4bcd354d32ff45a159ec8ca1 to your computer and use it in GitHub Desktop.
Save r10r/90c6ba2f4bcd354d32ff45a159ec8ca1 to your computer and use it in GitHub Desktop.
go-git example
package main
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"io/ioutil"
"path/filepath"
"time"
)
func main() {
repoDir := "/tmp/foo"
repo, err := git.PlainOpen(repoDir)
if err == git.ErrRepositoryNotExists {
repo, err = git.PlainInit(repoDir, false)
}
if err != nil {
panic(err)
}
file := filepath.Join(repoDir, "hello.txt")
data := time.Now().Format(time.RFC1123)
err = ioutil.WriteFile(file, []byte(data), 0640)
if err != nil {
panic(err)
}
commitOpts := git.CommitOptions{
Author: &object.Signature{
Name: "Automat",
Email: "automat@example.com",
When: time.Now(),
},
}
tree, err := repo.Worktree()
_, err = tree.Add("hello.txt")
if err != nil {
panic(err)
}
_, err = tree.Commit("new commit message", &commitOpts)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment