Class: Daddy::Git

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

Instance Method Summary collapse

Instance Method Details

#branches(remote = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/daddy/git.rb', line 10

def branches(remote = false)
  branches = []
  `git branch -a`.split("\n").each do |b|
    next if b.index('HEAD')
    next if b.index('gh-pages')
    next unless b.index('remotes/origin/')
    
    if remote
      branches << b.strip
    else
      branches << b.strip.sub('remotes/origin/', '')
    end
  end
  
  branches.sort! do |a, b|
    if a == 'master' or a == 'remotes/origin/master'
      -1
    elsif b == 'master' or b == 'remotes/origin/master'
      1
    else
      b <=> a
    end
  end
  
  branches
end

#current_branchObject



37
38
39
40
41
# File 'lib/daddy/git.rb', line 37

def current_branch
  `git branch`.split("\n").each do |b|
    return b.split.last if b.start_with?('*')
  end
end

#previous_branch(remote = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/daddy/git.rb', line 43

def previous_branch(remote = false)
  current_branch = self.current_branch
  branches = self.branches(remote)

  branches.each_with_index do |b, i|
    return nil if i == branches.size - 1

    if b == current_branch or b == "remotes/origin/#{current_branch}"
      return branches[i+1]
    end
  end
end

#show_previous(file, remote = false) ⇒ Object



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

def show_previous(file, remote = false)
  `git show #{previous_branch(remote)}:#{file}`
end