push & pull

This commit is contained in:
Paul Liverman III 2018-03-01 19:37:09 -08:00
parent 26c5fae9c4
commit 380708bae6
5 changed files with 171 additions and 0 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
*.lua
!push.lua
!pull.lua
!add.lua

45
pull.lua Normal file
View File

@ -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

38
pull.moon Normal file
View File

@ -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"

46
push.lua Normal file
View File

@ -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

39
push.moon Normal file
View File

@ -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}"