Class: MSPRelease::Git

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Exec::Helpers
Defined in:
lib/msp_release/git.rb

Defined Under Namespace

Modules: ClassMethods Classes: Commit

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

clone, version, version_gte

Methods included from Exec::Helpers

#exec

Constructor Details

#initialize(project, options) ⇒ Git

Returns a new instance of Git.



73
74
75
76
# File 'lib/msp_release/git.rb', line 73

def initialize(project, options)
  @project = project
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



80
81
82
# File 'lib/msp_release/git.rb', line 80

def options
  @options
end

Instance Method Details

#added_filesObject



107
108
109
# File 'lib/msp_release/git.rb', line 107

def added_files
  status_files("new file")
end

#branch_exists?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def branch_exists?(branch_name)
  # use backticks, we don't want an error
  `git show-branch origin/#{branch_name}`
  $? == 0
end

#create_and_switch(branch_name, options = {}) ⇒ Object



100
101
102
103
104
105
# File 'lib/msp_release/git.rb', line 100

def create_and_switch(branch_name, options={})
  start_point = options[:start_point]
  exec "git branch #{branch_name} #{start_point}"
  exec "git push origin #{branch_name}"
  exec "git checkout #{branch_name}"
end

#cur_branchObject



86
87
88
# File 'lib/msp_release/git.rb', line 86

def cur_branch
  /^\* (.+)$/.match(exec "git branch")[1]
end

#exec_nameObject



78
# File 'lib/msp_release/git.rb', line 78

def exec_name; 'git'; end

#last_release_tagObject



90
91
92
# File 'lib/msp_release/git.rb', line 90

def last_release_tag
  a = exec "git describe --tags --match 'release-*'"
end

#latest_commit(project) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/msp_release/git.rb', line 121

def latest_commit(project)
  commit_output =
    exec("git --no-pager log -1 --no-color --full-index --pretty=short").split("\n")

  commit_pattern = /^commit ([a-z0-9]+)$/
  author_pattern = /^Author: (.+)$/
  message_pattern = /^    (.+)$/

  hash = commit_output.grep(commit_pattern) {|m| commit_pattern.match(m)[1] }.first
  author = commit_output.grep(author_pattern) {|m| author_pattern.match(m)[1] }.first
  message = commit_output.grep(message_pattern).
    map {|row| message_pattern.match(row)[1] }.
    join(" ")

  Commit.new(project, {:hash => hash, :author => author, :message => message})
end

#modified_filesObject



111
112
113
# File 'lib/msp_release/git.rb', line 111

def modified_files
  status_files("modified")
end

#on_master?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/msp_release/git.rb', line 82

def on_master?
  cur_branch == 'master'
end

#remote_is_ahead?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


138
139
140
141
142
143
# File 'lib/msp_release/git.rb', line 138

def remote_is_ahead?
  raise NotImplementedError
  branch_name = cur_branch
  exec "git fetch"
  exec "git --no-pager log origin/#{branch_name} --no-color --oneline -1".split("")
end

#status_files(status) ⇒ Object



115
116
117
118
119
# File 'lib/msp_release/git.rb', line 115

def status_files(status)
  output = exec "git status", :status => :any
  pattern = /#{status}: +(.+)$/
  output.split("\n").map {|l| pattern.match(l) }.compact.map{|m|m[1]}
end