aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-03-07 23:41:07 +0000
committerPaulo Gomes <pjbgf@linux.com>2023-03-08 00:15:02 +0000
commit8e8281008ee98e9864fa9597be3e16aaecdf9891 (patch)
tree744f3c9a5a837d1c230adffa4766c016573769e3
parent9822ad8573e374421a79c096d8f1dfa91366fb02 (diff)
downloadgo-git-8e8281008ee98e9864fa9597be3e16aaecdf9891.tar.gz
examples: Add example for SHA256 repositories
Add a new example on initializing a new repository with object format SHA256. A new Makefile target was created to run the example to ensure that new changes won't break SHA256 support going forwards. Relates to the SHA256 implementation, defined in #706. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
-rw-r--r--.github/workflows/git.yml3
-rw-r--r--Makefile6
-rw-r--r--_examples/sha256/main.go66
3 files changed, 75 insertions, 0 deletions
diff --git a/.github/workflows/git.yml b/.github/workflows/git.yml
index c945e72..3a40c0c 100644
--- a/.github/workflows/git.yml
+++ b/.github/workflows/git.yml
@@ -42,6 +42,9 @@ jobs:
- name: Test
run: make test-coverage
+ - name: Test SHA256
+ run: make test-sha256
+
- name: Build go-git with CGO disabled
run: go build ./...
env:
diff --git a/Makefile b/Makefile
index 2acb8bc..f0c1abb 100644
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,12 @@ test:
@echo "running against `git version`"; \
$(GOTEST) -race ./...
+TEMP_REPO := $(shell mktemp)
+test-sha256:
+ $(GOCMD) run -tags sha256 _examples/sha256/main.go $(TEMP_REPO)
+ cd $(TEMP_REPO) && git fsck
+ rm -rf $(TEMP_REPO)
+
test-coverage:
@echo "running against `git version`"; \
echo "" > $(COVERAGE_REPORT); \
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)
+}