github-file.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /** @babel */
  2. import {shell} from 'electron'
  3. import {Range} from 'atom'
  4. import {parse as parseURL} from 'url'
  5. import path from 'path'
  6. export default class GitHubFile {
  7. // Public
  8. static fromPath (filePath) {
  9. return new GitHubFile(filePath)
  10. }
  11. constructor (filePath) {
  12. this.filePath = filePath
  13. const [rootDir] = atom.project.relativizePath(this.filePath)
  14. if (rootDir != null) {
  15. const rootDirIndex = atom.project.getPaths().indexOf(rootDir)
  16. this.repo = atom.project.getRepositories()[rootDirIndex]
  17. this.type = 'none'
  18. if (this.repo && this.gitURL()) {
  19. if (this.isGitHubWikiURL(this.githubRepoURL())) {
  20. this.type = 'wiki'
  21. } else if (this.isGistURL(this.githubRepoURL())) {
  22. this.type = 'gist'
  23. } else {
  24. this.type = 'repo'
  25. }
  26. }
  27. }
  28. }
  29. // Public
  30. open (lineRange) {
  31. if (this.validateRepo()) {
  32. this.openURLInBrowser(this.blobURL() + this.getLineRangeSuffix(lineRange))
  33. }
  34. }
  35. // Public
  36. openOnMaster (lineRange) {
  37. if (this.validateRepo()) {
  38. this.openURLInBrowser(this.blobURLForMaster() + this.getLineRangeSuffix(lineRange))
  39. }
  40. }
  41. // Public
  42. blame (lineRange) {
  43. if (this.validateRepo()) {
  44. if (this.type === 'repo') {
  45. this.openURLInBrowser(this.blameURL() + this.getLineRangeSuffix(lineRange))
  46. } else {
  47. atom.notifications.addWarning(`Blames do not exist for ${this.type}s`)
  48. }
  49. }
  50. }
  51. history () {
  52. if (this.validateRepo()) {
  53. this.openURLInBrowser(this.historyURL())
  54. }
  55. }
  56. copyURL (lineRange) {
  57. if (this.validateRepo()) {
  58. atom.clipboard.write(this.shaURL() + this.getLineRangeSuffix(lineRange))
  59. }
  60. }
  61. openBranchCompare () {
  62. if (this.validateRepo()) {
  63. if (this.type === 'repo') {
  64. this.openURLInBrowser(this.branchCompareURL())
  65. } else {
  66. atom.notifications.addWarning(`Branches do not exist for ${this.type}s`)
  67. }
  68. }
  69. }
  70. openIssues () {
  71. if (this.validateRepo()) {
  72. if (this.type === 'repo') {
  73. this.openURLInBrowser(this.issuesURL())
  74. } else {
  75. atom.notifications.addWarning(`Issues do not exist for ${this.type}s`)
  76. }
  77. }
  78. }
  79. openPullRequests () {
  80. if (this.validateRepo()) {
  81. if (this.type === 'repo') {
  82. this.openURLInBrowser(this.pullRequestsURL())
  83. } else {
  84. atom.notifications.addWarning(`Pull requests do not exist for ${this.type}s`)
  85. }
  86. }
  87. }
  88. openRepository () {
  89. if (this.validateRepo()) {
  90. this.openURLInBrowser(this.githubRepoURL())
  91. }
  92. }
  93. getLineRangeSuffix (lineRange) {
  94. if (lineRange && this.type !== 'wiki' && atom.config.get('open-on-github.includeLineNumbersInUrls')) {
  95. lineRange = Range.fromObject(lineRange)
  96. const startRow = lineRange.start.row + 1
  97. const endRow = lineRange.end.row + 1
  98. if (startRow === endRow) {
  99. if (this.type === 'gist') {
  100. return `-L${startRow}`
  101. } else {
  102. return `#L${startRow}`
  103. }
  104. } else {
  105. if (this.type === 'gist') {
  106. return `-L${startRow}-L${endRow}`
  107. } else {
  108. return `#L${startRow}-L${endRow}`
  109. }
  110. }
  111. } else {
  112. return ''
  113. }
  114. }
  115. // Internal
  116. validateRepo () {
  117. if (!this.repo) {
  118. atom.notifications.addWarning(`No repository found for path: ${this.filePath}.`)
  119. return false
  120. } else if (!this.gitURL()) {
  121. atom.notifications.addWarning(`No URL defined for remote: ${this.remoteName()}`)
  122. return false
  123. } else if (!this.githubRepoURL()) {
  124. atom.notifications.addWarning(`Remote URL is not hosted on GitHub: ${this.gitURL()}`)
  125. return false
  126. }
  127. return true
  128. }
  129. // Internal
  130. openURLInBrowser (url) {
  131. shell.openExternal(url)
  132. }
  133. // Internal
  134. blobURL () {
  135. const gitHubRepoURL = this.githubRepoURL()
  136. const repoRelativePath = this.repoRelativePath()
  137. if (this.type === 'wiki') {
  138. return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}`
  139. } else if (this.type === 'gist') {
  140. return `${gitHubRepoURL}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
  141. } else {
  142. return `${gitHubRepoURL}/blob/${this.remoteBranchName()}/${this.encodeSegments(repoRelativePath)}`
  143. }
  144. }
  145. // Internal
  146. blobURLForMaster () {
  147. const gitHubRepoURL = this.githubRepoURL()
  148. if (this.type === 'repo') {
  149. return `${gitHubRepoURL}/blob/master/${this.encodeSegments(this.repoRelativePath())}`
  150. } else {
  151. return this.blobURL() // Only repos have branches
  152. }
  153. }
  154. // Internal
  155. shaURL () {
  156. const gitHubRepoURL = this.githubRepoURL()
  157. const encodedSHA = this.encodeSegments(this.sha())
  158. const repoRelativePath = this.repoRelativePath()
  159. if (this.type === 'wiki') {
  160. return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}/${encodedSHA}`
  161. } else if (this.type === 'gist') {
  162. return `${gitHubRepoURL}/${encodedSHA}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
  163. } else {
  164. return `${gitHubRepoURL}/blob/${encodedSHA}/${this.encodeSegments(repoRelativePath)}`
  165. }
  166. }
  167. // Internal
  168. blameURL () {
  169. return `${this.githubRepoURL()}/blame/${this.remoteBranchName()}/${this.encodeSegments(this.repoRelativePath())}`
  170. }
  171. // Internal
  172. historyURL () {
  173. const gitHubRepoURL = this.githubRepoURL()
  174. if (this.type === 'wiki') {
  175. return `${gitHubRepoURL}/${this.extractFileName(this.repoRelativePath())}/_history`
  176. } else if (this.type === 'gist') {
  177. return `${gitHubRepoURL}/revisions`
  178. } else {
  179. return `${gitHubRepoURL}/commits/${this.remoteBranchName()}/${this.encodeSegments(this.repoRelativePath())}`
  180. }
  181. }
  182. // Internal
  183. issuesURL () {
  184. return `${this.githubRepoURL()}/issues`
  185. }
  186. // Internal
  187. pullRequestsURL () {
  188. return `${this.githubRepoURL()}/pulls`
  189. }
  190. // Internal
  191. branchCompareURL () {
  192. return `${this.githubRepoURL()}/compare/${this.encodeSegments(this.branchName())}`
  193. }
  194. encodeSegments (segments = '') {
  195. return segments.split('/').map(segment => encodeURIComponent(segment)).join('/')
  196. }
  197. // Internal
  198. extractFileName (relativePath = '') {
  199. return path.parse(relativePath).name
  200. }
  201. // Internal
  202. gitURL () {
  203. const remoteName = this.remoteName()
  204. if (remoteName != null) {
  205. return this.repo.getConfigValue(`remote.${remoteName}.url`, this.filePath)
  206. } else {
  207. return this.repo.getConfigValue(`remote.origin.url`, this.filePath)
  208. }
  209. }
  210. // Internal
  211. githubRepoURL () {
  212. let url = this.gitURL()
  213. if (url.match(/git@[^:]+:/)) { // git@github.com:user/repo.git
  214. url = url.replace(/^git@([^:]+):(.+)$/, (match, host, repoPath) => {
  215. repoPath = repoPath.replace(/^\/+/, '')
  216. return `https://${host}/${repoPath}` // -> https://github.com/user/repo.git
  217. })
  218. } else if (url.match(/^ssh:\/\/git@([^/]+)\//)) { // ssh://git@github.com/user/repo.git
  219. url = `https://${url.substring(10)}` // -> https://github.com/user/repo.git
  220. } else if (url.match(/^git:\/\/[^/]+\//)) { // git://github.com/user/repo.git
  221. url = `https${url.substring(3)}` // -> https://github.com/user/repo.git
  222. } else if (url.match(/^https?:\/\/\w+@/)) { // https://user@github.com/user/repo.git
  223. url = url.replace(/^https?:\/\/\w+@/, 'https://') // -> https://github.com/user/repo.git
  224. }
  225. // Remove trailing .git and trailing slashes
  226. url = url.replace(/\.git$/, '').replace(/\/+$/, '')
  227. // Change .wiki to /wiki
  228. url = url.replace(/\.wiki$/, '/wiki')
  229. if (!this.isBitbucketURL(url)) {
  230. return url
  231. }
  232. }
  233. isGistURL (url) {
  234. try {
  235. const {host} = parseURL(url)
  236. return host === 'gist.github.com'
  237. } catch (error) {
  238. return false
  239. }
  240. }
  241. isGitHubWikiURL (url) {
  242. return /\/wiki$/.test(url)
  243. }
  244. isBitbucketURL (url) {
  245. if (url.startsWith('git@bitbucket.org')) {
  246. return true
  247. }
  248. try {
  249. const {host} = parseURL(url)
  250. return host === 'bitbucket.org'
  251. } catch (error) {
  252. return false
  253. }
  254. }
  255. // Internal
  256. repoRelativePath () {
  257. return this.repo.getRepo(this.filePath).relativize(this.filePath)
  258. }
  259. // Internal
  260. remoteName () {
  261. const gitConfigRemote = this.repo.getConfigValue('atom.open-on-github.remote', this.filePath)
  262. if (gitConfigRemote) {
  263. return gitConfigRemote
  264. }
  265. const shortBranch = this.repo.getShortHead(this.filePath)
  266. if (!shortBranch) {
  267. return null
  268. }
  269. const branchRemote = this.repo.getConfigValue(`branch.${shortBranch}.remote`, this.filePath)
  270. if (branchRemote && branchRemote.length > 0) {
  271. return branchRemote
  272. }
  273. return null
  274. }
  275. // Internal
  276. sha () {
  277. return this.repo.getReferenceTarget('HEAD', this.filePath)
  278. }
  279. // Internal
  280. branchName () {
  281. const shortBranch = this.repo.getShortHead(this.filePath)
  282. if (!shortBranch) {
  283. return null
  284. }
  285. const branchMerge = this.repo.getConfigValue(`branch.${shortBranch}.merge`, this.filePath)
  286. if (!(branchMerge && branchMerge.length > 11)) {
  287. return shortBranch
  288. }
  289. if (branchMerge.indexOf('refs/heads/') !== 0) {
  290. return shortBranch
  291. }
  292. return branchMerge.substring(11)
  293. }
  294. // Internal
  295. remoteBranchName () {
  296. const gitConfigBranch = this.repo.getConfigValue('atom.open-on-github.branch', this.filePath)
  297. if (gitConfigBranch) {
  298. return gitConfigBranch
  299. } else if (this.remoteName() != null) {
  300. return this.encodeSegments(this.branchName())
  301. } else {
  302. return 'master'
  303. }
  304. }
  305. }