aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2024-01-15 16:52:59 +0000
committerGitHub <noreply@github.com>2024-01-15 16:52:59 +0000
commit51598381730654f6a98ceb5b1604e619bf4afc03 (patch)
tree4fb89a337a2ad1caee2e849530a210843446b053
parenta6e934f1f76996c7f92cb4fde708170c1eb5d032 (diff)
parent6ad207be9ca563b0791a2ad35d35a987db7aeb94 (diff)
downloadgo-git-51598381730654f6a98ceb5b1604e619bf4afc03.tar.gz
Merge pull request #998 from pjbgf/ssh-agent-example
Add example for git clone using ssh-agent
-rw-r--r--_examples/clone/auth/ssh/private_key/main.go (renamed from _examples/clone/auth/ssh/main.go)0
-rw-r--r--_examples/clone/auth/ssh/ssh_agent/main.go37
2 files changed, 37 insertions, 0 deletions
diff --git a/_examples/clone/auth/ssh/main.go b/_examples/clone/auth/ssh/private_key/main.go
index 5f21d90..5f21d90 100644
--- a/_examples/clone/auth/ssh/main.go
+++ b/_examples/clone/auth/ssh/private_key/main.go
diff --git a/_examples/clone/auth/ssh/ssh_agent/main.go b/_examples/clone/auth/ssh/ssh_agent/main.go
new file mode 100644
index 0000000..7a2ebd3
--- /dev/null
+++ b/_examples/clone/auth/ssh/ssh_agent/main.go
@@ -0,0 +1,37 @@
+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/transport/ssh"
+)
+
+func main() {
+ CheckArgs("<url>", "<directory>")
+ url, directory := os.Args[1], os.Args[2]
+
+ authMethod, err := ssh.NewSSHAgentAuth("git")
+ CheckIfError(err)
+
+ // Clone the given repository to the given directory
+ Info("git clone %s ", url)
+
+ r, err := git.PlainClone(directory, false, &git.CloneOptions{
+ Auth: authMethod,
+ 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)
+}