diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-03-07 20:55:02 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@simtec.co.uk> | 2012-03-07 20:55:02 +0000 |
commit | 1862e2b29845f226e53c4747ee7b1d2cc7cdb269 (patch) | |
tree | a6726e4999ea708d53357b77cb208b5a4e931e1a | |
parent | 53f14c314457fbb1e09a3c566bdf7c98676a4b46 (diff) | |
download | gitano-1862e2b29845f226e53c4747ee7b1d2cc7cdb269.tar.gz |
LEGIT: Simple low-level wrapper and deep copy utility function
-rw-r--r-- | lib/legit/git/ll.lua | 84 | ||||
-rw-r--r-- | lib/legit/util.lua | 29 |
2 files changed, 113 insertions, 0 deletions
diff --git a/lib/legit/git/ll.lua b/lib/legit/git/ll.lua new file mode 100644 index 0000000..6ad5c98 --- /dev/null +++ b/lib/legit/git/ll.lua @@ -0,0 +1,84 @@ +-- legit.git.ll +-- +-- Low level git interface routines for Legit +-- +-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org> +-- +-- + +local sp = require "luxio.subprocess" +local util = require "legit.util" + +local assert = assert + +local git_exe = "git" + +local function _rungit(t) + assert(t.repo, "No repository?") + + local proc_args = { + env = {}, + git_exe, unpack(t) + } + + for k, v in pairs(t.env or {}) do + proc_args.env[k] = v + end + + proc_args.env.GIT_DIR = t.repo + + if t.stdin then + proc_args.stdin = t.stdin + end + + if t.stdout then + proc_args.stdout = sp.PIPE + end + + local proc = sp.spawn_simple(proc_args) + local stdout + + if t.stdout then + stdout = proc.stdout:read("*a") + if type(t.stdout) == "function" then + stdout = t.stdout(stdout) + end + end + + local how, why = proc:wait() + + assert(how == "exit", "Not cleanly exited") + + return why, stdout +end + +local function _setgit(e) + git_exe = e +end + +local function _chomp(s) + local rest = s:match("^(.*)\n$") + return rest or s +end + +local mod_ret = { + rungit = _rungit, + setgit = _setgit, + chomp = _chomp +} + +local simple_cmds = { + "cat-file", "symbolic-ref", "show-ref", "hash-object", "ls-tree" +} + +for _, s in pairs(simple_cmds) do + local ss = s:gsub("%-", "_") + mod_ret[ss] = function(_t) + local t = util.deep_copy(_t) + table.insert(t, 1, s) + t.stdout = _chomp + return _rungit(t) + end +end + +return mod_ret diff --git a/lib/legit/util.lua b/lib/legit/util.lua new file mode 100644 index 0000000..2391a57 --- /dev/null +++ b/lib/legit/util.lua @@ -0,0 +1,29 @@ +-- legit.util +-- +-- Low level utility routines for Legit +-- +-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org> +-- +-- + +local function _deep_copy(t, memo) + if not memo then memo = {} end + if memo[t] then return memo[t] end + local ret = {} + local kk, vv + for k, v in pairs(t) do + kk, vv = k, v + if type(k) == "table" then + kk = _deep_copy(k) + end + if type(v) == "table" then + vv = _deep_copy(v) + end + ret[kk] = vv + end + return ret +end + +return { + deep_copy = _deep_copy +} |