Skip to content

Test push / fetch #7

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
Mar 17, 2018
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
Binary file added cmd/git-remote-ipld/git-remote-ipld
Binary file not shown.
9 changes: 5 additions & 4 deletions cmd/git-remote-ipld/ipld.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"fmt"
"github.com/magik6k/git-remote-ipld/core"
"gopkg.in/src-d/go-git.v4/plumbing"
"os"
)

type IpldHandler struct {
// remoteHash is hash form remote name
// remoteHash is hash from remote name
remoteHash string
// command line arguments
osArgs []string
}

func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error) {
Expand All @@ -38,7 +39,7 @@ func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error)
}

// pull ipld::hash, we only want to update HEAD
if !forPush && headRef.Target() == ref.Name() && headRef.Type() == plumbing.SymbolicReference && len(os.Args) >= 3 {
if !forPush && headRef.Target() == ref.Name() && headRef.Type() == plumbing.SymbolicReference && len(h.osArgs) >= 3 {
sha, err := hex.DecodeString(h.remoteHash)
if err != nil {
return err
Expand All @@ -61,7 +62,7 @@ func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error)
}

// For clone
if n == 0 && !forPush && len(os.Args) >= 3 {
if n == 0 && !forPush && len(h.osArgs) >= 3 {
sha, err := hex.DecodeString(h.remoteHash)
if err != nil {
return nil, err
Expand Down
101 changes: 101 additions & 0 deletions cmd/git-remote-ipld/ipld_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"bytes"
"github.com/magik6k/git-remote-ipld/util"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
)

func TestCapabilities(t *testing.T) {
tmpdir := setupTest(t)
defer os.RemoveAll(tmpdir)

// git clone ipld::d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8
args := []string{"git-remote-ipld", "origin", "d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8"}

listExp := []string{
"0000000000000000000000000000000000000000 refs/heads/french",
"0000000000000000000000000000000000000000 refs/heads/italian",
"d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8 refs/heads/master",
"@refs/heads/master HEAD",
}
listForPushExp := []string{
"0000000000000000000000000000000000000000 refs/heads/french",
"0000000000000000000000000000000000000000 refs/heads/italian",
"0000000000000000000000000000000000000000 refs/heads/master",
"@refs/heads/master HEAD",
}

testCase(t, args, "capabilities", []string{"push", "fetch"})
testCase(t, args, "list", listExp)
testCase(t, args, "list for-push", listForPushExp)

// mock/git> git push --set-upstream ipld:: master
testCase(t, args, "push refs/heads/master:refs/heads/master", []string{})

testCase(t, args, "fetch d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8 refs/heads/master\n", []string{""})
comparePullToMock(t, tmpdir, "git")
}

func testCase(t *testing.T, args []string, input string, expected []string) {
reader := strings.NewReader(input + "\n")
var writer bytes.Buffer
logger := log.New(ioutil.Discard, "", 0)
err := Main(args, reader, &writer, logger)
if err != nil && err != io.EOF {
t.Fatal(err)
}

response := writer.String()
exp := strings.Join(expected, "\n")
if strings.TrimSpace(response) != exp {
t.Fatalf("Args: %s\nInput:\n%s\nExpected:\n%s\nActual:\n'%s'\n", args, input, exp, response)
}
}

func comparePullToMock(t *testing.T, tmpdir, mock string) {
wd, _ := os.Getwd()
mockdir := filepath.Join(wd, "..", "..", "mock", mock)
compareContents(t, filepath.Join(tmpdir, ".git"), mockdir)
}

func compareContents(t *testing.T, src, dst string) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
err := util.CompareDirs(src, dst, []string{"ipld"})
if err != nil {
t.Fatal(err)
}
}

func setupTest(t *testing.T) string {
wd, _ := os.Getwd()
src := filepath.Join(wd, "..", "..", "mock", "git")
si, err := os.Stat(src)
if err != nil {
t.Fatal(err)
}
if !si.IsDir() {
t.Fatal("source is not a directory")
}

tmpdir, err := ioutil.TempDir("", "git-test")
if err != nil {
t.Fatal(err)
}

dst := filepath.Join(tmpdir, ".git")
err = util.CopyDir(src, dst)
if err != nil {
t.Fatal(err)
}

os.Setenv("GIT_DIR", dst)
return tmpdir
}
12 changes: 6 additions & 6 deletions cmd/git-remote-ipld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"log"
"os"
"strings"
Expand All @@ -14,27 +15,26 @@ const (
IPFS_PREFIX = "ipfs://"
)

func Main() error {
if len(os.Args) < 3 {
func Main(args []string, reader io.Reader, writer io.Writer, logger *log.Logger) error {
if len(args) < 3 {
return fmt.Errorf("Usage: git-remote-ipld remote-name url")
}

hashArg := os.Args[2]
hashArg := args[2]
if strings.HasPrefix(hashArg, IPLD_PREFIX) || strings.HasPrefix(hashArg, IPFS_PREFIX) {
hashArg = hashArg[len(IPLD_PREFIX):]
}

remote, err := core.NewRemote(&IpldHandler{remoteHash: hashArg})
remote, err := core.NewRemote(&IpldHandler{remoteHash: hashArg, osArgs: args}, reader, writer, logger)
if err != nil {
return err
}
defer remote.Close()

return remote.ProcessCommands()
}

func main() {
if err := Main(); err != nil {
if err := Main(os.Args, nil, nil, nil); err != nil {
fmt.Fprintf(os.Stderr, "\x1b[K")
log.Fatal(err)
}
Expand Down
Binary file added cmd/git-remote-ipns/git-remote-ipns
Binary file not shown.
10 changes: 5 additions & 5 deletions cmd/git-remote-ipns/ipns.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
"path"
"fmt"
"path"
"strings"

ipfs "github.com/ipfs/go-ipfs-api"
"github.com/magik6k/git-remote-ipld/core"

cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
"gopkg.in/src-d/go-git.v4/plumbing"
"encoding/hex"
"bytes"
"encoding/hex"
"gopkg.in/src-d/go-git.v4/plumbing"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
)

const (
Expand All @@ -20,7 +20,7 @@ const (
)

type refPath struct {
path string
path string
rType int

hash string
Expand Down
9 changes: 5 additions & 4 deletions cmd/git-remote-ipns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package main

import (
"fmt"
"github.com/magik6k/git-remote-ipld/core"
"io"
"log"
"os"
"strings"
"github.com/magik6k/git-remote-ipld/core"
)

const (
IPNS_PREFIX = "ipns://"
)

func Main() error {
func Main(reader io.Reader, writer io.Writer, logger *log.Logger) error {
if len(os.Args) < 3 {
return fmt.Errorf("Usage: git-remote-ipns remote-name url")
}
Expand All @@ -22,7 +23,7 @@ func Main() error {
remoteName = remoteName[len(IPNS_PREFIX):]
}

remote, err := core.NewRemote(&IpnsHandler{remoteName:remoteName})
remote, err := core.NewRemote(&IpnsHandler{remoteName: remoteName}, reader, writer, logger)
if err != nil {
return err
}
Expand All @@ -31,7 +32,7 @@ func Main() error {
}

func main() {
if err := Main(); err != nil {
if err := Main(nil, nil, nil); err != nil {
fmt.Fprintf(os.Stderr, "\x1b[K")
log.Fatal(err)
}
Expand Down
29 changes: 22 additions & 7 deletions core/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"path"
Expand All @@ -18,6 +19,8 @@ type RemoteHandler interface {
}

type Remote struct {
reader io.Reader
writer io.Writer
Logger *log.Logger
localDir string

Expand All @@ -29,7 +32,7 @@ type Remote struct {
todo []func() (string, error)
}

func NewRemote(handler RemoteHandler) (*Remote, error) {
func NewRemote(handler RemoteHandler, reader io.Reader, writer io.Writer, logger *log.Logger) (*Remote, error) {
localDir, err := GetLocalDir()
if err != nil {
return nil, err
Expand All @@ -50,20 +53,32 @@ func NewRemote(handler RemoteHandler) (*Remote, error) {
return nil, fmt.Errorf("fetch: %v", err)
}

if reader == nil {
reader = os.Stdin
}
if writer == nil {
writer = os.Stdout
}
if logger == nil {
logger = log.New(os.Stderr, "", 0)
}

return &Remote{
Logger: log.New(os.Stderr, "", 0),
reader: reader,
writer: writer,
Logger: logger,
localDir: localDir,

Repo: repo,
Tracker: tracker,

Handler: handler,
}, nil
}, nil
}

func (r *Remote) Printf(format string, a ...interface{}) (n int, err error) {
r.Logger.Printf("> "+format, a...)
return fmt.Printf(format, a...)
return fmt.Fprintf(r.writer, format, a...)
}

func (r *Remote) NewPush() *Push {
Expand Down Expand Up @@ -108,9 +123,9 @@ func (r *Remote) fetch(sha, ref string) {
}

func (r *Remote) ProcessCommands() error {
stdinReader := bufio.NewReader(os.Stdin)
reader := bufio.NewReader(r.reader)
for {
command, err := stdinReader.ReadString('\n')
command, err := reader.ReadString('\n')
if err != nil {
return err
}
Expand Down Expand Up @@ -141,7 +156,7 @@ func (r *Remote) ProcessCommands() error {
case command == "":
fallthrough
case command == "\n":
r.Logger.Println("Processsing tasks")
r.Logger.Println("Processing tasks")
for _, task := range r.todo {
resp, err := task()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
"path"

"fmt"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
"fmt"
)

func compressObject(in []byte) []byte {
Expand Down
1 change: 1 addition & 0 deletions mock/git/COMMIT_EDITMSG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
English -> Italian
1 change: 1 addition & 0 deletions mock/git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions mock/git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
1 change: 1 addition & 0 deletions mock/git/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
15 changes: 15 additions & 0 deletions mock/git/hooks/applypatch-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script to check the commit log message taken by
# applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit. The hook is
# allowed to edit the commit message file.
#
# To enable this hook, rename this file to "applypatch-msg".

. git-sh-setup
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
:
24 changes: 24 additions & 0 deletions mock/git/hooks/commit-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
Loading