aboutsummaryrefslogtreecommitdiffstats
path: root/examples/clone/main.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-12-12 15:50:03 +0100
committerGitHub <noreply@github.com>2016-12-12 15:50:03 +0100
commit3967812bd0de40330dfbb9e1a7d14d4073cc1b10 (patch)
treedbd5df2a66bdd50df40fd773e1d1ec284483ecfe /examples/clone/main.go
parent6f701ecc18909959364b708b8efddd03cf4e809c (diff)
downloadgo-git-3967812bd0de40330dfbb9e1a7d14d4073cc1b10.tar.gz
examples: review, testing and documentation (#176)
* examples reviews, testing and documentation * including the execution on travis, and fix readme * fix example link * including the execution on travis
Diffstat (limited to 'examples/clone/main.go')
-rw-r--r--examples/clone/main.go73
1 files changed, 11 insertions, 62 deletions
diff --git a/examples/clone/main.go b/examples/clone/main.go
index 0fa5fe6..13257ca 100644
--- a/examples/clone/main.go
+++ b/examples/clone/main.go
@@ -2,87 +2,36 @@ package main
import (
"fmt"
- "io"
"os"
- "path/filepath"
-
- "github.com/fatih/color"
"gopkg.in/src-d/go-git.v4"
+ . "gopkg.in/src-d/go-git.v4/examples"
)
func main() {
- checkArgs()
+ CheckArgs("<url>", "<directory>")
url := os.Args[1]
directory := os.Args[2]
r, err := git.NewFilesystemRepository(directory)
- checkIfError(err)
+ CheckIfError(err)
- // Clone the given repository, using depth we create a shallow clone :
- // > git clone <url> --depth 1
- color.Blue("git clone %s --depth 1 %s", url, directory)
+ // Clone the given repository to the given directory
+ Info("git clone %s %s", url, directory)
err = r.Clone(&git.CloneOptions{
- URL: url,
+ URL: url,
+ Depth: 1,
})
- checkIfError(err)
+
+ CheckIfError(err)
// ... retrieving the branch being pointed by HEAD
ref, err := r.Head()
- checkIfError(err)
+ CheckIfError(err)
// ... retrieving the commit object
commit, err := r.Commit(ref.Hash())
- checkIfError(err)
+ CheckIfError(err)
fmt.Println(commit)
- os.Exit(0)
-
- // ... we get all the files from the commit
- files, err := commit.Files()
- checkIfError(err)
-
- // ... now we iterate the files to save to disk
- err = files.ForEach(func(f *git.File) error {
- abs := filepath.Join(directory, f.Name)
- dir := filepath.Dir(abs)
-
- os.MkdirAll(dir, 0777)
- file, err := os.Create(abs)
- if err != nil {
- return err
- }
-
- defer file.Close()
- r, err := f.Reader()
- if err != nil {
- return err
- }
-
- defer r.Close()
-
- if err := file.Chmod(f.Mode); err != nil {
- return err
- }
-
- _, err = io.Copy(file, r)
- return err
- })
- checkIfError(err)
-}
-
-func checkIfError(err error) {
- if err == nil {
- return
- }
-
- color.Red("error: %s", err)
- os.Exit(1)
-}
-
-func checkArgs() {
- if len(os.Args) < 3 {
- color.Cyan("Usage: %s <url> <directory>", os.Args[0])
- os.Exit(1)
- }
}