aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md1
-rw-r--r--_examples/azure_devops/main.go56
-rw-r--r--_examples/find-if-any-tag-point-head/main.go38
-rw-r--r--_examples/remotes/main.go2
4 files changed, 96 insertions, 1 deletions
diff --git a/_examples/README.md b/_examples/README.md
index 92b9d4d..3a4c539 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -19,6 +19,7 @@ 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.
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/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/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)