 
 module.exports =
 class GitHubFile
+
+  # Public
   @fromSession: (editSession) ->
     new GitHubFile(editSession)
 
+  # Internal
   constructor: (@editSession) ->
 
+  # Public
   open: ->
     unless @isOpenable()
       console.warn error for error in @validationErrors()


    @openUrlInBrowser(@blobUrl())

  openUrlInBrowser: (url) ->
    child_process.exec "open #{url}", (error, stdout, stderr) ->
      throw error if error?

  blobUrl: ->
    "#{@githubRepoUrl()}/blob/#{@branch()}/#{@filePath()}"

  gitUrl: ->
    git.getRepo().getConfigValue("remote.#{@remoteName()}.url")

  githubRepoUrl: ->
    url = @gitUrl()
    if url.match /https:\/\/github.com\// # e.g., https://github.com/foo/bar.git
      url.replace(/\.git$/, '')
    else if url.match /git@github.com/    # e.g., git@github.com:foo/bar.git
      url.
        replace(/^git@github.com:/, 'https://github.com/').
        replace(/\.git$/, '')

  filePath: ->
    git.relativize(@editSession.getBuffer().getPath())

  remoteName: ->
    refName = git.getRepo().getUpstreamBranch() # e.g., "refs/remotes/origin/master"
    return null unless refName?

    refName.match(/^refs\/remotes\/(.*)\/.*$/)[1]

  branch: ->
    git.getShortHead()

  isOpenable: ->
    @validationErrors().length == 0

  validationErrors: ->
    unless @remoteName()
      return ["No remote tracking branch exists for current branch (#{@branch()})"]

      return ["Remote URL is not hosted on GitHub.com (#{@gitUrl()})"]

    []

  # Internal
  openUrlInBrowser: (url) ->
    child_process.exec "open #{url}", (error, stdout, stderr) ->
      throw error if error?

  # Internal
  blobUrl: ->
    "#{@githubRepoUrl()}/blob/#{@branch()}/#{@filePath()}"

  # Internal
  gitUrl: ->
    git.getRepo().getConfigValue("remote.#{@remoteName()}.url")

  # Internal
  githubRepoUrl: ->
    url = @gitUrl()
    if url.match /https:\/\/github.com\// # e.g., https://github.com/foo/bar.git
      url.replace(/\.git$/, '')
    else if url.match /git@github.com/    # e.g., git@github.com:foo/bar.git
      url.
        replace(/^git@github.com:/, 'https://github.com/').
        replace(/\.git$/, '')

  # Internal
  filePath: ->
    git.relativize(@editSession.getBuffer().getPath())

  # Internal
  remoteName: ->
    refName = git.getRepo().getUpstreamBranch() # e.g., "refs/remotes/origin/master"
    return null unless refName?

    refName.match(/^refs\/remotes\/(.*)\/.*$/)[1]

  # Internal
  branch: ->
    git.getShortHead()
