From 381a9b85d4ea82c5792fcc75383b2d0d98b72100 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 15 Dec 2016 04:36:19 -0500 Subject: more --- git-mirror-cgit | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 git-mirror-cgit (limited to 'git-mirror-cgit') diff --git a/git-mirror-cgit b/git-mirror-cgit new file mode 100755 index 0000000..8c1b799 --- /dev/null +++ b/git-mirror-cgit @@ -0,0 +1,69 @@ +#!/usr/bin/env ruby +# coding: utf-8 + +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 + 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.sort_by{|url| prefs.index(url)} + 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.sort_by{|url| prefs.index(url)} + 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 -- cgit v1.2.3