Class: DTK::Network::Client::GitGem

Inherits:
Object
  • Object
show all
Defined in:
lib/client/git_adapter/git_gem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_dir, opts = {}) ⇒ GitGem

opts can have keys

:branch


66
67
68
69
70
# File 'lib/client/git_adapter/git_gem.rb', line 66

def initialize(repo_dir, opts = {})
  @repo_dir          = repo_dir
  @git_repo          = ::Git.init(repo_dir)
  @local_branch_name = opts[:branch]
end

Instance Attribute Details

#git_repoObject

Returns the value of attribute git_repo.



6
7
8
# File 'lib/client/git_adapter/git_gem.rb', line 6

def git_repo
  @git_repo
end

Class Method Details

.clone(repo_url, target_path, branch) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/client/git_adapter/git_gem.rb', line 72

def self.clone(repo_url, target_path, branch)
  git_base = ::Git.clone(repo_url, target_path)
  begin
    git_base.checkout(branch)
  rescue => e
    raise "The branch or tag '#{branch}' does not exist on repo '#{repo_url}'"
  end
  git_base
end

Instance Method Details

#add(*files) ⇒ Object



191
192
193
# File 'lib/client/git_adapter/git_gem.rb', line 191

def add(*files)
  @git_repo.add(files.flatten)
end

#add_allObject



195
196
197
198
199
# File 'lib/client/git_adapter/git_gem.rb', line 195

