Class: Raykit::Git::Repository

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

Overview

Functionality to manage a remote git repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Repository

Returns a new instance of Repository.



12
13
14
15
16
17
18
19
# File 'lib/raykit/git/repository.rb', line 12

def initialize(url)
  if (url.nil? || url.empty?)
    raise "Raykit::Git::Repository::initialize(url), url is nil or empty!"
  end
  @url = url
  @clone_directory = Raykit::Git::Directory.new(get_dev_dir("clone"))
  @work_directory = Raykit::Git::Directory.new(get_dev_dir("work"))
end

Instance Attribute Details

#clone_directoryObject

Returns the value of attribute clone_directory.



10
11
12
# File 'lib/raykit/git/repository.rb', line 10

def clone_directory
  @clone_directory
end

#urlObject

The url of the remote repository



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

def url
  @url
end

#work_directoryObject

Returns the value of attribute work_directory.



10
11
12
# File 'lib/raykit/git/repository.rb', line 10

def work_directory
  @work_directory
end

Class Method Details

.backup(url, backup_dir) ⇒ Object



263
264
265
266
267
268
269
270
271
# File 'lib/raykit/git/repository.rb', line 263

def self.backup(url, backup_dir)
  if (Dir.exist?(backup_dir))
    Dir.chdir(backup_dir) do
      run("git pull")
    end
  else
    run("git clone #{url} \"#{backup_dir}\"")
  end
end

.get_relative_path(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/raykit/git/repository.rb', line 40

def self.get_relative_path(url)
  uri = URI.parse(url)

  # Remove top-level domain (e.g., ".com")

  if (uri.host.nil?)
    puts "uri.host is nil for #{url}"
  end
  host = uri.host.split(".")[0]
  #host.pop # remove the last element which should be the top-level domain

  #host = host.join(".")


  # Remove scheme (e.g., "http" or "https")

  path = host + uri.path

  # Remove top-level domain (e.g., ".com")

  #path = path.split(".")

  #path.pop # remove the last element which should be the top-level domain

  #path = path.join(".")


  # Remove trailing ".git" if present

  path = path.chomp(".git")

  path
end

.make_url(url, commit_id, command) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/raykit/git/repository.rb', line 243

def self.make_url(url, commit_id, command)
  repo = Raykit::Git::Repository.new(url)
  puts "  make #{url} #{commit_id} #{command}"
  make_dir = Raykit::Environment::normalize_path(repo.get_dev_dir("make") + "/" + commit_id)
  FileUtils.mkdir_p(repo.get_dev_dir("make")) if !Dir.exist?(repo.get_dev_dir("make"))
  if (Dir.exist?(make_dir))
    FileUtils.rm_rf(make_dir)
  end
  run("git clone #{url} #{make_dir}")
  cmd = 0
  Dir.chdir(make_dir) do
    run("git reset --hard #{commit_id}")
    FileUtils.rm_rf(".git")
    cmd = Raykit::Command.new(command)
    cmd = cmd.run().summary()
  end
  FileUtils.rm_rf(make_dir) if (cmd.exitstatus == 0)
  cmd
end

.parse(json) ⇒ Object



29
30
31
32
# File 'lib/raykit/git/repository.rb', line 29

def self.parse(json)
  hash = JSON.parse(json)
  Repository.new(hash["url"])
end

.work_integrate(url) ⇒ Object



220
221
222
223
224
225
226
227
228
# File 'lib/raykit/git/repository.rb', line 220

def self.work_integrate(url)
  repo = Raykit::Git::Repository.new(url)
  work_dir = repo.get_dev_dir("work")
  repo.clone(work_dir) if !Dir.exist?(work_dir)
  Dir.chdir(work_dir) do
    run("git pull")
    run("rake integrate")
  end
end

.work_pull(url) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/raykit/git/repository.rb', line 211

def self.work_pull(url)
  repo = Raykit::Git::Repository.new(url)
  work_dir = repo.get_dev_dir("work")
  repo.clone(work_dir) if !Dir.exist?(work_dir)
  Dir.chdir(work_dir) do
    run("git pull")
  end
end

.work_url(url, cmd) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/raykit/git/repository.rb', line 230

def self.work_url(url, cmd)
  repo = Raykit::Git::Repository.new(url)
  puts "  work #{url} #{cmd}"
  work_dir = repo.get_dev_dir("work")
  repo.clone(work_dir) if !Dir.exist?(work_dir)
  Dir.chdir(work_dir) do
    run("git pull")
    cmd = Raykit::Command.new(cmd)
    cmd = cmd.run().summary()
    cmd
  end
end

Instance Method Details

#branchesObject

The branches for the git repository



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/raykit/git/repository.rb', line 93

def branches
  results = []
  update_local_clone_directory
  if Dir.exist?(local_clone_directory)
    Dir.chdir(local_clone_directory) do
      `git branch`.split('\n').each do |line|
        branch = line.gsub("*", "").strip
        results.insert(-1, branch) if branch.length.positive?
      end
    end
  end
  results
end

#clobberObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/raykit/git/repository.rb', line 157

def clobber
  ["work", "clone", "make"].each { |d|
    dir = get_dev_dir(d)
    if (Dir.exist?(dir))
      begin
        puts "  deleting #{dir}"
        FileUtils.rm_rf(dir)
        FileUtils.rm(dir) if (Dir.exist?(dir))
      rescue
        puts "  problem while deleting #{dir}"
      end
    end
  }
end

#clone(directory, depth = 0) ⇒ Object

Clone the repository to a specific directory



71
72
73
74
75
76
77
# File 'lib/raykit/git/repository.rb', line 71

def clone(directory, depth = 0)
  if depth.zero?
    PROJECT.run("git clone #{@url} #{directory}")
  else
    PROJECT.run("git clone #{@url} #{directory} --depth #{depth}")
  end
end

#default_branchObject

default branch



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/raykit/git/repository.rb', line 80

def default_branch
  update_local_clone_directory
  if Dir.exist?(local_clone_directory)
    Dir.chdir(local_clone_directory) do
      # refs/remotes/origin/master

      default_branch = `git symbolic-ref refs/remotes/origin/HEAD`.split("/").last.strip
      return default_branch
    end
  end
  "main"
end

#get_dev_dir(dir) ⇒ Object



65
66
67
68
# File 'lib/raykit/git/repository.rb', line 65

def get_dev_dir(dir)
  dev_dir = Environment.get_dev_dir(dir)
  Raykit::Environment::normalize_path("#{dev_dir}/#{relative_path}")
end

#latest_commit(branch) ⇒ Object

The latest commit id for a branch of the repostiory



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/raykit/git/repository.rb', line 108

def latest_commit(branch)
  if checkout_local_clone_directory_branch(branch)
    update_local_clone_directory
    Dir.chdir(local_clone_directory) do
      text = `git log -n 1`
      scan = text.scan(/commit (\w+)\s/)
      return scan[0][0].to_s
    end
  end
  ""
end

#latest_tag(branch) ⇒ Object

The latest tag for a branch of the repository



121
122
123
124
125
# File 'lib/raykit/git/repository.rb', line 121

def latest_tag(branch)
  return `git describe --abbrev=0`.strip if checkout_local_clone_directory_branch(branch)

  ""
end

#make(command, force = false) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/raykit/git/repository.rb', line 197

def make(command, force = false)
  commit_id = "#{latest_commit(default_branch)}"
  make_id = "make_#{relative_path.gsub("/", "-")}_#{commit_id}"
  fcommand = Raykit::FileSystem::replace_invalid_chars(command)
  filename = "#{Raykit::Environment::log_dir}/#{make_id}_#{fcommand}.json"
  if (!force && File.exist?(filename))
    return Raykit::Command::parse(IO.read(filename))
  else
    make_cmd = Raykit::Git::Repository::make_url(url, commit_id, "rake default")
    make_cmd.save_as(filename)
    return make_cmd
  end
end

#pullObject



172
173
174
175
# File 'lib/raykit/git/repository.rb', line 172

def pull
  Raykit::Git::Repository::work_pull(url)
  update_local_clone_directory
end

#relative_pathObject

The relative path is a valid local path name derived from the url



35
36
37
38
# File 'lib/raykit/git/repository.rb', line 35

def relative_path
  Repository::get_relative_path(@url)
  #@url.gsub("https://", "").gsub(".git", "").gsub("http://", "")

end

#short_nameObject



21
22
23
# File 'lib/raykit/git/repository.rb', line 21

def short_name()
  @url.split("/").last.gsub(".git", "")
end

#to_json(*_args) ⇒ Object



25
26
27
# File 'lib/raykit/git/repository.rb', line 25

def to_json(*_args)
  JSON.generate({ "url" => @url })
end

#to_sObject

def self.backup



273
274
275
# File 'lib/raykit/git/repository.rb', line 273

def to_s
  "Name: #{short_name}\nUrl: #{@url}\nRelative Path: #{relative_path}"
end

#to_tableObject

def to_s



277
278
279
280
281
282
283
284
285
286
# File 'lib/raykit/git/repository.rb', line 277

def to_table
  #max_name_width = 10

  #max_value_width = 10

  #header = "  =".ljust(max_name_width + max_value_width + 5, "=")

  header = "==Repository=="
  table = header
  table += "\n" + to_table_row("Name", short_name)
  table += "\n" + to_table_row("Url", @url)
  table
end

#to_table_row(name, value) ⇒ Object

def to_table



288
289
290
291
292
# File 'lib/raykit/git/repository.rb', line 288

def to_table_row(name, value)
  max_name_width = 10
  max_value_width = 30
  Rainbow(name.rjust(max_name_width, " ")).cyan + " | " + Rainbow(value).white.bold
end

#update_local_clone_directoryObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/raykit/git/repository.rb', line 131

public def update_local_clone_directory
  if Dir.exist?(local_clone_directory)
    Dir.chdir(local_clone_directory) do
      pull = Raykit::Command.new("git pull")
      pull.run
      pull
      # t = `git pull`

    end
  else
    PROJECT.run("git clone #{@url} #{local_clone_directory}")
  end
end

#work(command, force = false) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/raykit/git/repository.rb', line 177

def work(command, force = false)
  pull if (!Dir.exist?(get_dev_dir("work")))
  fcommand = Raykit::FileSystem::replace_invalid_chars(command)
  filename = "#{Raykit::Environment::log_dir}/work_#{fcommand}_#{relative_path.gsub("/", "-")}.json"
  if (Raykit::Git::Directory.new(get_dev_dir("work")).outstanding_commit? || force)
    puts "  outstanding commit in #{get_dev_dir("work")}"
    work_cmd = Raykit::Git::Repository::work_url(url, command)
    work_cmd.save_as(filename)
    return work_cmd
  else
    if (File.exist?(filename))
      return Raykit::Command::parse(IO.read(filename))
    else
      work_cmd = Raykit::Git::Repository::work_url(url, command)
      work_cmd.save_as(filename)
    end
    #

  end
end