Class: Daddy::Git

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

Constant Summary collapse

@@sub_dir =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sub_dir=(sub_dir) ⇒ Object



12
13
14
# File 'lib/daddy/git.rb', line 12

def self.sub_dir=(sub_dir)
  @@sub_dir = sub_dir
end

Instance Method Details

#branches(remote = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/daddy/git.rb', line 16

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
      if remote
        b.sub('remotes/origin/p', '').to_i <=> a.sub('remotes/origin/p', '').to_i
      else
        b.sub('p', '').to_i <=> a.sub('p', '').to_i
      end
    end
  end
  
  branches
end

#current_branchObject



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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/daddy/git.rb', line 76

def git_diff_name(*includes)
  diff = `git diff origin/#{previous_branch} --name-only #{includes.join(' ')}`
  return diff unless @@sub_dir

  ret = []
  diff.split("\n").each do |line|
    index = line.index(@@sub_dir)
    if index
      sub_file = line[@@sub_dir.length..-1]
      sub_file = sub_file[1..-1] if sub_file.start_with?('/')
      ret << sub_file
    else
      ret << line
    end
  end

  ret.join("\n")
end

#previous_branch(remote = false) ⇒ Object



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

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



66
67
68
69
70
71
72
73
74
# File 'lib/daddy/git.rb', line 66

def show_previous(file, remote = false)
  if @@sub_dir
    sub_dir = @@sub_dir
    sub_dir += '/' unless @@sub_dir.end_with?('/')
    `git show #{previous_branch(remote)}:#{sub_dir}#{file}`
  else
    `git show #{previous_branch(remote)}:#{file}`
  end
end