Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

restructure git viewer URL generations mechanism.

  • Loading branch information...
commit accf429f79962cdeecbe22a2c451f2aad3eb418c 1 parent 2d1aeda
akr authored

Showing 1 changed file with 95 additions and 23 deletions. Show diff stats Hide diff stats

  1. +95 23 chkbuild/git.rb
118 chkbuild/git.rb
... ... @@ -1,6 +1,6 @@
1 1 # chkbuild/git.rb - git access method
2 2 #
3   -# Copyright (C) 2008-2010 Tanaka Akira <akr@fsij.org>
  3 +# Copyright (C) 2008-2012 Tanaka Akira <akr@fsij.org>
4 4 #
5 5 # Redistribution and use in source and binary forms, with or without
6 6 # modification, are permitted provided that the following conditions are met:
@@ -25,9 +25,10 @@
25 25 # OF SUCH DAMAGE.
26 26
27 27 require 'fileutils'
28   -require "uri"
  28 +require 'uri'
  29 +require 'cgi'
29 30
30   -require "pp"
  31 +require 'pp'
31 32
32 33 module ChkBuild; end # for testing
33 34
@@ -63,11 +64,15 @@ def git(cloneurl, working_dir, opts={})
63 64 GIT_SHARED_DIR = ChkBuild.build_top + 'git-repos'
64 65
65 66 def git_internal(cloneurl, working_dir, opts={})
66   - urigen = nil
  67 + viewer = nil
67 68 opts = opts.dup
68 69 opts[:section] ||= 'git'
69 70 if opts[:github]
70   - urigen = GitHub.new(*opts[:github])
  71 + viewer = ['GitHub', opts[:github]]
  72 + elsif opts[:gitweb]
  73 + viewer = ['GitWeb', opts[:gitweb]]
  74 + elsif opts[:cgit]
  75 + viewer = ['cgit', opts[:cgit]]
71 76 end
72 77 FileUtils.mkdir_p(GIT_SHARED_DIR)
73 78 opts_shared = opts.dup
@@ -125,14 +130,13 @@ def git_internal(cloneurl, working_dir, opts={})
125 130 puts "LASTLOG #{line}"
126 131 }
127 132 puts "CHECKOUT git #{cloneurl} #{working_dir}"
  133 + puts "VIEWER #{viewer[0]} #{viewer[1]}" if viewer
128 134 puts "LASTCOMMIT #{new_head}"
129 135 }
130 136 end
131 137
132 138 def github(user, project, working_dir, opts={})
133   - opts = opts.dup
134   - opts[:github] = [user, project]
135   - git("git://github.com/#{user}/#{project}.git", working_dir, opts)
  139 + git("git://github.com/#{u user}/#{u project}.git", working_dir, opts)
136 140 end
137 141
138 142 def git_single_log(rev)
@@ -211,17 +215,55 @@ def git_path_sort(ary)
211 215 end
212 216
213 217 class GitHub
214   - def initialize(user, project)
215   - @user = user
216   - @project = project
  218 + def initialize(uri)
  219 + # https://github.com/rubyspec/rubyspec
  220 + @user, @project = uri.split(%r{/})[-2, 2]
217 221 end
218 222
219 223 def call(commit_hash)
220   - # http://github.com/brixen/rubyspec/commit/b8f8eb6765afe915f2ecfdbbe59a53e6393d6865
221   - "http://github.com/#{@user}/#{@project}/commit/#{commit_hash}"
  224 + # https://github.com/rubyspec/rubyspec/commit/b8f8eb6765afe915f2ecfdbbe59a53e6393d6865
  225 + "https://github.com/#{@user}/#{@project}/commit/#{commit_hash}"
  226 + end
  227 + end
  228 +
  229 + class GitWeb
  230 + def initialize(uri)
  231 + # http://git.savannah.gnu.org/gitweb/?p=autoconf.git
  232 + @uri = URI(uri)
  233 + @git_project_name = CGI.parse(@uri.query)['p'][0]
  234 + end
  235 +
  236 + def call(hash)
  237 + # http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commit;h=cc2118d83698708c7c0334ad72f2cd03c4f81f0b
  238 + uri = @uri.dup
  239 + query = [
  240 + ['p', @git_project_name],
  241 + ['a', 'commit'],
  242 + ['h', hash]
  243 + ]
  244 + uri.query = query.map {|k, v| "#{k}=#{CGI.escape v}" }.join(';')
  245 + uri.to_s
  246 + end
  247 + end
  248 +
  249 + class Cgit
  250 + def initialize(uri)
  251 + # http://git.savannah.gnu.org/cgit/autoconf.git
  252 + @uri = uri
  253 + end
  254 +
  255 + def call(hash)
  256 + # http://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
  257 + "#{@uri}/commit/?id=#{hash}"
