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



141
142
143
# File 'lib/heidi/git.rb', line 141

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

#[]=(key, value) ⇒ Object

git config $key $value



136
137
138
# File 'lib/heidi/git.rb', line 136

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]



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

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

#diff(commit) ⇒ Object



131
132
133
# File 'lib/heidi/git.rb', line 131

def diff(commit)
  @shell.git(%W(diff --color #{commit}^ #{commit})).out
end

#fetch(where = "origin") ⇒ Object

git fetch $where=‘origin’



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

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

#graph(amount = 40) ⇒ Object



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

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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/heidi/git.rb', line 109

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



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

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

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

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



70
71
72
73
74
75
# File 'lib/heidi/git.rb', line 70

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'


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/heidi/git.rb', line 81

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

#remote_branchesObject



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

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

#stat(commit) ⇒ Object



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

def stat(commit)
  @shell.git(%W(log -n1 --color --stat #{commit})).out
end

#switch(name) ⇒ Object

git checkout $name



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

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

#tag(name, message) ⇒ Object

git tag -a -m $message $name



101
102
103
104
105
106
107
# File 'lib/heidi/git.rb', line 101

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



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

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