Class: DTK::Network::Client::GitRepo

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

Class Method Summary collapse

Class Method Details

.add_remote(repo, remote_name, remote_url) ⇒ Object



81
82
83
84
# File 'lib/client/git_repo.rb', line 81

def self.add_remote(repo, remote_name, remote_url)
  repo.add_remote(remote_name, remote_url)
  remote_name
end

.add_remote_and_publish(git_args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/client/git_repo.rb', line 3

def self.add_remote_and_publish(git_args)
  Command.wrap_command(git_args) do |git_args|
    repo_dir      = git_args.required(:repo_dir)
    remote_url    = git_args.required(:remote_url)
    local_branch  = git_args[:branch] || 'master'
    remote_branch = git_args[:remote_branch] || local_branch
    remote        = git_args[:remote] || 'origin'

    repo = git_repo.new(repo_dir, :branch => local_branch)
    create_if_missing = local_branch_exist?(repo, local_branch) ? false : true

    repo.checkout(local_branch, new_branch: create_if_missing)
    repo.add_all
    repo.commit("Publish from dtk client", :allow_empty => true)
    add_remote_and_push(repo, remote, remote_url, remote_branch)
  end
end

.add_remote_and_pull(repo, remote, remote_url, local_branch, remote_branch, opts = {}) ⇒ Object



105
106
107
108
# File 'lib/client/git_repo.rb', line 105

def self.add_remote_and_pull(repo, remote, remote_url, local_branch, remote_branch, opts = {})
  repo.add_remote(remote, remote_url)
  opts[:force] ? force_pull(repo, remote, remote_branch) : reset_if_error(repo, local_branch) { repo.pull(remote, remote_branch) }
end

.add_remote_and_push(repo, remote, remote_url, remote_branch, opts = {}) ⇒ Object



100
101
102
103
# File 'lib/client/git_repo.rb', line 100

def self.add_remote_and_push(repo, remote, remote_url, remote_branch, opts = {})
  repo.add_remote(remote, remote_url)
  repo.push(remote, remote_branch, opts)
end

.all_branches(args) ⇒ Object

def self.add_remote_and_push(repo, repo_url, remote_branch)

repo.add_remote(Dtk_Server::GIT_REMOTE, repo_url)
repo.stage_and_commit
repo.push(Dtk_Server::GIT_REMOTE, remote_branch, { :force => true })

end



207
208
209
210
211
# File 'lib/client/git_repo.rb', line 207

def self.all_branches(args)
  repo_url = args.required(:path)
  repo = git_repo.new(repo_url)
  repo.all_branches
end

.create_empty_git_repo?(repo_dir, opts = {}) ⇒ Boolean

opts can have keys

:branch

returns object of type DTK::Client::GitRepo

Returns:

  • (Boolean)


71
72
73
# File 'lib/client/git_repo.rb', line 71

def self.create_empty_git_repo?(repo_dir, opts = {})
  git_repo.new(repo_dir, :branch => opts[:branch])
end

.current_branch(args) ⇒ Object



213
214
215
216
217
# File 'lib/client/git_repo.rb', line 213

def self.current_branch(args)
  repo_url = args.required(:path)
  repo = git_repo.new(repo_url)
  repo.current_branch.name
end

.empty_commit(repo, commit_msg = nil) ⇒ Object

returns head_sha



76
77
78
79
# File 'lib/client/git_repo.rb', line 76

def self.empty_commit(repo, commit_msg = nil)
  repo.empty_commit(commit_msg)
  repo.head_commit_sha
end

.fetch(repo, remote_name) ⇒ Object



86
87
88
# File 'lib/client/git_repo.rb', line 86

def self.fetch(repo, remote_name)
  repo.fetch(remote_name)
end

.force_pull(repo, remote, remote_branch) ⇒ Object



110
111
112
113
# File 'lib/client/git_repo.rb', line 110

def self.force_pull(repo, remote, remote_branch)
  repo.fetch(remote)
  repo.reset_hard("#{remote}/#{remote_branch}")
end

.git_repoObject



219
220
221
# File 'lib/client/git_repo.rb', line 219

def self.git_repo
  GitClient
end

.local_ahead?(repo, merge_from_ref, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/client/git_repo.rb', line 130

def self.local_ahead?(repo, merge_from_ref, opts = {})
  base_sha = repo.head_commit_sha
  remote_branch = repo.all_branches.remote.find { |r| "#{r.remote}/#{r.name}" == merge_from_ref }
  remote_sha = remote_branch.gcommit.sha
  repo.local_ahead(base_sha, remote_sha)
end

.local_branch_exist?(repo, branch) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
# File 'lib/client/git_repo.rb', line 115

def self.local_branch_exist?(repo, branch)
  local_branches = branch.is_a?(String) ? repo.all_branches.local.map { |branch| branch.name } : (repo.all_branches.local || [])
  local_branches.include?(branch)
end

.merge(repo, merge_from_ref, opts = {}) ⇒ Object

opts can have keys

:no_commit


122
123
124
125
126
127
128
# File 'lib/client/git_repo.rb', line 122

def self.merge(repo, merge_from_ref, opts = {})
  base_sha = repo.head_commit_sha
  repo.merge(merge_from_ref, :use_theirs => opts[:use_theirs])
  # the git gem does not take no_commit as merge argument; so doing it with soft reset
  repo.reset_soft(base_sha) if opts[:no_commit]
  repo.head_commit_sha
end

.pull_from_remote(git_args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/client/git_repo.rb', line 48

def self.pull_from_remote(git_args)
  Command.wrap_command(git_args) do |git_args|
    repo_dir      = git_args.required(:repo_dir)
    remote_url    = git_args.required(:remote_url)
    local_branch  = git_args[:branch] || 'master'
    remote_branch = git_args[:remote_branch] || local_branch
    remote        = git_args[:remote] || 'origin'
    force         = git_args[:force]

    repo = git_repo.new(repo_dir, :branch => local_branch)
    repo.checkout(local_branch)

    if repo.is_there_remote?(remote)
      pull_when_there_is_remote(repo, remote, remote_url, local_branch, remote_branch, { force: force })
    else
      add_remote_and_pull(repo, remote, remote_url, local_branch, remote_branch, { force: force })
    end
  end
end

.pull_when_there_is_remote(repo, remote, remote_url, local_branch, remote_branch, opts = {}) ⇒ Object



95
96
97
98
# File 'lib/client/git_repo.rb', line 95

def self.pull_when_there_is_remote(repo, remote, remote_url, local_branch, remote_branch, opts = {})
  repo.remove_remote(remote)
  add_remote_and_pull(repo, remote, remote_url, local_branch, remote_branch, opts)
end

.push_to_remote(git_args) ⇒ Object



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
46
# File 'lib/client/git_repo.rb', line 21

def self.push_to_remote(git_args)
  Command.wrap_command(git_args) do |git_args|
    repo_dir      = git_args.required(:repo_dir)
    remote_url    = git_args.required(:remote_url)
    local_branch  = git_args[:branch] || 'master'
    remote_branch = git_args[:remote_branch] || local_branch
    remote        = git_args[:remote] || 'origin'
    commit_msg    = git_args[:commit_msg] || "Push from dtkn client"
    force         = git_args[:force]

    repo = git_repo.new(repo_dir, :branch => local_branch)
    create_if_missing = local_branch_exist?(repo, local_branch) ? false : git_args[:create_if_missing]

    repo.checkout(local_branch, new_branch: create_if_missing)
    repo.add_all
    repo.commit(commit_msg, :allow_empty => true)
    diffs = repo.diff_name_status("#{remote}/#{remote_branch}")

    if repo.is_there_remote?(remote)
      push_when_there_is_remote(repo, remote, remote_url, remote_branch, { force: force })
    else
      add_remote_and_push(repo, remote, remote_url, remote_branch, { force: force })
    end
    diffs
  end
end

.push_when_there_is_remote(repo, remote, remote_url, remote_branch, opts = {}) ⇒ Object



90
91
92
93
# File 'lib/client/git_repo.rb', line 90

def self.push_when_there_is_remote(repo, remote, remote_url, remote_branch, opts = {})
  repo.remove_remote(remote)
  add_remote_and_push(repo, remote, remote_url, remote_branch, opts)
end

.reset_hard(repo, merge_from_ref) ⇒ Object



223
224
225
226
# File 'lib/client/git_repo.rb', line 223

def self.reset_hard(repo, merge_from_ref)
  repo.reset_hard(merge_from_ref)
  repo.head_commit_sha
end

.reset_if_error(repo, branch, &block) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/client/git_repo.rb', line 228

def self.reset_if_error(repo, branch, &block)
  ret = nil
  begin
    sha_before_operations =  repo.revparse(branch)
    ret = yield
  rescue => e
    # reset to enable checkout of another branch
    repo.add_all
    repo.reset_hard(sha_before_operations)
    raise e
  end
  ret
end

.stage_and_commit(repo_dir, local_branch_type, opts = {}) ⇒ Object

opts can have keys:

:commit_msg

returns head_sha



140
141
142
143
144
145
# File 'lib/client/git_repo.rb', line 140

def self.stage_and_commit(repo_dir,local_branch_type, opts = {})
  local_branch = branch_from_local_branch_type(local_branch_type)
  repo = create_empty_git_repo?(repo_dir, :branch => local_branch)
  repo.stage_and_commit(opts[:commit_msg])
  repo.head_commit_sha
end