Class: Heidi::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/heidi/git.rb

Overview

A very simple interface to git base on SimpleShell, no library ties and no fancy stuff

Constant Summary collapse

VERBOSE =
false

Instance Method Summary collapse

Constructor Details

#initialize(path = Dir.pwd, verbose = VERBOSE) ⇒ Git

Returns a new instance of Git.



13
14
15
16
17
# File 'lib/heidi/git.rb', line 13

def initialize(path=Dir.pwd, verbose=VERBOSE)
  @path = path
  SimpleShell.noisy = verbose
  @shell = SimpleShell.new(@path)
end

Instance Method Details

#[](key) ⇒ Object

git config $key



127
128
129
# File 'lib/heidi/git.rb', line 127

def [](key)
  @shell.system("git", "config", "heidi.#{key}").out
end

#[]=(key, value) ⇒ Object

git config $key $value



122
123
124
# File 'lib/heidi/git.rb', line 122

def []=(key, value)
  @shell.system("git", "config", "heidi.#{key}", value)
end

#branchObject

find the current branch (the one with the *)



28
29
30
31
32
# File 'lib/heidi/git.rb', line 28

def branch
  res = @shell.git "branch", "--no-color"
  active = res.out.scan(/\* \w+/).first
  active.scan(/\w+$/).first
end

#branchesObject

git branch



35
36
37
38
# File 'lib/heidi/git.rb', line 35

def branches
  res = @shell.git "branch", "--no-color"
  res.out.split("\n").collect{ |b| b.gsub(/^[\s*]+/, '') }
end

#checkout(name, base = nil) ⇒ Object

git checkout -b $name [$base]



47
48
49
50
51
# File 'lib/heidi/git.rb', line 47

def checkout(name, base=nil)
  command = [ "git", "checkout", "-b", name ]
  command << base unless base.nil?
  @shell.system(*command)
end

#commitObject Also known as: HEAD, head

get the latest commit hash



20
21
22
23
# File 'lib/heidi/git.rb', line 20

def commit
  res = @shell.git "log", "-n", "1", "--pretty=format:%H"
  res.out
end

#fetch(where = "origin") ⇒ Object

git fetch $where=‘origin’



59
60
61
# File 'lib/heidi/git.rb', line 59

def fetch(where="origin")
  @shell.git %W(fetch #{where})
end

#graph(amount = 40) ⇒ Object



117
118
119
# File 'lib/heidi/git.rb', line 117

def graph(amount=40)
  @shell.git %W(log -n#{amount} --color --graph --pretty=oneline --abbrev-commit)
end

#log(amount, format = nil, commit = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/heidi/git.rb', line 103

def log(amount, format=nil, commit=nil)
  args = %W(log -n#{amount})

  if !format.nil? && format !~ /\%/
    commit = format
    format = nil
  end

  args << "--pretty=#{format}" unless format.nil?
  args << commit unless commit.nil?

  @shell.git(args).out
end

#merge(base) ⇒ Object

git merge $base



54
55
56
# File 'lib/heidi/git.rb', line 54

def merge(base)
  @shell.git %W(merge #{base})
end

#pull(where = "origin", what = nil) ⇒ Object

git pull $where=‘origin’ [$what]



64
65
66
67
68
69
# File 'lib/heidi/git.rb', line 64

def pull(where="origin", what=nil)
  command = [ "git", "pull", where ]
  command << what if !what.nil?

  @shell.system(*command)
end

#push(where = "origin", what = nil) ⇒ Object

git push $where [$what]

$what may be '--tags'


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/heidi/git.rb', line 75

def push(where="origin", what=nil)
  command = [ "git", "push", where ]
  if !what.nil?
    if what == "--tags"
      command.insert(2, what)
    else
      command << what
    end
  end

  @shell.system(*command)
end

#switch(name) ⇒ Object

git checkout $name



41
42
43
44
# File 'lib/heidi/git.rb', line 41

def switch(name)
  return nil unless branches.include?(name)
  @shell.git "checkout", name
end

#tag(name, message) ⇒ Object

git tag -a -m $message $name



95
96
97
98
99
100
101
# File 'lib/heidi/git.rb', line 95

def tag(name, message)
  command = [ "git", "tag", "-a", "-m", message, name ]
  if tags.include?(name)
    command.insert(4, "-f")
  end
  @shell.system(*command)
end

#tagsObject

git tags



89
90
91
92
# File 'lib/heidi/git.rb', line 89

def tags
  res = @shell.system("git", "tag")
  res.out.split("\n")
end