diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-04-11 21:00:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-11 21:00:47 +0100 |
commit | d5b1afd3cc786e56a90cec910b79815ed3b23cc7 (patch) | |
tree | 4e0b93ea480fdea37761d2e879a073ce6f5c1747 /_examples | |
parent | ce62f3e9ff86270538a514a68d3bd5563a733e3b (diff) | |
parent | 8e8281008ee98e9864fa9597be3e16aaecdf9891 (diff) | |
download | go-git-d5b1afd3cc786e56a90cec910b79815ed3b23cc7.tar.gz |
Merge pull request #707 from pjbgf/experimental-sha256
*: Add support for initializing SHA256 repositories
Diffstat (limited to '_examples')
-rw-r--r-- | _examples/sha256/main.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/_examples/sha256/main.go b/_examples/sha256/main.go new file mode 100644 index 0000000..2a257e8 --- /dev/null +++ b/_examples/sha256/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "time" + + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/plumbing/format/config" + "github.com/go-git/go-git/v5/plumbing/object" +) + +// This example requires building with the sha256 tag for it to work: +// go run -tags sha256 main.go /tmp/repository + +// Basic example of how to initialise a repository using sha256 as the hashing algorithmn. +func main() { + CheckArgs("<directory>") + directory := os.Args[1] + + os.RemoveAll(directory) + + // Init a new repository using the ObjectFormat SHA256. + r, err := git.PlainInitWithOptions(directory, &git.PlainInitOptions{ObjectFormat: config.SHA256}) + 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 \"hello 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) + + // 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 Since version 5.0.1, we can omit the Author signature, being read + // from the git config files. + 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) +} |