222 258 end
223 259 end
224 260
  261 + GIT_VIEWERS = {
  262 + 'GitHub' => GitHub,
  263 + 'GitWeb' => GitWeb,
  264 + 'cgit' => Cgit,
  265 + }
  266 +
225 267 def git_print_logs(logs, urigen, out)
226 268 logs.each {|commit_hash, title_line|
227 269 if urigen
@@ -241,16 +283,20 @@ def output_git_change_lines(checkout_line, lines1, lines2, out)
241 283 end
242 284 cloneurl = $1
243 285 working_dir = $2
244   - urigen = nil
245   - if %r{\Agit://github.com/([^/]+)/([^/]+).git\z} =~ cloneurl
246   - urigen = GitHub.new($1, $2)
247   - elsif %r{\Agit://git\.sv\.gnu\.org/([^/]+)\.git\z} =~ cloneurl
248   - # git://git.sv.gnu.org/autoconf.git
249   - # http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commit;h=cc2118d83698708c7c0334ad72f2cd03c4f81f0b
250   - git_project_basename = $1
251   - urigen = lambda {|hash|
252   - "http://git.savannah.gnu.org/gitweb/?p=#{git_project_basename}.git;a=commit;h=#{hash}"
253   - }
  286 + viewer = nil
  287 + viewer_line_pattern = /\AVIEWER\s+(#{Regexp.union GIT_VIEWERS.keys})\s+(\S+)/
  288 + lines2.each {|line|
  289 + if viewer_line_pattern =~ line
  290 + viewer = [$1, $2]
  291 + break
  292 + end
  293 + }
  294 + if !viewer
  295 + viewer = git_find_viewer(cloneurl)
  296 + end
  297 + if viewer
  298 + urigen_class = GIT_VIEWERS[viewer[0]]
  299 + urigen = urigen_class.new(viewer[1])
254 300 end
255 301
256 302 lastcommit1 = lines1.find {|line| /\ALASTCOMMIT / =~ line }
@@ -268,4 +314,30 @@ def output_git_change_lines(checkout_line, lines1, lines2, out)
268 314 git_print_logs(logs, urigen, out)
269 315 }
270 316 end
  317 +
  318 + # find a viewer for major source code hosting sites.
  319 + def git_find_viewer(cloneurl)
  320 + # segment = *pchar
  321 + # pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  322 + # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  323 + # pct-encoded = "%" HEXDIG HEXDIG
  324 + # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  325 + # / "*" / "+" / "," / ";" / "="
  326 + segment_regexp = '(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f][0-9A-Fa-f])*'
  327 + if %r{\Agit://github\.com/(#{segment_regexp})/(#{segment_regexp})\.git\z}o =~ cloneurl
  328 + user = $1
  329 + project = $1
  330 + return ['GitHub', "https://github.com/#{user}/#{project}"]
  331 + elsif %r{\Agit://git\.savannah\.gnu\.org/(#{segment_regexp})\.git\z}o =~ cloneurl
  332 + project_basename = $1
  333 + # git://git.savannah.gnu.org/autoconf.git
  334 + # http://git.savannah.gnu.org/cgit/autoconf.git
  335 + # http://git.savannah.gnu.org/gitweb/?p=autoconf.git
  336 + return ['cgit', "http://git.savannah.gnu.org/cgit/#{project_basename}.git"]
  337 + # project_basename = CGI.escape(CGI.unescape($1)) # segment to query component
  338 + # return ['GitWeb', "http://git.savannah.gnu.org/gitweb/?p=#{project_basename}.git"]
  339 + end
  340 + nil
  341 + end
  342 +
271 343 end

0 comments on commit accf429

Please sign in to comment.
Something went wrong with that request. Please try again.