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
Expand file tree
/
Copy pathgithub-file.js
More file actions
363 lines (305 loc) · 9.39 KB
/
github-file.js
File metadata and controls
363 lines (305 loc) · 9.39 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/** @babel */
import {shell} from 'electron'
import {Range} from 'atom'
import {parse as parseURL} from 'url'
import path from 'path'
export default class GitHubFile {
// Public
static fromPath (filePath) {
return new GitHubFile(filePath)
}
constructor (filePath) {
this.filePath = filePath
const [rootDir] = atom.project.relativizePath(this.filePath)
if (rootDir != null) {
const rootDirIndex = atom.project.getPaths().indexOf(rootDir)
this.repo = atom.project.getRepositories()[rootDirIndex]
this.type = 'none'
if (this.repo && this.gitURL()) {
if (this.isGitHubWikiURL(this.githubRepoURL())) {
this.type = 'wiki'
} else if (this.isGistURL(this.githubRepoURL())) {
this.type = 'gist'
} else {
this.type = 'repo'
}
}
}
}
// Public
open (lineRange) {
if (this.validateRepo()) {
this.openURLInBrowser(this.blobURL() + this.getLineRangeSuffix(lineRange))
}
}
// Public
openOnMaster (lineRange) {
if (this.validateRepo()) {
this.openURLInBrowser(this.blobURLForMaster() + this.getLineRangeSuffix(lineRange))
}
}
// Public
blame (lineRange) {
if (this.validateRepo()) {
if (this.type === 'repo') {
this.openURLInBrowser(this.blameURL() + this.getLineRangeSuffix(lineRange))
} else {
atom.notifications.addWarning(`Blames do not exist for ${this.type}s`)
}
}
}
history () {
if (this.validateRepo()) {
this.openURLInBrowser(this.historyURL())
}
}
copyURL (lineRange) {
if (this.validateRepo()) {
atom.clipboard.write(this.shaURL() + this.getLineRangeSuffix(lineRange))
}
}
openBranchCompare () {
if (this.validateRepo()) {
if (this.type === 'repo') {
this.openURLInBrowser(this.branchCompareURL())
} else {
atom.notifications.addWarning(`Branches do not exist for ${this.type}s`)
}
}
}
openIssues () {
if (this.validateRepo()) {
if (this.type === 'repo') {
this.openURLInBrowser(this.issuesURL())
} else {
atom.notifications.addWarning(`Issues do not exist for ${this.type}s`)
}
}
}
openPullRequests () {
if (this.validateRepo()) {
if (this.type === 'repo') {
this.openURLInBrowser(this.pullRequestsURL())
} else {
atom.notifications.addWarning(`Pull requests do not exist for ${this.type}s`)
}
}
}
openRepository () {
if (this.validateRepo()) {
this.openURLInBrowser(this.githubRepoURL())
}
}
getLineRangeSuffix (lineRange) {
if (lineRange && this.type !== 'wiki' && atom.config.get('open-on-github.includeLineNumbersInUrls')) {
lineRange = Range.fromObject(lineRange)
const startRow = lineRange.start.row + 1
const endRow = lineRange.end.row + 1
if (startRow === endRow) {
if (this.type === 'gist') {
return `-L${startRow}`
} else {
return `#L${startRow}`
}
} else {
if (this.type === 'gist') {
return `-L${startRow}-L${endRow}`
} else {
return `#L${startRow}-L${endRow}`
}
}
} else {
return ''
}
}
// Internal
validateRepo () {
if (!this.repo) {
atom.notifications.addWarning(`No repository found for path: ${this.filePath}.`)
return false
} else if (!this.gitURL()) {
atom.notifications.addWarning(`No URL defined for remote: ${this.remoteName()}`)
return false
} else if (!this.githubRepoURL()) {
atom.notifications.addWarning(`Remote URL is not hosted on GitHub: ${this.gitURL()}`)
return false
}
return true
}
// Internal
openURLInBrowser (url) {
shell.openExternal(url)
}
// Internal
blobURL () {
const gitHubRepoURL = this.githubRepoURL()
const repoRelativePath = this.repoRelativePath()
if (this.type === 'wiki') {
return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}`
} else if (this.type === 'gist') {
return `${gitHubRepoURL}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
} else {
return `${gitHubRepoURL}/blob/${this.remoteBranchName()}/${this.encodeSegments(repoRelativePath)}`
}
}
// Internal
blobURLForMaster () {
const gitHubRepoURL = this.githubRepoURL()
if (this.type === 'repo') {
return `${gitHubRepoURL}/blob/master/${this.encodeSegments(this.repoRelativePath())}`
} else {
return this.blobURL() // Only repos have branches
}
}
// Internal
shaURL () {
const gitHubRepoURL = this.githubRepoURL()
const encodedSHA = this.encodeSegments(this.sha())
const repoRelativePath = this.repoRelativePath()
if (this.type === 'wiki') {
return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}/${encodedSHA}`
} else if (this.type === 'gist') {
return `${gitHubRepoURL}/${encodedSHA}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
} else {
return `${gitHubRepoURL}/blob/${encodedSHA}/${this.encodeSegments(repoRelativePath)}`
}
}
// Internal
blameURL () {
return `${this.githubRepoURL()}/blame/${this.remoteBranchName()}/${this.encodeSegments(this.repoRelativePath())}`
}
// Internal
historyURL () {
const gitHubRepoURL = this.githubRepoURL()
if (this.type === 'wiki') {
return `${gitHubRepoURL}/${this.extractFileName(this.repoRelativePath())}/_history`
} else if (this.type === 'gist') {
return `${gitHubRepoURL}/revisions`
} else {
return `${gitHubRepoURL}/commits/${this.remoteBranchName()}/${this.encodeSegments(this.repoRelativePath())}`
}
}
// Internal
issuesURL () {
return `${this.githubRepoURL()}/issues`
}
// Internal
pullRequestsURL () {
return `${this.githubRepoURL()}/pulls`
}
// Internal
branchCompareURL () {
return `${this.githubRepoURL()}/compare/${this.encodeSegments(this.branchName())}`
}
encodeSegments (segments = '') {
return segments.split('/').map(segment => encodeURIComponent(segment)).join('/')
}
// Internal
extractFileName (relativePath = '') {
return path.parse(relativePath).name
}
// Internal
gitURL () {
const remoteName = this.remoteName()
if (remoteName != null) {
return this.repo.getConfigValue(`remote.${remoteName}.url`, this.filePath)
} else {
return this.repo.getConfigValue(`remote.origin.url`, this.filePath)
}
}
// Internal
githubRepoURL () {
let url = this.gitURL()
if (url.match(/git@[^:]+:/)) { // git@github.com:user/repo.git
url = url.replace(/^git@([^:]+):(.+)$/, (match, host, repoPath) => {
repoPath = repoPath.replace(/^\/+/, '')
return `https://${host}/${repoPath}` // -> https://github.com/user/repo.git
})
} else if (url.match(/^ssh:\/\/git@([^/]+)\//)) { // ssh://git@github.com/user/repo.git
url = `https://${url.substring(10)}` // -> https://github.com/user/repo.git
} else if (url.match(/^git:\/\/[^/]+\//)) { // git://github.com/user/repo.git
url = `https${url.substring(3)}` // -> https://github.com/user/repo.git
} else if (url.match(/^https?:\/\/\w+@/)) { // https://user@github.com/user/repo.git
url = url.replace(/^https?:\/\/\w+@/, 'https://') // -> https://github.com/user/repo.git
}
// Remove trailing .git and trailing slashes
url = url.replace(/\.git$/, '').replace(/\/+$/, '')
// Change .wiki to /wiki
url = url.replace(/\.wiki$/, '/wiki')
if (!this.isBitbucketURL(url)) {
return url
}
}
isGistURL (url) {
try {
const {host} = parseURL(url)
return host === 'gist.github.com'
} catch (error) {
return false
}
}
isGitHubWikiURL (url) {
return /\/wiki$/.test(url)
}
isBitbucketURL (url) {
if (url.startsWith('git@bitbucket.org')) {
return true
}
try {
const {host} = parseURL(url)
return host === 'bitbucket.org'
} catch (error) {
return false
}
}
// Internal
repoRelativePath () {
return this.repo.getRepo(this.filePath).relativize(this.filePath)
}
// Internal
remoteName () {
const gitConfigRemote = this.repo.getConfigValue('atom.open-on-github.remote', this.filePath)
if (gitConfigRemote) {
return gitConfigRemote
}
const shortBranch = this.repo.getShortHead(this.filePath)
if (!shortBranch) {
return null
}
const branchRemote = this.repo.getConfigValue(`branch.${shortBranch}.remote`, this.filePath)
if (branchRemote && branchRemote.length > 0) {
return branchRemote
}
return null
}
// Internal
sha () {
return this.repo.getReferenceTarget('HEAD', this.filePath)
}
// Internal
branchName () {
const shortBranch = this.repo.getShortHead(this.filePath)
if (!shortBranch) {
return null
}
const branchMerge = this.repo.getConfigValue(`branch.${shortBranch}.merge`, this.filePath)
if (!(branchMerge && branchMerge.length > 11)) {
return shortBranch
}
if (branchMerge.indexOf('refs/heads/') !== 0) {
return shortBranch
}
return branchMerge.substring(11)
}
// Internal
remoteBranchName () {
const gitConfigBranch = this.repo.getConfigValue('atom.open-on-github.branch', this.filePath)
if (gitConfigBranch) {
return gitConfigBranch
} else if (this.remoteName() != null) {
return this.encodeSegments(this.branchName())
} else {
return 'master'
}
}
}