diff --git a/main.go b/main.go index abe4fbb..e85f3f2 100644 --- a/main.go +++ b/main.go @@ -57,11 +57,12 @@ func usage() { var ( ref2hash = make(map[string]string) - ipfsShell = shell.NewShell("localhost:5001") - ipfsRepoPath string - thisGitRepo string - errc chan<- error - log = logging.Logger("git-remote-ipfs") + ipfsShell = shell.NewShell("localhost:5001") + ipfsRepoPath string + thisGitRepo string + thisGitRemote string + errc chan<- error + log = logging.Logger("git-remote-ipfs") ) func main() { @@ -84,9 +85,10 @@ func main() { v := len(os.Args[1:]) switch v { case 2: - log.Debug("repo:", os.Args[1]) - log.Debug("url:", os.Args[2]) + thisGitRemote = os.Args[1] u = os.Args[2] + log.Debug("remote:", thisGitRemote) + log.Debug("url:", u) default: log.Fatalf("usage: unknown # of args: %d\n%v", v, os.Args[1:]) } diff --git a/push.go b/push.go index b94f562..e0de990 100644 --- a/push.go +++ b/push.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "os/exec" "path/filepath" "strings" @@ -69,47 +70,49 @@ func push(src, dst string) error { return errgo.Notef(err, "patchLink failed") } root = newRoot - log.WithField("newRoot", newRoot).WithField("sha1", sha1).Error("updated object") + log.WithField("newRoot", newRoot).WithField("sha1", sha1).Debug("updated object") } srcSha1, err := gitRefHash(src) if err != nil { return errgo.Notef(err, "gitRefHash(%s) failed", src) } - h, ok := ref2hash[dst] if !ok { return errgo.Newf("writeRef: ref2hash entry missing: %s", dst) } isFF := gitIsAncestor(h, srcSha1) if isFF != nil && !force { - // print "non-fast-forward" to git + // TODO: print "non-fast-forward" to git return fmt.Errorf("non-fast-forward") } - mhash, err := ipfsShell.Add(bytes.NewBufferString(fmt.Sprintf("%s\n", srcSha1))) if err != nil { return errgo.Notef(err, "shell.Add(%s) failed", srcSha1) } - root, err = ipfsShell.PatchLink(root, dst, mhash, true) if err != nil { - // print "fetch first" to git + // TODO:print "fetch first" to git err = errgo.Notef(err, "patchLink(%s) failed", ipfsRepoPath) log.WithField("err", err).Error("shell.PatchLink failed") return fmt.Errorf("fetch first") } - - log.WithField("newRoot", root).WithField("dst", dst).WithField("hash", srcSha1).Error("updated ref") - + log.WithField("newRoot", root).WithField("dst", dst).WithField("hash", srcSha1).Debug("updated ref") // invalidate info/refs and HEAD(?) - // TODO: unclean: need to put other revs, too - root, err = ipfsShell.Patch(root, "rm-link", "info/refs") - if err != nil { + // TODO: unclean: need to put other revs, too make a soft git update-server-info maybe + noInfoRefsHash, err := ipfsShell.Patch(root, "rm-link", "info/refs") + if err == nil { + log.WithField("newRoot", noInfoRefsHash).Info("rm-link'ed info/refs") + root = noInfoRefsHash + } else { // todo shell.IsNotExists() ? log.WithField("err", err).Warning("shell.Patch rm-link info/refs failed - might be okay... TODO") } - - log.WithField("newRoot", root).Error("rm-link'ed info/refs") - + newRemoteURL := fmt.Sprintf("ipfs://%s", root) + setUrlCmd := exec.Command("git", "remote", "set-url", thisGitRemote, newRemoteURL) + out, err := setUrlCmd.CombinedOutput() + if err != nil { + return errgo.Notef(err, "updating remote url failed\nOut:%s", string(out)) + } + log.Info("remote updated - new address:", newRemoteURL) return nil }