| 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>", "<private_key_file>") |
| 14 |
url, directory, privateKeyFile := os.Args[1], os.Args[2], os.Args[3] |
| 15 |
var password string |
| 16 |
if len(os.Args) == 5 { |
| 17 |
password = os.Args[4] |
| 18 |
} |
| 19 |
|
| 20 |
_, err := os.Stat(privateKeyFile) |
| 21 |
if err != nil { |
| 22 |
Warning("read file %s failed %s\n", privateKeyFile, err.Error()) |
| 23 |
return |
| 24 |
} |
| 25 |
|
| 26 |
// Clone the given repository to the given directory |
| 27 |
Info("git clone %s ", url) |
| 28 |
publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, password) |
| 29 |
if err != nil { |
| 30 |
Warning("generate publickeys failed: %s\n", err.Error()) |
| 31 |
return |
| 32 |
} |
| 33 |
|
| 34 |
r, err := git.PlainClone(directory, false, &git.CloneOptions{ |
| 35 |
Auth: publicKeys, |
| 36 |
URL: url, |
| 37 |
Progress: os.Stdout, |
| 38 |
}) |
| 39 |
CheckIfError(err) |
| 40 |
|
| 41 |
// ... retrieving the branch being pointed by HEAD |
| 42 |
ref, err := r.Head() |
| 43 |
CheckIfError(err) |
| 44 |
// ... retrieving the commit object |
| 45 |
commit, err := r.CommitObject(ref.Hash()) |
| 46 |
CheckIfError(err) |
| 47 |
|
| 48 |
fmt.Println(commit) |
| 49 |
} |