summaryrefslogtreecommitdiff
path: root/git-mirror-cgit
blob: 3a953b284aec82c514f4cedd10876d716a060ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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
		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