git-browse-remote 0.0.1 → 0.0.2

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 +35 -8
  2. metadata +3 -2
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  # USAGE:
4
- # git browse-remote [-r|--remote <remote>}] [--top|--rev|--ref] [<commit>]
4
+ # git browse-remote [-r|--remote <remote>] [--top|--rev|--ref] [-L <n>] [<commit>] [<file>]
5
5
  #
6
6
 
7
7
  require 'optparse'
@@ -14,6 +14,8 @@ end
14
14
 
15
15
  remote = nil
16
16
  mode = nil
17
+ file = nil
18
+ line = nil
17
19
 
18
20
  # Variables available:
19
21
  # * host (eg. "github.com")
@@ -21,23 +23,32 @@ mode = nil
21
23
  # * ref (eg. "refs/heads/master")
22
24
  # * short_ref (eg. "master")
23
25
  # * commit (eg. "04f7c64ba9d524cf311a673ddce5722b2441e2ea")
26
+ # * short_commit (eg. "04f7c64b")
27
+ # * rev (ref or commit)
28
+ # * short_rev (short_ref or short_commit)
29
+ # * file (eg. "bin/git-browse-remote")
30
+ # * line (eg. 30)
24
31
  MAPPING_RECIPES = {
25
32
  :github => {
26
- :top => 'https://{host}/{path}',
27
- :ref => 'https://{host}/{path}/tree/{short_ref}',
28
- :rev => 'https://{host}/{path}/commit/{commit}',
33
+ :top => 'https://{host}/{path}',
34
+ :ref => 'https://{host}/{path}/tree/{short_ref}',
35
+ :rev => 'https://{host}/{path}/commit/{commit}',
36
+ :file => 'https://{host}/{path}/blob/{short_rev}/{file}{line && "#L%d" % line}'
29
37
  },
30
38
 
31
39
  :gitweb => {
32
40
  :top => 'http://{host}/?p={path[-2..-1]}.git',
33
41
  :ref => 'http://{host}/?p={path[-2..-1]}.git;h={ref}',
34
42
  :rev => 'http://{host}/?p={path[-2..-1]}.git;a=commit;h={ref}',
43
+ # XXX
44
+ # I don't know file url of gitweb...
35
45
  }
36
46
  }
37
47
 
38
48
  OptionParser.new do |opt|
39
49
  opt.banner = 'git browse-remote [options] [<commit> | <remote>]'
40
50
  opt.on('-r', '--remote=<remote>', 'specify remote') { |r| remote = r }
51
+
41
52
  opt.on('--top', 'open `top` page') { mode = :top }
42
53
  opt.on('--rev', 'open `rev` page') { mode = :rev }
43
54
  opt.on('--ref', 'open `ref` page') { mode = :ref }
@@ -58,10 +69,19 @@ OptionParser.new do |opt|
58
69
  STDERR.puts 'Mappings generated:'
59
70
  exec "git config --get-regexp ^browse-remote\\.#{host}\\."
60
71
  end
72
+ opt.on('-L <n>', 'specify line number (only meaningful on file mode)', Integer) { |n| line = n }
61
73
  opt.parse!(ARGV)
62
74
  end
63
75
 
64
- target = ARGV.shift || `git symbolic-ref -q HEAD`[/.+/]
76
+ target, file = *ARGV[0..1]
77
+
78
+ # handle git browse-remote <file>
79
+ # TODO file exists and also is a valid ref/origin
80
+ if target && !file && File.exists?(target)
81
+ target, file = nil, target
82
+ end
83
+
84
+ target ||= `git symbolic-ref -q HEAD`[/.+/]
65
85
 
66
86
  if target
67
87
  if `git rev-parse --verify --quiet #{target}` && $? == 0
@@ -79,8 +99,6 @@ else
79
99
  ref = `git name-rev --name-only HEAD`.chomp.sub(%r(\^0$), '') # some workaround for ^0
80
100
  end
81
101
 
82
- commit = `git rev-parse #{target || 'HEAD'}`.chomp
83
-
84
102
  if ref == 'HEAD'
85
103
  ref = nil
86
104
  short_ref = nil
@@ -102,6 +120,15 @@ else
102
120
 
103
121
  end
104
122
 
123
+ commit = `git rev-parse #{target || 'HEAD'}`.chomp
124
+ short_commit = `git rev-parse --short #{target || 'HEAD'}`.chomp
125
+
126
+ # `rev` will be raw sha1 if --rev is specified explicitly
127
+ rev = mode == :rev ? commit : ref || commit
128
+ short_rev = mode == :rev ? short_commit : short_ref || short_commit
129
+
130
+ mode = :file if file
131
+
105
132
  remote ||= 'origin'
106
133
  mode ||= :rev
107
134
 
@@ -115,5 +142,5 @@ path = Path.new(path)
115
142
  template = `git config --get browse-remote.#{host}.#{mode}`[/.+/] or
116
143
  abort "No '#{mode}' mapping found for #{host} (maybe `git browse-remote --init` required)"
117
144
 
118
- url = template.gsub(/{(.+?)}/) { |m| eval($1) }
145
+ url = template.gsub(/\{(.+?)\}/) { |m| eval($1) }
119
146
  exec 'git', 'web--browse', url
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-browse-remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-16 00:00:00.000000000 Z
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: motemen@gmail.com
@@ -44,3 +44,4 @@ signing_key:
44
44
  specification_version: 3
45
45
  summary: Open web browser to view remote Git repositories
46
46
  test_files: []
47
+ has_rdoc: