aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md2
-rw-r--r--_examples/azure_devops/main.go56
-rw-r--r--_examples/commit/main.go3
-rw-r--r--_examples/common_test.go3
-rw-r--r--_examples/find-if-any-tag-point-head/main.go38
-rw-r--r--_examples/ls-remote/main.go14
-rw-r--r--_examples/remotes/main.go2
-rw-r--r--_examples/sha256/main.go65
-rw-r--r--_examples/tag-create-push/main.go3
9 files changed, 176 insertions, 10 deletions
diff --git a/_examples/README.md b/_examples/README.md
index 92b9d4d..1f150f9 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -19,10 +19,10 @@ Here you can find a list of annotated _go-git_ examples:
- [branch](branch/main.go) - How to create and remove branches or any other kind of reference.
- [tag](tag/main.go) - List/print repository tags.
- [tag create and push](tag-create-push/main.go) - Create and push a new tag.
+- [tag find if head is tagged](find-if-any-tag-point-head/main.go) - Find if `HEAD` is tagged.
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc.
- [progress](progress/main.go) - Printing the progress information from the sideband.
- [revision](revision/main.go) - Solve a revision into a commit.
-- [config](config/main.go) - Explains how to work with config files.
- [submodule](submodule/main.go) - Submodule update remote.
### Advanced
diff --git a/_examples/azure_devops/main.go b/_examples/azure_devops/main.go
new file mode 100644
index 0000000..9c02ca0
--- /dev/null
+++ b/_examples/azure_devops/main.go
@@ -0,0 +1,56 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ git "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
+ "github.com/go-git/go-git/v5/plumbing/transport"
+ "github.com/go-git/go-git/v5/plumbing/transport/http"
+)
+
+func main() {
+ CheckArgs("<url>", "<directory>", "<azuredevops_username>", "<azuredevops_password>")
+ url, directory, username, password := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
+
+ // Clone the given repository to the given directory
+ Info("git clone %s %s", url, directory)
+
+ // Azure DevOps requires capabilities multi_ack / multi_ack_detailed,
+ // which are not fully implemented and by default are included in
+ // transport.UnsupportedCapabilities.
+ //
+ // The initial clone operations require a full download of the repository,
+ // and therefore those unsupported capabilities are not as crucial, so
+ // by removing them from that list allows for the first clone to work
+ // successfully.
+ //
+ // Additional fetches will yield issues, therefore work always from a clean
+ // clone until those capabilities are fully supported.
+ //
+ // New commits and pushes against a remote worked without any issues.
+ transport.UnsupportedCapabilities = []capability.Capability{
+ capability.ThinPack,
+ }
+
+ r, err := git.PlainClone(directory, false, &git.CloneOptions{
+ Auth: &http.BasicAuth{
+ Username: username,
+ Password: password,
+ },
+ URL: url,
+ Progress: os.Stdout,
+ })
+ CheckIfError(err)
+
+ // ... retrieving the branch being pointed by HEAD
+ ref, err := r.Head()
+ CheckIfError(err)
+ // ... retrieving the commit object
+ commit, err := r.CommitObject(ref.Hash())
+ CheckIfError(err)
+
+ fmt.Println(commit)
+}
diff --git a/_examples/commit/main.go b/_examples/commit/main.go
index 4529c84..3f3c880 100644
--- a/_examples/commit/main.go
+++ b/_examples/commit/main.go
@@ -2,7 +2,6 @@ package main
import (
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"time"
@@ -29,7 +28,7 @@ func main() {
// 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)
+ err = os.WriteFile(filename, []byte("hello world!"), 0644)
CheckIfError(err)
// Adds the new file to the staging area.
diff --git a/_examples/common_test.go b/_examples/common_test.go
index 9945c87..6630f15 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -3,7 +3,6 @@ package examples
import (
"flag"
"go/build"
- "io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -65,7 +64,7 @@ func TestExamples(t *testing.T) {
}
func tempFolder() string {
- path, err := ioutil.TempDir("", "")
+ path, err := os.MkdirTemp("", "")
CheckIfError(err)
tempFolders = append(tempFolders, path)
diff --git a/_examples/find-if-any-tag-point-head/main.go b/_examples/find-if-any-tag-point-head/main.go
new file mode 100644
index 0000000..834aea2
--- /dev/null
+++ b/_examples/find-if-any-tag-point-head/main.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/plumbing"
+)
+
+// Basic example of how to find if HEAD is tagged.
+func main() {
+ CheckArgs("<path>")
+ path := os.Args[1]
+
+ // We instantiate a new repository targeting the given path (the .git folder)
+ r, err := git.PlainOpen(path)
+ CheckIfError(err)
+
+ // Get HEAD reference to use for comparison later on.
+ ref, err := r.Head()
+ CheckIfError(err)
+
+ tags, err := r.Tags()
+ CheckIfError(err)
+
+ // List all tags, both lightweight tags and annotated tags and see if some tag points to HEAD reference.
+ err = tags.ForEach(func(t *plumbing.Reference) error {
+ // This technique should work for both lightweight and annotated tags.
+ revHash, err := r.ResolveRevision(plumbing.Revision(t.Name()))
+ CheckIfError(err)
+ if *revHash == ref.Hash() {
+ fmt.Printf("Found tag %s with hash %s pointing to HEAD %s\n", t.Name().Short(), revHash, ref.Hash())
+ }
+ return nil
+ })
+}
diff --git a/_examples/ls-remote/main.go b/_examples/ls-remote/main.go
index af038d6..e49e8c9 100644
--- a/_examples/ls-remote/main.go
+++ b/_examples/ls-remote/main.go
@@ -2,25 +2,35 @@ package main
import (
"log"
+ "os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "github.com/go-git/go-git/v5/_examples"
)
// Retrieve remote tags without cloning repository
func main() {
+ CheckArgs("<url>")
+ url := os.Args[1]
+
+ Info("git ls-remote --tags %s", url)
// Create the remote with repository URL
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
- URLs: []string{"https://github.com/Zenika/MARCEL"},
+ URLs: []string{url},
})
log.Print("Fetching tags...")
// We can then use every Remote functions to retrieve wanted information
- refs, err := rem.List(&git.ListOptions{})
+ refs, err := rem.List(&git.ListOptions{
+ // Returns all references, including peeled references.
+ PeelingOption: git.AppendPeeled,
+ })
if err != nil {
log.Fatal(err)
}
diff --git a/_examples/remotes/main.go b/_examples/remotes/main.go
index b1a91a9..d09957e 100644
--- a/_examples/remotes/main.go
+++ b/_examples/remotes/main.go
@@ -33,7 +33,7 @@ func main() {
CheckIfError(err)
// List remotes from a repository
- Info("git remotes -v")
+ Info("git remote -v")
list, err := r.Remotes()
CheckIfError(err)
diff --git a/_examples/sha256/main.go b/_examples/sha256/main.go
new file mode 100644
index 0000000..e1772d2
--- /dev/null
+++ b/_examples/sha256/main.go
@@ -0,0 +1,65 @@
+package main
+
+import (
+ "fmt"
+ "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 = os.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)
+}
diff --git a/_examples/tag-create-push/main.go b/_examples/tag-create-push/main.go
index c443641..b820c76 100644
--- a/_examples/tag-create-push/main.go
+++ b/_examples/tag-create-push/main.go
@@ -2,7 +2,6 @@ package main
import (
"fmt"
- "io/ioutil"
"log"
"os"
@@ -67,7 +66,7 @@ func cloneRepo(url, dir, publicKeyPath string) (*git.Repository, error) {
func publicKey(filePath string) (*ssh.PublicKeys, error) {
var publicKey *ssh.PublicKeys
- sshKey, _ := ioutil.ReadFile(filePath)
+ sshKey, _ := os.ReadFile(filePath)
publicKey, err := ssh.NewPublicKeys("git", []byte(sshKey), "")
if err != nil {
return nil, err