unixorn/bitbucket-git-helpers.plugin.zsh

View on GitHub
bin/git-bb-open

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
#
# Open a file/directory on bitbucket in the current branch
#
# Copyright 2016 Joe Block <jpb@unixorn.net>
#
# License: Apache 2.0, see LICENSE in this repository.

require 'pathname'
require_relative 'bbhelpers'

def constructURL
  remote = git_remote()
  bbloc = remote.index('bitbucket.org')
  if not bbloc
    puts "git-bb-open only works for repositories with their origin remote on bitbucket.org"
    exit 1
  end
  url_prefix="https://#{remote[bbloc..-5].sub(':','/')}/src/#{git_commit}"
  root = Pathname.new(git_root())
  pwd = Pathname.new(Dir.pwd())
  if ARGV.length == 1
    path = "/#{pwd.relative_path_from(root).to_s()}/#{ARGV[0]}"
  else
    if Dir.pwd() == git_root()
      path = ''
    else
      path = "/#{pwd.relative_path_from(root).to_s()}"
    end
  end
  url = url_prefix + path + "?at=#{git_branch}"
end

if ARGV.length > 1
  puts 'Usage: git-bb-open without args to open the current directory on bitbucket, viewing the current branch.'
  puts '       git-bb-open <path> to open <path> on bitbucket, viewing the current branch'
end

openURL(constructURL())