git_remote_crvc.nim
1 import os, strformat, strutils, rdstdin 2 3 const 4 gitEnv = "GIT_DIR" 5 protocol = "crvc" 6 7 proc setup(gitEnv: string, protocol: string): (string, string, string) = 8 # Checks the Environment and returns the git Working directory, the protocol, the remote and url 9 10 if paramCount() < 2: 11 raise newException(ValueError, fmt"Usage: {getAppFilename()} <remote-name> <url>") 12 13 let 14 remote = paramStr(1) # <remote-name> 15 url = paramStr(2) # <url> 16 gitDir = getEnv(gitEnv) 17 18 # Check if GIT_DIR Environment Variable is set 19 if gitDir.isNilOrWhitespace: 20 raise newException(OSError, fmt"{gitEnv} is not set") 21 22 # Check if the .git directory exists 23 if not existsDir(gitDir): 24 raise newException(OSError, fmt"{gitDir} does not appear to be a git repository") 25 26 # Create our Working Directory if it doesn't exist 27 let path = joinPath(gitDir, remote) 28 if not existsDir(path): 29 createDir(path) 30 # TODO process url and pass to processStdio parseUrl? 31 return (path, remote, url) 32 33 proc processStdio (env: tuple): void = 34 # Supports the Git Remote Helper stdio protocol 35 # https://git-scm.com/docs/git-remote-helpers 36 let (path, remote, url) = env 37 38 while true: 39 var 40 response: string 41 line: string = readLine(stdin) # TODO something better here ? https://nim-lang.org/docs/streams.html 42 # echo line 43 44 # Git sends the remote helper a list of commands on standard input, one per line. 45 # 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. 46 case line: 47 of "capabilities": 48 response = "capabilities response" 49 of "list": 50 response = "list response" 51 of "export": 52 response = "export response" 53 else: 54 raise newException(IOError, fmt"Unrecognised command: {line}") 55 write(stdout, response) 56 57 58 proc bail() {.noconv.} = 59 quit() 60 61 when isMainModule: 62 setControlCHook(bail) 63 64 try: 65 processStdio(setup(gitEnv, protocol)) 66 except: 67 echo getCurrentExceptionMsg() 68 finally: 69 quit()