8000 Merge pull request #998 from pjbgf/ssh-agent-example · go-git/go-git@5159838 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5159838

Browse files
authored
Merge pull request #998 from pjbgf/ssh-agent-example
Add example for git clone using ssh-agent
2 parents a6e934f + 6ad207b commit 5159838

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
git "github.com/go-git/go-git/v5"
8+
. "github.com/go-git/go-git/v5/_examples"
9+
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
10+
)
11+
12+
func main() {
13+
CheckArgs("<url>", "<directory>")
14+
url, directory := os.Args[1], os.Args[2]
8000 15+
16+
authMethod, err := ssh.NewSSHAgentAuth("git")
17+
CheckIfError(err)
18+
19+
// Clone the given repository to the given directory
20+
Info("git clone %s ", url)
21+
22+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
23+
Auth: authMethod,
24+
URL: url,
25+
Progress: os.Stdout,
26+
})
27+
CheckIfError(err)
28+
29+
// ... retrieving the branch being pointed by HEAD
30+
ref, err := r.Head()
31+
CheckIfError(err)
32+
// ... retrieving the commit object
33+
commit, err := r.CommitObject(ref.Hash())
34+
CheckIfError(err)
35+
36+
fmt.Println(commit)
37+
}

0 commit comments

Comments
 (0)
0