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



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

def branches(remote = false)
  branches = []
  `git branch -a`.split("\n").each do |b|
    next unless b.index('remotes/origin/master') or b.index(/remotes\/origin\/p[0-9]+(\.[0-9]+)?/)
    
    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
      if remote
        b.sub('remotes/origin/p', '').to_f <=> a.sub('remotes/origin/p', '').to_f
      else
        b.sub('p', '').to_f <=> a.sub('p', '').to_f
      end
    end
  end
  
  branches
end

#current_branchObject



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

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

#git_diff_name(*includes) ⇒ Object



64
65
66
# File 'lib/daddy/git.rb', line 64

def git_diff_name(*includes)
  `git diff origin/#{previous_branch} --name-only #{includes.join(' ')}`
end

#previous_branch(remote = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/daddy/git.rb', line 40

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, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/daddy/git.rb', line 53

def show_previous(file, options = {})
  remote = options[:remote]
  commit = options[:commit]

  if commit
    `git show #{commit}:#{file}`
  else
    `git show #{previous_branch(remote)}:#{file}`
  end
end