def add_all
  # Cannot use '@git_repo.add(:all => true)' because this only works if pwd is base git repo
  fully_qualified_repo_dir = (@repo_dir =~ /^\// ? @repo_dir : File.join(Dir.pwd, @repo_dir))
  @git_repo.add(fully_qualified_repo_dir, :all => true )
end

#add_remote(name, url) ⇒ Object



94
95
96
97
# File 'lib/client/git_adapter/git_gem.rb', line 94

def add_remote(name, url)
  @git_repo.remove_remote(name) if is_there_remote?(name)
  @git_repo.add_remote(name, url)
end

#addedObject



129
130
131
# File 'lib/client/git_adapter/git_gem.rb', line 129

def added
  status.is_a?(Hash) ? status.added().keys : status.added().collect { |file| file.first }
end

#all_branchesObject



234
235
236
# File 'lib/client/git_adapter/git_gem.rb', line 234

def all_branches
  @git_repo.branches
end

#changedObject



117
118
119
# File 'lib/client/git_adapter/git_gem.rb', line 117

def changed
  status.is_a?(Hash) ? status.changed().keys : status.changed().collect { |file| file.first }
end

#changed?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/client/git_adapter/git_gem.rb', line 229

def changed?
  (!(changed().empty? && untracked().empty? && deleted().empty?))
end

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

opts can have keys

:new_branch - Boolean


84
85
86
87
88
# File 'lib/client/git_adapter/git_gem.rb', line 84

def checkout(branch, opts = {})
  ret = @git_repo.checkout(branch, opts)
  @local_branch_name = branch
  ret
end

#commit(commit_msg = "", opts = {}) ⇒ Object

opts can have keys

:allow_empty


187
188
189
# File 'lib/client/git_adapter/git_gem.rb', line 187

def commit(commit_msg = "", opts = {})
  @git_repo.commit(commit_msg, :allow_empty => opts[:allow_empty])
end

#current_branchObject



205
206
207
# File 'lib/client/git_adapter/git_gem.rb', line 205

def current_branch
  @git_repo.branches.local.find { |b| b.current }
end

#deletedObject



125
126
127
# File 'lib/client/git_adapter/git_gem.rb', line 125

def deleted
  status.is_a?(Hash) ? status.deleted().keys : status.deleted().collect { |file| file.first }
end

#diffObject



221
222
223
# File 'lib/client/git_adapter/git_gem.rb', line 221

def diff
  @git_repo.diff
end

#diff_name_status(branch_or_sha_1, branch_or_sha_2, opts = {}) ⇒ Object



225
226
227
# File 'lib/client/git_adapter/git_gem.rb', line 225

def diff_name_status(branch_or_sha_1, branch_or_sha_2, opts = {})
  @git_repo.name_status(branch_or_sha_1, branch_or_sha_2, opts)
end

#empty_commit(commit_msg = nil) ⇒ Object



143
144
145
146
# File 'lib/client/git_adapter/git_gem.rb', line 143

def empty_commit(commit_msg = nil)
  commit_msg ||= default_commit_message
  commit(commit_msg, :allow_empty => true)
end

#fetch(remote = 'origin') ⇒ Object



90
91
92
# File 'lib/client/git_adapter/git_gem.rb', line 90

def fetch(remote = 'origin')
  @git_repo.fetch(remote)
end

#head_commit_shaObject



213
214
215
# File 'lib/client/git_adapter/git_gem.rb', line 213

def head_commit_sha
  current_branch.gcommit.sha
end

#is_there_remote?(remote_name) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/client/git_adapter/git_gem.rb', line 201

def is_there_remote?(remote_name)
  @git_repo.remotes.find { |r| r.name == remote_name }
end

#local_ahead(base_sha, remote_sha) ⇒ Object



164
165
166
167
# File 'lib/client/git_adapter/git_gem.rb', line 164

def local_ahead(base_sha, remote_sha)
  results = @git_repo.rev_list(base_sha)
  !results.split("\n").grep(remote_sha).empty?
end

#merge(branch_to_merge_from, opts = {}) ⇒ Object



109
110
111
# File 'lib/client/git_adapter/git_gem.rb', line 109

def merge(branch_to_merge_from, opts = {})
  @git_repo.merge(branch_to_merge_from, 'merge', :allow_unrelated_histories => allow_unrelated_histories?, :use_theirs => opts[:use_theirs])
end

#pull(remote, branch) ⇒ Object



217
218
219
# File 'lib/client/git_adapter/git_gem.rb', line 217

def pull(remote, branch)
  @git_repo.pull(remote, branch)
end

#push(remote, branch, opts = {}) ⇒ Object



103
104
105
106
107
# File 'lib/client/git_adapter/git_gem.rb', line 103

def push(remote, branch, opts = {})
  branch_name = current_branch ? current_branch.name : 'master'
  branch_for_push = "#{branch_name}:refs/heads/#{branch || local_branch_name}"
  @git_repo.push(remote, branch_for_push, opts)
end

#remotesObject



209
210
211
# File 'lib/client/git_adapter/git_gem.rb', line 209

def remotes
  @git_repo.remotes
end

#remove_remote(name) ⇒ Object



99
100
101
# File 'lib/client/git_adapter/git_gem.rb', line 99

def remove_remote(name)
  @git_repo.remove_remote(name) if is_there_remote?(name)
end

#reset_hard(sha) ⇒ Object



152
153
154
# File 'lib/client/git_adapter/git_gem.rb', line 152

def reset_hard(sha)
  @git_repo.reset_hard(sha)
end

#reset_soft(sha) ⇒ Object



148
149
150
# File 'lib/client/git_adapter/git_gem.rb', line 148

def reset_soft(sha)
  @git_repo.reset(sha)
end

#rev_list(base_sha) ⇒ Object



160
161
162
# File 'lib/client/git_adapter/git_gem.rb', line 160

def rev_list(base_sha)
  @git_repo.rev_list(base_sha)
end

#revparse(sha_or_string) ⇒ Object



156
157
158
# File 'lib/client/git_adapter/git_gem.rb', line 156

def revparse(sha_or_string)
  @git_repo.revparse(sha_or_string)
end

#stage_and_commit(commit_msg = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/client/git_adapter/git_gem.rb', line 133

def stage_and_commit(commit_msg = nil)
  commit_msg ||= default_commit_message
  add_all
  begin
    commit(commit_msg)
  rescue
    # do not raise if nothing to commit
  end
end

#stage_changesObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/client/git_adapter/git_gem.rb', line 169

def stage_changes()
  @git_repo.add(untracked())
  @git_repo.add(added())
  @git_repo.add(changed())

  deleted().each do |file|
    begin
      @git_repo.remove(file)
    rescue
      # ignore this error means file has already been staged
      # we cannot support status of file, in 1.8.7 so this is
      # solution for that
    end
  end
end

#statusObject



113
114
115
# File 'lib/client/git_adapter/git_gem.rb', line 113

def status
  @git_repo.status
end

#untrackedObject



121
122
123
# File 'lib/client/git_adapter/git_gem.rb', line 121

def untracked
  status.is_a?(Hash) ? status.untracked().keys : status.untracked().collect { |file| file.first }
end