Module: Atlassian::Stash::Git

Defined in:
lib/atlassian/stash/git.rb

Constant Summary collapse

DEFAULT_REMOTE =
"origin"

Instance Method Summary collapse

Instance Method Details

#create_git_aliasObject


56
57
58
# File 'lib/atlassian/stash/git.rb', line 56

def create_git_alias
  %x(git config --global alias.create-pull-request "\!sh -c 'stash pull-request \\$0 \\[email protected]'")
end

#ensure_within_git!Object


48
49
50
51
52
53
54
# File 'lib/atlassian/stash/git.rb', line 48

def ensure_within_git!
  if is_in_git_repository?
    yield
  else
    raise "fatal: Not a git repository"
  end
end

#get_branchesObject


13
14
15
16
# File 'lib/atlassian/stash/git.rb', line 13

def get_branches()
  all = %x{git branch --no-color -a}.gsub("*","").gsub(" ", "").split("\n")
  all.select{|x| not x.include? "->"}
end

#get_current_branchObject


9
10
11
# File 'lib/atlassian/stash/git.rb', line 9

def get_current_branch
  %x(git symbolic-ref HEAD)[/refs\/heads\/(.*)/, 1]
end

#get_remote(branch = nil) ⇒ Object


31
32
33
34
35
# File 'lib/atlassian/stash/git.rb', line 31

def get_remote(branch = nil)
  remote_branch = %x(git rev-parse --abbrev-ref --symbolic-full-name #{branch}@{u} 2>/dev/null)
  remote = remote_branch.split('/').first
  remote == "" ? nil : remote
end

#get_remote_url(remote = nil) ⇒ Object


37
38
39
40
41
42
43
44
45
46
# File 'lib/atlassian/stash/git.rb', line 37

def get_remote_url(remote=nil)
  remotes = get_remotes
  return nil if remotes.empty?

  remote = DEFAULT_REMOTE if remote.nil? || remote.empty?

  origin = remotes.split("\n").collect { |r| r.strip }.grep(/^#{remote}.*\(push\)$/).first
  return nil if origin.nil?
  URI.extract(origin).first
end

#get_remotesObject


27
28
29
# File 'lib/atlassian/stash/git.rb', line 27

def get_remotes
  %x(git remote -v)
end

#get_repo_root_directoryObject


60
61
62
# File 'lib/atlassian/stash/git.rb', line 60

def get_repo_root_directory
  %x(git rev-parse --show-toplevel)
end

#is_branch?(match) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
21
# File 'lib/atlassian/stash/git.rb', line 18

def is_branch?(match)
  all = get_branches
  not all.select{|x| x == match}.empty?
end

#is_in_git_repository?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/atlassian/stash/git.rb', line 23

def is_in_git_repository?
  system('git rev-parse')
end