This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgithub-file.coffee
213 lines (171 loc) · 5.46 KB
/
github-file.coffee
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Shell = require 'shell'
{Range} = require 'atom'
parseUrl = require('url').parse
module.exports =
class GitHubFile
# Public
@fromPath: (filePath) ->
new GitHubFile(filePath)
# Internal
constructor: (@filePath) ->
[rootDir] = atom.project.relativizePath(filePath)
if rootDir?
rootDirIndex = atom.project.getPaths().indexOf(rootDir)
@repo = atom.project.getRepositories()[rootDirIndex]
# Public
open: (lineRange) ->
if @isOpenable()
@openUrlInBrowser(@blobUrl() + @getLineRangeSuffix(lineRange))
else
@reportValidationErrors()
# Public
openOnMaster: (lineRange) ->
if @isOpenable()
@openUrlInBrowser(@blobUrlForMaster() + @getLineRangeSuffix(lineRange))
else
@reportValidationErrors()
# Public
blame: (lineRange) ->
if @isOpenable()
@openUrlInBrowser(@blameUrl() + @getLineRangeSuffix(lineRange))
else
@reportValidationErrors()
history: ->
if @isOpenable()
@openUrlInBrowser(@historyUrl())
else
@reportValidationErrors()
copyUrl: (lineRange) ->
if @isOpenable()
atom.clipboard.write(@shaUrl() + @getLineRangeSuffix(lineRange))
else
@reportValidationErrors()
openBranchCompare: ->
if @isOpenable()
@openUrlInBrowser(@branchCompareUrl())
else
@reportValidationErrors()
openIssues: ->
if @isOpenable()
@openUrlInBrowser(@issuesUrl())
else
@reportValidationErrors()
openRepository: ->
if @isOpenable()
@openUrlInBrowser(@githubRepoUrl())
else
@reportValidationErrors()
getLineRangeSuffix: (lineRange) ->
if lineRange and atom.config.get('open-on-github.includeLineNumbersInUrls')
lineRange = Range.fromObject(lineRange)
startRow = lineRange.start.row + 1
endRow = lineRange.end.row + 1
if startRow is endRow
"#L#{startRow}"
else
"#L#{startRow}-L#{endRow}"
else
''
# Public
isOpenable: ->
@validationErrors().length is 0
# Public
validationErrors: ->
unless @repo
if @filePath
return ["No repository found for path: #{@filePath}"]
else
return ["No repository found for path"]
unless @gitUrl()
if @remoteName()
return ["No URL defined for remote filepath: #{@remoteName()}"]
else
return ["No URL defined for remote filepath"]
unless @githubRepoUrl()
if @gitUrl()
return ["Remote URL is not hosted on GitHub: #{@gitUrl()}"]
else
return ["Remote URL is not hosted on GitHub"]
[]
# Internal
reportValidationErrors: ->
message = @validationErrors().join('\n')
atom.notifications.addWarning(message)
# Internal
openUrlInBrowser: (url) ->
Shell.openExternal url
# Internal
blobUrl: ->
"#{@githubRepoUrl()}/blob/#{@remoteBranchName()}/#{@encodeSegments(@repoRelativePath())}"
# Internal
blobUrlForMaster: ->
"#{@githubRepoUrl()}/blob/master/#{@encodeSegments(@repoRelativePath())}"
# Internal
shaUrl: ->
"#{@githubRepoUrl()}/blob/#{@encodeSegments(@sha())}/#{@encodeSegments(@repoRelativePath())}"
# Internal
blameUrl: ->
"#{@githubRepoUrl()}/blame/#{@remoteBranchName()}/#{@encodeSegments(@repoRelativePath())}"
# Internal
historyUrl: ->
"#{@githubRepoUrl()}/commits/#{@remoteBranchName()}/#{@encodeSegments(@repoRelativePath())}"
# Internal
issuesUrl: ->
"#{@githubRepoUrl()}/issues"
# Internal
branchCompareUrl: ->
"#{@githubRepoUrl()}/compare/#{@encodeSegments(@branchName())}"
encodeSegments: (segments='') ->
segments = segments.split('/')
segments = segments.map (segment) -> encodeURIComponent(segment)
segments.join('/')
# Internal
gitUrl: ->
remoteOrBestGuess = @remoteName() ? 'origin'
@repo.getConfigValue("remote.#{remoteOrBestGuess}.url", @filePath)
# Internal
githubRepoUrl: ->
url = @gitUrl()
if url.match /git@[^:]+:/ # e.g., [email protected]:foo/bar.git
url = url.replace /^git@([^:]+):(.+)$/, (match, host, repoPath) ->
repoPath = repoPath.replace(/^\/+/, '') # replace leading slashes
"http://#{host}/#{repoPath}"
else if url.match /ssh:\/\/git@([^\/]+)\// # e.g., ssh://[email protected]/foo/bar.git
url = "http://#{url.substring(10)}"
else if url.match /^git:\/\/[^\/]+\// # e.g., git://github.com/foo/bar.git
url = "http#{url.substring(3)}"
url = url.replace(/\.git$/, '')
url = url.replace(/\/+$/, '')
return url unless @isBitbucketUrl(url)
isBitbucketUrl: (url) ->
return true if url.indexOf('[email protected]') is 0
try
{host} = parseUrl(url)
host is 'bitbucket.org'
# Internal
repoRelativePath: ->
@repo.getRepo(@filePath).relativize(@filePath)
# Internal
remoteName: ->
shortBranch = @repo.getShortHead(@filePath)
return null unless shortBranch
branchRemote = @repo.getConfigValue("branch.#{shortBranch}.remote", @filePath)
return null unless branchRemote?.length > 0
branchRemote
# Internal
sha: ->
@repo.getReferenceTarget('HEAD', @filePath)
# Internal
branchName: ->
shortBranch = @repo.getShortHead(@filePath)
return null unless shortBranch
branchMerge = @repo.getConfigValue("branch.#{shortBranch}.merge", @filePath)
return shortBranch unless branchMerge?.length > 11
return shortBranch unless branchMerge.indexOf('refs/heads/') is 0
branchMerge.substring(11)
# Internal
remoteBranchName: ->
if @remoteName()?
@encodeSegments(@branchName())
else
'master'