aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--_examples/checkout-branch/main.go85
-rw-r--r--_examples/common_test.go73
3 files changed, 124 insertions, 35 deletions
diff --git a/Makefile b/Makefile
index 1e10396..3d5b54f 100644
--- a/Makefile
+++ b/Makefile
@@ -28,6 +28,7 @@ build-git:
test:
@echo "running against `git version`"; \
$(GOTEST) -race ./...
+ $(GOTEST) -v _examples/common_test.go _examples/common.go --examples
TEMP_REPO := $(shell mktemp)
test-sha256:
diff --git a/_examples/checkout-branch/main.go b/_examples/checkout-branch/main.go
new file mode 100644
index 0000000..59dfdfc
--- /dev/null
+++ b/_examples/checkout-branch/main.go
@@ -0,0 +1,85 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/config"
+ "github.com/go-git/go-git/v5/plumbing"
+)
+
+// Checkout a branch
+func main() {
+ CheckArgs("<url>", "<directory>", "<branch>")
+ url, directory, branch := os.Args[1], os.Args[2], os.Args[3]
+
+ // Clone the given repository to the given directory
+ Info("git clone %s %s", url, directory)
+ r, err := git.PlainClone(directory, false, &git.CloneOptions{
+ URL: url,
+ })
+ CheckIfError(err)
+
+ // ... retrieving the commit being pointed by HEAD
+ Info("git show-ref --head HEAD")
+ ref, err := r.Head()
+ CheckIfError(err)
+
+ fmt.Println(ref.Hash())
+
+ w, err := r.Worktree()
+ CheckIfError(err)
+
+ // ... checking out branch
+ Info("git checkout %s", branch)
+
+ branchRefName := plumbing.NewBranchReferenceName(branch)
+ branchCoOpts := git.CheckoutOptions{
+ Branch: plumbing.ReferenceName(branchRefName),
+ Force: true,
+ }
+ if err := w.Checkout(&branchCoOpts); err != nil {
+ Warning("local checkout of branch '%s' failed, will attempt to fetch remote branch of same name.", branch)
+ Warning("like `git checkout <branch>` defaulting to `git checkout -b <branch> --track <remote>/<branch>`")
+
+ mirrorRemoteBranchRefSpec := fmt.Sprintf("refs/heads/%s:refs/heads/%s", branch, branch)
+ err = fetchOrigin(r, mirrorRemoteBranchRefSpec)
+ CheckIfError(err)
+
+ err = w.Checkout(&branchCoOpts)
+ CheckIfError(err)
+ }
+ CheckIfError(err)
+
+ Info("checked out branch: %s", branch)
+
+ // ... retrieving the commit being pointed by HEAD (branch now)
+ Info("git show-ref --head HEAD")
+ ref, err = r.Head()
+ CheckIfError(err)
+ fmt.Println(ref.Hash())
+}
+
+func fetchOrigin(repo *git.Repository, refSpecStr string) error {
+ remote, err := repo.Remote("origin")
+ CheckIfError(err)
+
+ var refSpecs []config.RefSpec
+ if refSpecStr != "" {
+ refSpecs = []config.RefSpec{config.RefSpec(refSpecStr)}
+ }
+
+ if err = remote.Fetch(&git.FetchOptions{
+ RefSpecs: refSpecs,
+ }); err != nil {
+ if err == git.NoErrAlreadyUpToDate {
+ fmt.Print("refs already up to date")
+ } else {
+ return fmt.Errorf("fetch origin failed: %v", err)
+ }
+ }
+
+ return nil
+}
diff --git a/_examples/common_test.go b/_examples/common_test.go
index 6630f15..5e3f753 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -2,10 +2,10 @@ package examples
import (
"flag"
- "go/build"
"os"
"os/exec"
"path/filepath"
+ "runtime"
"testing"
)
@@ -14,26 +14,43 @@ var examplesTest = flag.Bool("examples", false, "run the examples tests")
var defaultURL = "https://github.com/git-fixtures/basic.git"
var args = map[string][]string{
- "branch": {defaultURL, tempFolder()},
- "checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
- "clone": {defaultURL, tempFolder()},
- "context": {defaultURL, tempFolder()},
- "commit": {cloneRepository(defaultURL, tempFolder())},
- "custom_http": {defaultURL},
- "open": {cloneRepository(defaultURL, tempFolder())},
- "progress": {defaultURL, tempFolder()},
- "push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
- "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
- "showcase": {defaultURL, tempFolder()},
- "tag": {cloneRepository(defaultURL, tempFolder())},
- "pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
- "ls": {cloneRepository(defaultURL, tempFolder()), "HEAD", "vendor"},
- "merge_base": {cloneRepository(defaultURL, tempFolder()), "--is-ancestor", "HEAD~3", "HEAD^"},
+ "blame": {defaultURL, "CHANGELOG"},
+ "branch": {defaultURL, tempFolder()},
+ "checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
+ "checkout-branch": {defaultURL, tempFolder(), "branch"},
+ "clone": {defaultURL, tempFolder()},
+ "commit": {cloneRepository(defaultURL, tempFolder())},
+ "context": {defaultURL, tempFolder()},
+ "custom_http": {defaultURL},
+ "find-if-any-tag-point-head": {cloneRepository(defaultURL, tempFolder())},
+ "ls": {cloneRepository(defaultURL, tempFolder()), "HEAD", "vendor"},
+ "ls-remote": {defaultURL},
+ "merge_base": {cloneRepository(defaultURL, tempFolder()), "--is-ancestor", "HEAD~3", "HEAD^"},
+ "open": {cloneRepository(defaultURL, tempFolder())},
+ "progress": {defaultURL, tempFolder()},
+ "pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
+ "push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
+ "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
+ "sha256": {tempFolder()},
+ "showcase": {defaultURL, tempFolder()},
+ "tag": {cloneRepository(defaultURL, tempFolder())},
}
-var ignored = map[string]bool{}
+// tests not working / set-up
+var ignored = map[string]bool{
+ "azure_devops": true,
+ "ls": true,
+ "sha256": true,
+ "submodule": true,
+ "tag-create-push": true,
+}
+
+var (
+ tempFolders = []string{}
-var tempFolders = []string{}
+ _, callingFile, _, _ = runtime.Caller(0)
+ basepath = filepath.Dir(callingFile)
+)
func TestExamples(t *testing.T) {
flag.Parse()
@@ -44,13 +61,13 @@ func TestExamples(t *testing.T) {
defer deleteTempFolders()
- examples, err := filepath.Glob(examplesFolder())
+ exampleMains, err := filepath.Glob(filepath.Join(basepath, "*", "main.go"))
if err != nil {
t.Errorf("error finding tests: %s", err)
}
- for _, example := range examples {
- dir := filepath.Dir(example)
+ for _, main := range exampleMains {
+ dir := filepath.Dir(main)
_, name := filepath.Split(dir)
if ignored[name] {
@@ -71,20 +88,6 @@ func tempFolder() string {
return path
}
-func packageFolder() string {
- return filepath.Join(
- build.Default.GOPATH,
- "src", "github.com/go-git/go-git/v5",
- )
-}
-
-func examplesFolder() string {
- return filepath.Join(
- packageFolder(),
- "_examples", "*", "main.go",
- )
-}
-
func cloneRepository(url, folder string) string {
cmd := exec.Command("git", "clone", url, folder)
err := cmd.Run()