dolt/{bats,go}: Git-Dolt v0!

This commit is contained in:
Matt Jesuele
2019-06-06 12:53:55 -07:00
parent 2a70479dd9
commit 888ebe309d
2 changed files with 124 additions and 0 deletions

53
bats/git-dolt.bats Executable file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env bats
setup() {
if [ -z "$BATS_TMPDIR" ]; then
export BATS_TMPDIR=$HOME/batstmp/
mkdir $BATS_TMPDIR
fi
export PATH=$PATH:~/go/bin
export NOMS_VERSION_NEXT=1
cd $BATS_TMPDIR
mkdir remotes-$$
echo remotesrv log available here $BATS_TMPDIR/remotes-$$/remotesrv.log
remotesrv --http-port 1234 --dir ./remotes-$$ &> ./remotes-$$/remotesrv.log 3>&- &
mkdir dolt-repo-$$
cd dolt-repo-$$
dolt init
dolt remote add test-remote localhost:50051/test-org/test-repo --insecure
dolt push test-remote master
}
teardown() {
rm -rf $BATS_TMPDIR/{git,dolt}-repo-$$
pkill -2 remotesrv
rm -rf $BATS_TMPDIR/remotes-$$
}
@test "git dolt link takes a remote url (and an optional revspec and destination directory), clones the repo, and outputs a pointer file" {
mkdir ../git-repo-$$
cd ../git-repo-$$
git init
run git dolt link localhost:50051/test-org/test-repo
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ "Success!" ]] || false
[[ "${lines[1]}" =~ "Dolt repository cloned to test-repo" ]] || false
[[ "${lines[2]}" =~ "Pointer file created at test-repo.git-dolt" ]] || false
[[ "${lines[3]}" =~ "test-repo added to .gitignore" ]] || false
[[ "${lines[4]}" =~ "You should git commit these results" ]] || false
[ -d test-repo ]
run cat test-repo.git-dolt
[[ "${lines[0]}" =~ "version 0" ]] || false
[[ "${lines[1]}" =~ "remote localhost:50051/test-org/test-repo" ]] || false
[[ "${lines[2]}" =~ ^(revision [0-9a-v]{32})$ ]] || false
run cat .gitignore
[[ "${lines[0]}" =~ "test-repo" ]] || false
}
@test "git dolt prints usage information with no arguments" {
run git dolt
[[ "$output" =~ Usage ]]
}

View File

@@ -0,0 +1,71 @@
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
const gitDoltVersion = 0
func main() {
nArgs := len(os.Args)
if nArgs == 1 {
fmt.Println("Dolt: It's Git for Data.")
fmt.Println("Usage")
return
}
cmd := os.Args[1]
if cmd == "link" {
remote := os.Args[2]
dirname := lastSegment(remote)
_, err := exec.Command("dolt", "clone", remote, "--insecure").Output()
check(err)
revision := currentRevision(dirname)
ptrFile, err := os.Create(fmt.Sprintf("%s.git-dolt", dirname))
check(err)
defer ptrFile.Close()
_, err = ptrFile.WriteString(fmt.Sprintf("version %d\nremote %s\nrevision %s\n", gitDoltVersion, remote, revision))
check(err)
giFile, err := os.OpenFile(".gitignore", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
check(err)
defer giFile.Close()
_, err = giFile.WriteString(fmt.Sprintf("%s\n", dirname))
check(err)
fmt.Printf("\nSuccess!\n\n")
fmt.Printf("* Dolt repository cloned to %s at revision %s\n", dirname, revision)
fmt.Printf("* Pointer file created at %s.git-dolt\n", dirname)
fmt.Printf("* %s added to .gitignore\n\nYou should git commit these results.\n", dirname)
}
}
var hashRegex = regexp.MustCompile(`[0-9a-v]{32}`)
func currentRevision(dirname string) string {
cmd := exec.Command("dolt", "log", "-n", "1")
cmd.Dir = dirname
out, err := cmd.Output()
check(err)
return hashRegex.FindString(string(out))
}
func lastSegment(s string) string {
return strings.Split(s, "/")[2]
}
func check(e error) {
if e != nil {
panic(e)
}
}