Class: Tddium::Git

Inherits:
Object
  • Object
show all
Extended by:
TddiumConstant
Includes:
TddiumConstant
Defined in:
lib/tddium/scm/git.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.git_changes?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tddium/scm/git.rb', line 125

def git_changes?(options={})
  options[:exclude] ||= []
  options[:exclude] = [options[:exclude]] unless options[:exclude].is_a?(Array)
  cmd = "(git status --porcelain -uno || echo GIT_FAILED) < /dev/null 2>&1"
  p = IO.popen(cmd)
  changes = false
  while line = p.gets do
    if line =~ /GIT_FAILED/
      warn(Text::Warning::SCM_UNABLE_TO_DETECT)
      return false
    end
    line = line.strip
    status, name = line.split(/\s+/)
    next if options[:exclude].include?(name)
    if status !~ /^\?/ then
      changes = true
      break
    end
  end
  return changes
end

.git_push(this_branch, additional_refs = [], remote_branch = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/tddium/scm/git.rb', line 156

def git_push(this_branch, additional_refs=[], remote_branch=nil)
  say Text::Process::SCM_PUSH
  remote_branch ||= this_branch
  refs = ["#{this_branch}:#{remote_branch}"]
  refs += additional_refs
  refspec = refs.map(&:shellescape).join(" ")
  cmd = "git push -f #{Config::REMOTE_NAME} #{refspec}"
  say "Running '#{cmd}'"
  system(cmd)
end

.git_set_remotes(git_repo_uri, remote_name = nil) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/tddium/scm/git.rb', line 147

def git_set_remotes(git_repo_uri, remote_name=nil)
  remote_name ||= Config::REMOTE_NAME

  unless `git remote show -n #{remote_name}` =~ /#{git_repo_uri}/
    `git remote rm #{remote_name} > /dev/null 2>&1`
    `git remote add #{remote_name} #{git_repo_uri.shellescape}`
  end
end

.version_okObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/tddium/scm/git.rb', line 167

def version_ok
  version = nil
  begin
    version_string = `git --version`
    m =  version_string.match(Dependency::VERSION_REGEXP)
    version = m[0] unless m.nil?
  rescue Errno
  rescue Exception
  end
  if version.nil? || version.empty? then
    abort Text::Error::SCM_NOT_FOUND
  end
  version_parts = version.split(".")
  if version_parts[0].to_i < 1 ||
     (version_parts[0].to_i < 2 && version_parts[1].to_i == 1 && version_parts[1].to_i < 7) then
    warn(Text::Warning::GIT_VERSION % version)
  end
  true
end

Instance Method Details

#changes?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/tddium/scm/git.rb', line 77

def changes?(options={})
  return Tddium::Git.git_changes?(:exclude=>".gitignore")
end

#checkout(branch, options = {}) ⇒ Object

XXX DANGER: This method will edit the current workspace. It’s meant to be run to make a git mirror up-to-date.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tddium/scm/git.rb', line 58

def checkout(branch, options={})
  if !!options[:update] then
    `git fetch origin`
    return false if !$?.success?
  end

  cmd = "git checkout "
  if !!options[:force] then
    cmd += "-f "
  end
  cmd += Shellwords.shellescape(branch)
  `#{cmd}`

  return false if !$?.success?

  `git reset --hard origin/#{branch}`
  return $?.success?
end

#commitsObject



106
107
108
109
# File 'lib/tddium/scm/git.rb', line 106

def commits
  commits = GitCommitLogParser.new(self.latest_commit).commits
  return commits
end

#current_branchObject



48
49
50
# File 'lib/tddium/scm/git.rb', line 48

def current_branch
  `git symbolic-ref HEAD`.gsub("\n", "").split("/")[2..-1].join("/")
end

#current_commitObject



102
103
104
# File 'lib/tddium/scm/git.rb', line 102

def current_commit
  `git rev-parse --verify HEAD`.strip
end

#default_branchObject



52
53
54
# File 'lib/tddium/scm/git.rb', line 52

def default_branch
  `git remote show origin | grep HEAD | awk '{print $3}'`.gsub("\n", "")
end

#ignore_pathObject



43
44
45
46
# File 'lib/tddium/scm/git.rb', line 43

def ignore_path
  path = File.join(self.root, Config::GIT_IGNORE)
  return path
end

#mirror_pathObject



29
30
31
# File 'lib/tddium/scm/git.rb', line 29

def mirror_path
  return nil
end

#number_of_commits(id_from, id_to) ⇒ Object



111
112
113
114
# File 'lib/tddium/scm/git.rb', line 111

def number_of_commits(id_from, id_to)
  result = `git log --pretty='%H' #{id_from}..#{id_to}`
  result.split("\n").length
end

#origin_urlObject



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

def origin_url
  result = `(git config --get remote.origin.url || echo GIT_FAILED) 2>/dev/null`
  return nil if result =~ /GIT_FAILED/
  result.strip
end

#push_latest(session_data, suite_details, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tddium/scm/git.rb', line 81

def push_latest(session_data, suite_details, options={})
  branch = options[:branch] || self.current_branch
  remote_branch = options[:remote_branch] || branch
  git_repo_uri = if options[:git_repo_uri] then
                   options[:git_repo_uri]
                 elsif options[:use_private_uri] then
                   suite_details["git_repo_private_uri"] || suite_details["git_repo_uri"]
                 else
                   suite_details["git_repo_uri"]
                 end
  this_ref = (session_data['commit_data'] || {})['git_ref']
  refs = this_ref ? ["HEAD:#{this_ref}"] : []

  if options[:git_repo_origin_uri] then
    Tddium::Git.git_set_remotes(options[:git_repo_origin_uri], 'origin')
  end

  Tddium::Git.git_set_remotes(git_repo_uri)
  return Tddium::Git.git_push(branch, refs, remote_branch)
end

#repo?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
# File 'lib/tddium/scm/git.rb', line 11

def repo?
  if File.directory?('.git') then
    return true
  end
  ignore = `git status 2>&1`
  ok = $?.success?
  return ok
end

#repo_nameObject



33
34
35
# File 'lib/tddium/scm/git.rb', line 33

def repo_name
  return File.basename(self.root)
end

#rootObject



20
21
22
23
24
25
26
27
# File 'lib/tddium/scm/git.rb', line 20

def root
  root = `git rev-parse --show-toplevel 2>&1`
  if $?.exitstatus == 0 then
    root.chomp! if root
    return root
  end
  return Dir.pwd
end

#scm_nameObject



7
8
9
# File 'lib/tddium/scm/git.rb', line 7

def scm_name
  return 'git'
end