Class: Cherrybase::Git

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

Instance Method Summary collapse

Constructor Details

#initialize(cmd = Cherrybase::Cmd.new) ⇒ Git

Returns a new instance of Git.



5
6
7
# File 'lib/git.rb', line 5

def initialize(cmd = Cherrybase::Cmd.new)
  @cmd = cmd
end

Instance Method Details

#cherry_pick(commit_hash) ⇒ Object



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

def cherry_pick(commit_hash)
  @cmd.run("git cherry-pick #{commit_hash}")
end

#commit(commit_hash) ⇒ Object



46
47
48
# File 'lib/git.rb', line 46

def commit(commit_hash)
  @cmd.run("git commit -C #{commit_hash}")
end

#commits_to_cherrypick(branch_name, first_commit = nil, last_commit = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/git.rb', line 72

def commits_to_cherrypick(branch_name, first_commit = nil, last_commit = nil)
  commits = []
  @cmd.run("git log #{branch_name} --pretty=oneline").each do |line|
    commit_hash = line.split(' ')[0]
    if commit_hash == first_commit
      commits << commit_hash
      break
    else
      commits << commit_hash
    end
  end
  
  if last_commit
    remove_commits = []
    commits.each do |commit|
      if  commit == last_commit
         break
      else
         remove_commits << commit
      end
  end
  commits = commits - remove_commits
 end
 
 commits.reverse
end

#current_branchObject



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

def current_branch()
  @cmd.run("git branch").each do |line|
    if line.index('*')
      return line.gsub(/\*(.+)/, '\1').strip
    end
  end
  nil
end

#has_branch?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_branch?(branch_name)
  @cmd.run("git branch").each do |line|
    if line.strip == branch_name
      return true
    end
  end
  false
end

#has_commit?(branch_name, commit_hash) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_commit?(branch_name, commit_hash)
  resolve_commit(branch_name, commit_hash) != nil
end

#has_conflicts?Boolean

Returns:

  • (Boolean)


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

def has_conflicts?()
  @cmd.run("git ls-files -tu").length > 0
end

#last_commit(branch_name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/git.rb', line 29

def last_commit(branch_name)
  lines = @cmd.run("git log #{branch_name} --pretty=oneline")
  if (lines.length > 0)
    lines[0].split(' ')[0]
  else
    nil
  end
end

#last_svn_commitObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/git.rb', line 99

def last_svn_commit()
  last_commit_hash = nil
  svn_commit_found = false
  @cmd.run("git log").each do |line|
    if line.include?("git-svn-id")
      svn_commit_found = true
      break
    else
      if line =~ /commit [a-z0-9]+$/
        last_commit_hash = line[7,line.length]
      end
    end
  end
  if (!svn_commit_found)
    last_commit_hash = nil
  end
  last_commit_hash
end

#reset(commit_hash) ⇒ Object



9
10
11
# File 'lib/git.rb', line 9

def reset(commit_hash)
  @cmd.run("git reset --hard #{commit_hash}")
end

#resolve_commit(branch_name, commit_hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git.rb', line 17

def resolve_commit(branch_name, commit_hash)
  if commit_hash
    raise "Please supply at least 5 characters for a commit hash" if commit_hash.length < 5
    @cmd.run("git log #{branch_name} --pretty=oneline").each do |line|
      if line == commit_hash || line.include?(commit_hash)
        return line.split(' ')[0]
      end
    end
  end
  nil
end

#statusObject



50
51
52
# File 'lib/git.rb', line 50

def status()
  @cmd.run("git status", true)
end