#!/usr/bin/env ruby # coding: utf-8 $:.unshift File.dirname(__FILE__) load 'git-mirror-backend.rb' require 'uri' require 'open-uri' require 'nokogiri' class Cgit < GitMirrorBackend def initialize() @config = {} end def cmd_config(*args) args.each do |arg| key, val = arg.split('=', 2) case key when "url" val = URI(val) unless val.path.end_with?("/") val.path += "/" end end @config[key] = val end return nil end def cmd_get_meta(path) doc = Nokogiri::HTML(open(@config['url']+path)) head = open(@config['url']+(path+'/HEAD')).read return { 'description' => doc.css('#header .sub')[0].text, 'owner' => doc.css('#header .sub')[1].text, 'default-branch' => head.split("\n")[0].sub(/^ref: /, '').sub(/^refs\/heads\//, ''), } end def cmd_set_meta(path, *pairs) raise "no can do" end def urls(path) doc = Nokogiri::HTML(open(@config['url']+path)) return doc.css('a[rel="vcs-git"]').map{|a| a['href']} end def cmd_push_url(path) prefs = ['ssh', 'https', 'http', 'git'] return self.urls(path).sort_by{|u| prefs.index(u.split(':', 2)[0])}.first end def cmd_pull_url(path) # prefer https ahead of git because of a bug in git-daemon # with '~'. prefs = ['https', 'git', 'http', 'ssh'] return self.urls(path).sort_by{|u| prefs.index(u.split(':', 2)[0])}.first end def cmd_repo_mode(path) return "passive" end end if __FILE__ == $0 if ARGV.length != 1 throw "Usage: $0 ACCOUNT_NAME" end Cgit.new().repl(ARGV[1]) end