From 3c1e9d63946a35ac4febefbdd6f56d16252e9abd Mon Sep 17 00:00:00 2001 From: Jarrad Hope Date: Mon, 25 Feb 2019 13:52:22 +0700 Subject: [PATCH] handle environment, ready for stdio communication --- .gitignore | 3 +++ crvc.nimble | 14 +++++++++++ src/crvc.nim | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 .gitignore create mode 100644 crvc.nimble create mode 100644 src/crvc.nim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc72010 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +nimcache +bin +src/crvc \ No newline at end of file diff --git a/crvc.nimble b/crvc.nimble new file mode 100644 index 0000000..c0effe1 --- /dev/null +++ b/crvc.nimble @@ -0,0 +1,14 @@ +# Package + +version = "0.1.0" +author = "Jarrad Hope" +description = "Cryptoeconomic Release and Version Control" +license = "MIT" +srcDir = "src" +binDir = "bin" +bin = @["crvc"] # TODO rename to "git-remote-crvc" ? + + +# Dependencies + +requires "nim >= 0.19.0" diff --git a/src/crvc.nim b/src/crvc.nim new file mode 100644 index 0000000..149f760 --- /dev/null +++ b/src/crvc.nim @@ -0,0 +1,68 @@ +import os, strformat, strutils, rdstdin + +const + gitEnv = "GIT_DIR" + protocol = "crvc" + +proc setup(gitEnv: string, protocol: string): (string, string, string, string) = + # Checks the Environment and returns the git Working directory, the protocol, the remote and url + + if paramCount() < 2: + raise newException(ValueError, fmt"Usage: {getAppFilename()} ") + + let + remote = paramStr(1) # + url = paramStr(2) # + gitDir = getEnv(gitEnv) + + # Check if GIT_DIR Environment Variable is set + if gitDir.isNilOrWhitespace: + raise newException(OSError, fmt"{gitEnv} is not set") + + # Check if the .git directory exists + if not existsDir(gitDir): + raise newException(OSError, fmt"{gitDir} does not appear to be a git repository") + + # Create our Working Directory if it doesn't exist + let path = joinPath(gitDir, protocol, remote) + if not existsDir(path): + createDir(path) + return (gitDir, protocol, remote, url) # I don't like result, too immutable + +proc processStdio (env: tuple): void = + # Supports the Git Remote Helper stdio protocol + # https://git-scm.com/docs/git-remote-helpers + let (gitDir, protocol, remote, url) = env + + while true: + var + response: string + line: string = readLine(stdin) # TODO something better here ? https://nim-lang.org/docs/streams.html + # echo line + + # Git sends the remote helper a list of commands on standard input, one per line. + # The first command is always the capabilities command, in response to which the remote helper must print a list of the capabilities it supports followed by a blank line. + case line: + of "capabilities": + response = "capabilities response" + of "list": + response = "list response" + of "export": + response = "export response" + else: + raise newException(IOError, fmt"Unrecognised command: {line}") + write(stdout, response) + + +proc bail() {.noconv.} = + quit() + +when isMainModule: + setControlCHook(bail) + + try: + processStdio(setup(gitEnv, protocol)) + except: + echo getCurrentExceptionMsg() + finally: + quit() \ No newline at end of file