10000 Add example for git clone using ssh-agent by pjbgf · Pull Request #998 · go-git/go-git · GitHub
[go: up one dir, main page]

Skip to content

Add example for git clone using ssh-agent #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions _examples/clone/auth/ssh/ssh_agent/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
0