Skip to content

Commit

Permalink
Add fetch
Browse files Browse the repository at this point in the history
Also add a test. Man, this took forever to implement.
  • Loading branch information
vHanda committed May 1, 2022
1 parent 0e59271 commit 9c22ecb
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 5 deletions.
10 changes: 5 additions & 5 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gotest.tools/v3/assert"
)

func prepareFixture(t *testing.T, name string) string {
func PrepareFixture(t *testing.T, name string) string {
newRepoPath, err := ioutil.TempDir(os.TempDir(), name)
assert.NilError(t, err)

Expand All @@ -28,7 +28,7 @@ func prepareFixture(t *testing.T, name string) string {
}

func Test_NoChanges(t *testing.T) {
repoPath := prepareFixture(t, "no_changes")
repoPath := PrepareFixture(t, "no_changes")

err := commit(repoPath)
assert.NilError(t, err)
Expand All @@ -43,7 +43,7 @@ func Test_NoChanges(t *testing.T) {
}

func Test_NewFile(t *testing.T) {
repoPath := prepareFixture(t, "new_file")
repoPath := PrepareFixture(t, "new_file")

err := commit(repoPath)
assert.NilError(t, err)
Expand All @@ -58,7 +58,7 @@ func Test_NewFile(t *testing.T) {
}

func Test_OneFileChange(t *testing.T) {
repoPath := prepareFixture(t, "one_file_change")
repoPath := PrepareFixture(t, "one_file_change")

err := commit(repoPath)
assert.NilError(t, err)
Expand All @@ -73,7 +73,7 @@ func Test_OneFileChange(t *testing.T) {
}

func Test_MultipleFileChange(t *testing.T) {
repoPath := prepareFixture(t, "multiple_file_change")
repoPath := PrepareFixture(t, "multiple_file_change")

err := commit(repoPath)
assert.NilError(t, err)
Expand Down
29 changes: 29 additions & 0 deletions fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/ztrue/tracerr"
"gopkg.in/src-d/go-git.v4"
)

func fetch(repoPath string) error {
r, err := git.PlainOpen(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

remotes, err := r.Remotes()
if err != nil {
return tracerr.Wrap(err)
}

for _, remote := range remotes {
remoteName := remote.Config().Name

err, _, errB := GitCommand(repoPath, []string{"fetch", remoteName})
if err != nil {
return tracerr.Errorf("Remote: %s %s %w", remoteName, errB.Bytes(), err)
}
}

return nil
}
69 changes: 69 additions & 0 deletions fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

cp "github.com/otiai10/copy"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gotest.tools/v3/assert"
)

func PrepareMultiFixtures(t *testing.T, name string, deps []string) string {
newTestDataPath, err := ioutil.TempDir(os.TempDir(), "mutli_fixture")
assert.NilError(t, err)

for _, name := range deps {
fixturePath := filepath.Join("testdata", name)
newPath := filepath.Join(newTestDataPath, name)
err = cp.Copy(fixturePath, newPath)
assert.NilError(t, err)

err = os.Rename(filepath.Join(newPath, ".gitted"), filepath.Join(newPath, ".git"))
assert.NilError(t, err)
}

newRepoPath := PrepareFixture(t, name)
FixFixtureGitConfig(t, newRepoPath, newTestDataPath)

return newRepoPath
}

func FixFixtureGitConfig(t *testing.T, newRepoPath string, testDataPath string) {
// Fix remote paths
dotGitPath := filepath.Join(newRepoPath, ".git")
gitConfigFilePath := filepath.Join(dotGitPath, "config")
input, err := ioutil.ReadFile(gitConfigFilePath)
assert.NilError(t, err)

output := bytes.Replace(input, []byte("$TESTDATA$"), []byte(testDataPath), -1)

err = ioutil.WriteFile(gitConfigFilePath, output, 0666)
assert.NilError(t, err)
}

func Test_SimpleFetch(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "simple_fetch", []string{"multiple_file_change"})
fmt.Println(repoPath)

err := fetch(repoPath)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
assert.NilError(t, err)

head, err := r.Head()
assert.NilError(t, err)

assert.Equal(t, head.Hash(), plumbing.NewHash("28cc969d97ddb7640f5e1428bbc8f2947d1ffd57"))

ref, err := r.Reference(plumbing.NewRemoteReferenceName("origin1", "master"), true)
assert.NilError(t, err)

assert.Equal(t, ref.Hash(), plumbing.NewHash("7058b6b292ee3d1382670334b5f29570a1117ef1"))
}
1 change: 1 addition & 0 deletions testdata/simple_fetch/.gitted/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Empty file.
8 changes: 8 additions & 0 deletions testdata/simple_fetch/.gitted/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin1"]
url = file://$TESTDATA$/multiple_file_change/.git
fetch = +refs/heads/*:refs/remotes/origin1/*
1 change: 1 addition & 0 deletions testdata/simple_fetch/.gitted/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file added testdata/simple_fetch/.gitted/index
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions testdata/simple_fetch/.gitted/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
28cc969d97ddb7640f5e1428bbc8f2947d1ffd57 refs/heads/master
Empty file.
Empty file.
1 change: 1 addition & 0 deletions testdata/simple_fetch/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1

0 comments on commit 9c22ecb

Please sign in to comment.