git-browse-remote 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/git-browse-remote +119 -0
  2. metadata +46 -0
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # USAGE:
4
+ # git browse-remote [-r|--remote <remote>}] [--top|--rev|--ref] [<commit>]
5
+ #
6
+
7
+ require 'optparse'
8
+
9
+ class Path < Array
10
+ def to_s
11
+ join('/')
12
+ end
13
+ end
14
+
15
+ remote = nil
16
+ mode = nil
17
+
18
+ # Variables available:
19
+ # * host (eg. "github.com")
20
+ # * path - Sliceable like an Array (eg. "motemen/git-browse-remote")
21
+ # * ref (eg. "refs/heads/master")
22
+ # * short_ref (eg. "master")
23
+ # * commit (eg. "04f7c64ba9d524cf311a673ddce5722b2441e2ea")
24
+ MAPPING_RECIPES = {
25
+ :github => {
26
+ :top => 'https://{host}/{path}',
27
+ :ref => 'https://{host}/{path}/tree/{short_ref}',
28
+ :rev => 'https://{host}/{path}/commit/{commit}',
29
+ },
30
+
31
+ :gitweb => {
32
+ :top => 'http://{host}/?p={path[-2..-1]}.git',
33
+ :ref => 'http://{host}/?p={path[-2..-1]}.git;h={ref}',
34
+ :rev => 'http://{host}/?p={path[-2..-1]}.git;a=commit;h={ref}',
35
+ }
36
+ }
37
+
38
+ OptionParser.new do |opt|
39
+ opt.banner = 'git browse-remote [options] [<commit> | <remote>]'
40
+ opt.on('-r', '--remote=<remote>', 'specify remote') { |r| remote = r }
41
+ opt.on('--top', 'open `top` page') { mode = :top }
42
+ opt.on('--rev', 'open `rev` page') { mode = :rev }
43
+ opt.on('--ref', 'open `ref` page') { mode = :ref }
44
+ opt.on('--init [<host>=<recipe>]', 'initialize default url mappings') do |config|
45
+ if config
46
+ host, name = *config.split(/=/, 2)
47
+ else
48
+ host, name = 'github.com', 'github'
49
+ end
50
+
51
+ STDERR.puts "Writing config for #{host}..."
52
+
53
+ mapping = MAPPING_RECIPES[name.to_sym] or abort "Recipe '#{name}' not found"
54
+ mapping.each do |mode,template|
55
+ system %Q(git config --global browse-remote.#{host}.#{mode} '#{template}')
56
+ end
57
+
58
+ STDERR.puts 'Mappings generated:'
59
+ exec "git config --get-regexp ^browse-remote\\.#{host}\\."
60
+ end
61
+ opt.parse!(ARGV)
62
+ end
63
+
64
+ target = ARGV.shift || `git symbolic-ref -q HEAD`[/.+/]
65
+
66
+ if target
67
+ if `git rev-parse --verify --quiet #{target}` && $? == 0
68
+ # valid ref
69
+ ref = `git rev-parse --symbolic-full-name #{target}`.chomp
70
+ elsif `git config --get remote.#{target}.url`.chomp.empty? == false
71
+ # not a valid ref, but valid remote
72
+ ref = 'master'
73
+ remote = target
74
+ target = nil
75
+ else
76
+ abort "Not a valid ref or remote: #{target}"
77
+ end
78
+ else
79
+ ref = `git name-rev --name-only HEAD`.chomp.sub(%r(\^0$), '') # some workaround for ^0
80
+ end
81
+
82
+ commit = `git rev-parse #{target || 'HEAD'}`.chomp
83
+
84
+ if ref == 'HEAD'
85
+ ref = nil
86
+ short_ref = nil
87
+ else
88
+ short_ref = ref.sub(%r(^refs/), '')
89
+
90
+ if short_ref.sub!(%r(^heads/), '')
91
+ mode ||= short_ref == 'master' ? :top : :ref
92
+ end
93
+
94
+ if short_ref.sub!(%r(^tags/), '')
95
+ mode ||= :ref
96
+ end
97
+
98
+ if short_ref.sub!(%r(^remotes/([^/]+)/), '')
99
+ remote ||= $1
100
+ mode ||= short_ref == 'master' ? :top : :ref
101
+ end
102
+
103
+ end
104
+
105
+ remote ||= 'origin'
106
+ mode ||= :rev
107
+
108
+ remote_url = `git config remote.#{remote}.url`[/.+/] or
109
+ abort "Could not get remote url: #{remote}"
110
+
111
+ host, *path = remote_url.sub(%r(^\w+://), '').sub(/^[\w-]+@/, '').split(/[\/:]+/)
112
+ path.last.sub!(/\.git$/, '')
113
+ path = Path.new(path)
114
+
115
+ template = `git config --get browse-remote.#{host}.#{mode}`[/.+/] or
116
+ abort "No '#{mode}' mapping found for #{host} (maybe `git browse-remote --init` required)"
117
+
118
+ url = template.gsub(/{(.+?)}/) { |m| eval($1) }
119
+ exec 'git', 'web--browse', url
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-browse-remote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - motemen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: motemen@gmail.com
16
+ executables:
17
+ - git-browse-remote
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/git-browse-remote
22
+ homepage: https://github.com/motemen/git-browse-remote
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.21
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Open web browser to view remote Git repositories
46
+ test_files: []