From 380708bae614df5dacee915eece9f5e2906712db Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Thu, 1 Mar 2018 19:37:09 -0800 Subject: [PATCH] push & pull --- .gitignore | 3 +++ pull.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ pull.moon | 38 ++++++++++++++++++++++++++++++++++++++ push.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ push.moon | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+) create mode 100644 pull.lua create mode 100644 pull.moon create mode 100644 push.lua create mode 100644 push.moon diff --git a/.gitignore b/.gitignore index d907c43..53b6cd9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.lua +!push.lua +!pull.lua +!add.lua diff --git a/pull.lua b/pull.lua new file mode 100644 index 0000000..d3b1c27 --- /dev/null +++ b/pull.lua @@ -0,0 +1,45 @@ +local config = require("locator_config") +local execute +execute = function(cmd, capture_exit_code) + if capture_exit_code == nil then + capture_exit_code = true + end + local handle + if capture_exit_code then + handle = io.popen(tostring(cmd) .. "\necho $?") + else + handle = io.popen(cmd) + end + local result = handle:read("*a") + handle:close() + local exit_start, exit_end = result:find("(%d*)[%c]$") + local exit_code = tonumber(result:sub(exit_start, exit_end):sub(1, -2)) + local output = result:sub(1, exit_start - 1) + if exit_code == 0 then + return output + else + return error("sub-process '" .. tostring(cmd) .. "' returned status " .. tostring(exit_code) .. ".\n\n" .. tostring(output)) + end +end +local list = execute("git remote") +local remotes = { } +for line in list:gmatch("[^\n]+") do + remotes[line] = true +end +for _index_0 = 1, #config do + local item = config[_index_0] + if item.remote and item.remote.fetch then + if not (item.remote.branch) then + item.remote.branch = "master" + end + if item.remote.name then + if not (remotes[item.remote.name]) then + execute("git remote add -f " .. tostring(item.remote.name) .. " " .. tostring(item.remote.fetch)) + if item.remote.push and not ("boolean" == type(item.remote.push)) then + execute("git remote set-url --push " .. tostring(item.remote.name) .. " " .. tostring(item.remote.push)) + end + end + end + execute("git subtree pull --prefix " .. tostring(item.path:gsub("%.", "/")) .. " " .. tostring(item.remote.fetch) .. " " .. tostring(item.remote.branch) .. " --squash") + end +end diff --git a/pull.moon b/pull.moon new file mode 100644 index 0000000..54e3fbc --- /dev/null +++ b/pull.moon @@ -0,0 +1,38 @@ +config = require "locator_config" + +execute = (cmd, capture_exit_code=true) -> + local handle + if capture_exit_code + handle = io.popen "#{cmd}\necho $?" + else + handle = io.popen cmd + result = handle\read "*a" + handle\close! + + exit_start, exit_end = result\find "(%d*)[%c]$" + exit_code = tonumber result\sub(exit_start, exit_end)\sub 1, -2 + output = result\sub 1, exit_start - 1 + + if exit_code == 0 + return output + else + error "sub-process '#{cmd}' returned status #{exit_code}.\n\n#{output}" + +list = execute "git remote" +remotes = {} +for line in list\gmatch "[^\n]+" + remotes[line] = true + +for item in *config + if item.remote and item.remote.fetch -- if configured to pull + unless item.remote.branch + item.remote.branch = "master" + + if item.remote.name -- if we want a named remote + unless remotes[item.remote.name] -- add it if needed + execute "git remote add -f #{item.remote.name} #{item.remote.fetch}" + if item.remote.push and not ("boolean" == type item.remote.push) + execute "git remote set-url --push #{item.remote.name} #{item.remote.push}" + + -- we actually ignore names with in-script usage.. + execute "git subtree pull --prefix #{item.path\gsub "%.", "/"} #{item.remote.fetch} #{item.remote.branch} --squash" diff --git a/push.lua b/push.lua new file mode 100644 index 0000000..4bf1fd1 --- /dev/null +++ b/push.lua @@ -0,0 +1,46 @@ +local config = require("locator_config") +local execute +execute = function(cmd, capture_exit_code) + if capture_exit_code == nil then + capture_exit_code = true + end + local handle + if capture_exit_code then + handle = io.popen(tostring(cmd) .. "\necho $?") + else + handle = io.popen(cmd) + end + local result = handle:read("*a") + handle:close() + local exit_start, exit_end = result:find("(%d*)[%c]$") + local exit_code = tonumber(result:sub(exit_start, exit_end):sub(1, -2)) + local output = result:sub(1, exit_start - 1) + if exit_code == 0 then + return output + else + return error("sub-process '" .. tostring(cmd) .. "' returned status " .. tostring(exit_code) .. ".\n\n" .. tostring(output)) + end +end +local list = execute("git remote") +local remotes = { } +for line in list:gmatch("[^\n]+") do + remotes[line] = true +end +for _index_0 = 1, #config do + local item = config[_index_0] + if item.remote and item.remote.push then + if "boolean" == type(item.remote.push) then + item.remote.push = item.remote.fetch + end + if not (item.remote.branch) then + item.remote.branch = "master" + end + if item.remote.name then + if not (remotes[item.remote.name]) then + execute("git remote add -f " .. tostring(item.remote.name) .. " " .. tostring(item.remote.fetch)) + execute("git remote set-url --push " .. tostring(item.remote.name) .. " " .. tostring(item.remote.push)) + end + end + execute("git subtree push --prefix " .. tostring(item.path:gsub("%.", "/")) .. " " .. tostring(item.remote.push) .. " " .. tostring(item.remote.branch)) + end +end diff --git a/push.moon b/push.moon new file mode 100644 index 0000000..8cbf99d --- /dev/null +++ b/push.moon @@ -0,0 +1,39 @@ +config = require "locator_config" + +execute = (cmd, capture_exit_code=true) -> + local handle + if capture_exit_code + handle = io.popen "#{cmd}\necho $?" + else + handle = io.popen cmd + result = handle\read "*a" + handle\close! + + exit_start, exit_end = result\find "(%d*)[%c]$" + exit_code = tonumber result\sub(exit_start, exit_end)\sub 1, -2 + output = result\sub 1, exit_start - 1 + + if exit_code == 0 + return output + else + error "sub-process '#{cmd}' returned status #{exit_code}.\n\n#{output}" + +list = execute "git remote" +remotes = {} +for line in list\gmatch "[^\n]+" + remotes[line] = true + +for item in *config + if item.remote and item.remote.push -- if configured to push + if "boolean" == type item.remote.push + item.remote.push = item.remote.fetch + unless item.remote.branch + item.remote.branch = "master" + + if item.remote.name -- if we want a named remote + unless remotes[item.remote.name] -- add it if needed + execute "git remote add -f #{item.remote.name} #{item.remote.fetch}" + execute "git remote set-url --push #{item.remote.name} #{item.remote.push}" + + -- we actually ignore names with in-script usage.. + execute "git subtree push --prefix #{item.path\gsub "%.", "/"} #{item.remote.push} #{item.remote.branch}"