Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 119 lines (73 sloc) 2.6 KB
#!/usr/bin/env coffee
# vim: ft=coffee
request = require 'request'
_ = require 'underscore'
{exec} = require 'child_process'
{parallel} = require 'async'
{resolve, existsSync} = require 'path'
fs = require 'fs'
repos = new Function( "return " + fs.readFileSync(__dirname + '/repos.js', 'utf8') )()
localRepos = __dirname + '/repos.local.js'
if existsSync(localRepos)
reposLocal = new Function( "return " + fs.readFileSync(localRepos, 'utf8') )()
else
reposLocal = {}
configFile = __dirname + '/config.js'
if existsSync(configFile)
config = new Function( "return " + fs.readFileSync(configFile, 'utf8') )()
else
config = {}
for name, repo of repos
_.extend repo, reposLocal[name] ? {}
repo.path = resolve(repo.path)
getSha = (url, callback) ->
request url: url, (error, response, body) ->
if not error and response.statusCode is 200
callback null, /[a-z0-9]{40}/.exec(body)?[0]
else
callback (error or response.statusCode)
refTypeMap =
remotes: 'remote'
tags: 'tag'
heads: 'head'
refRe = /^refs\/([^\/]+)\/(.*)$/
parseRef = (ref) ->
if m = ref?.match(refRe)
ref: m[2], type: (refTypeMap[m[1]] || m[1])
else
ref: ref
if config.comfy_chair?
tellTheChair = (site, tags, sha) ->
rightNow = Math.floor( new Date().getTime() / 1000 )
json = {}
json[site.name] = ref: tags, sha: sha, name: site.name, lastUpdated: rightNow
options =
url: config.comfy_chair,
method: 'post',
json: json
request options, (err, response, body) ->
if err
console.log "unable to inform comfy_chair: #{err}"
getProdVersion = (refs, site) ->
->
getSha site.url, (err, sha) ->
if err
console.log "error getting #{site.url}: #{err}"
else if refsForSha = refs[sha]
tags = (ref.ref.replace(/\^.*$/,'') for ref in refsForSha when ref? && ref.type == 'tag')
tags = tags.pop() if tags.length == 1
console.log "%s - %s (%s)", site.name, tags, sha[0...7]
tellTheChair?( site, tags, sha )
else
console.log "warning #{site.name}: no refs for sha #{sha}. Maybe you need to git fetch --tags"
console.log "%s - (%s)", site.name, sha[0...7]
tellTheChair?( site, [], sha )
getVersionsForRepo = (repo) ->
exec "git show-ref --dereference", cwd: repo.path, (err, stdout, stderr) ->
refs = {}
for line in stdout.split("\n")
[sha,ref] = line.split(/\s+/)
refs[sha] ||= []
refs[sha].push parseRef(ref)
parallel (getProdVersion(refs, site) for site in repo.sites)
getVersionsForRepo(repo) for key,repo of repos when not repo.ignore