Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewchambers/8118437434738b08d5dc671e7e8bd138 to your computer and use it in GitHub Desktop.
Save andrewchambers/8118437434738b08d5dc671e7e8bd138 to your computer and use it in GitHub Desktop.
use sys
use std
// XXX move to stdlib
const rename = {from, to
var ret = sys.rename(from, to)
-> ret == 0
}
const readhist = {histfile
var tmp
if !std.fexists(histfile)
tmp = std.sldup(histfile)
tmp = std.sljoin(&tmp, ".tmp")
if !std.fexists(tmp)
std.fatal("history file {} not found\n", histfile)
;;
if !rename(tmp, histfile)
std.fatal("could not rename temporary history file {}\n", tmp)
;;
std.slfree(tmp)
;;
match std.slurp(histfile)
| `std.Ok commit:
-> commit
| `std.Fail err: std.fatal("error reading {}: {}\n", histfile, err)
;;
}
const updatehist = {histfile, commit
var tmp
tmp = std.sldup(histfile)
tmp = std.sljoin(&tmp, ".tmp")
if !std.blat(tmp, commit, 0o644)
std.fatal("could not write temporary history file\n")
;;
if !std.remove(histfile)
std.fatal("could not remove old history file\n")
;;
if !rename(tmp, histfile)
std.fatal("could not update history file\n")
;;
std.slfree(tmp)
}
const pollrepo = {repo, branch, lastcommit
var commits = [][:]
std.slpush(&commits, std.sldup("FAKECOMMIT1"))
std.slpush(&commits, std.sldup("FAKECOMMIT2"))
-> commits
}
const runexecscript = {script, repo, hash
std.put("{} {} {}\n", script, repo, hash)
-> (0, "FAKEOUTPUT")
}
const runnotifyscript = {script, success, log
std.put("{} {} <<EOF\n{}\nEOF\n", script, success, log)
}
const run = {repo, histfile, exec, notify
var lastcommit
var newcommits
var success, log
lastcommit = readhist(histfile)
newcommits = pollrepo(repo, "master", lastcommit)
for newcommit in newcommits
(success, log) = runexecscript(exec, repo, newcommit)
runnotifyscript(notify, success, log)
updatehist(histfile, newcommit)
std.slfree(newcommit)
;;
std.slfree(newcommits)
std.slfree(lastcommit)
}
const main = {args : byte[:][:]
var cmd
var repo = ""
var histfile = ""
var notify = ""
var exec = ""
cmd = std.optparse(args, &[
.opts = [
[.opt='r', .arg="repo", .desc="path to repository to monitor", .optional=false],
[.opt='f', .arg="history", .desc="path to history file which stores the last commit", .optional=false],
[.opt='n', .arg="notify", .desc="path to notify script", .optional=false],
[.opt='e', .arg="exec", .desc="path to exec script", .optional=false],
][:]
])
for opt in cmd.opts
match opt
| ('r', r): repo = r
| ('f', f): histfile = f
| ('n', n): notify = n
| ('e', e): exec = e
| _: std.fatal("bad opt\n")
;;
;;
match (repo, histfile, notify, exec)
| ("", _, _, _): std.fatal("please specify the repo\n")
| (_, "", _, _): std.fatal("please specify the history file\n")
| (_, _, "", _): std.fatal("please specify the notify script\n")
| (_, _, _, ""): std.fatal("please specify the exec script\n")
| _:
;;
run(repo, histfile, exec, notify)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment