Class: Raykit::Git::Repository
- Inherits:
-
Object
- Object
- Raykit::Git::Repository
- Defined in:
- lib/raykit/git/repository.rb
Overview
Functionality to manage a remote git repository
Instance Attribute Summary collapse
-
#clone_directory ⇒ Object
Returns the value of attribute clone_directory.
-
#latest_commit_exit_status ⇒ Object
The url of the remote repository.
-
#latest_commit_id ⇒ Object
The url of the remote repository.
-
#url ⇒ Object
The url of the remote repository.
-
#work_directory ⇒ Object
Returns the value of attribute work_directory.
Class Method Summary collapse
- .backup(url, backup_dir) ⇒ Object
- .get_relative_path(url) ⇒ Object
- .make_url(url, commit_id, command) ⇒ Object
- .parse(json) ⇒ Object
- .work_integrate(url) ⇒ Object
- .work_pull(url) ⇒ Object
- .work_url(url, cmd) ⇒ Object
Instance Method Summary collapse
-
#branches ⇒ Object
The branches for the git repository.
- #clobber ⇒ Object
-
#clone(directory, depth = 0) ⇒ Object
Clone the repository to a specific directory.
-
#default_branch ⇒ Object
default branch.
- #filename ⇒ Object
- #get_dev_dir(dir) ⇒ Object
- #get_make_command(commit_id, command) ⇒ Object
-
#initialize(url) ⇒ Repository
constructor
A new instance of Repository.
-
#latest_commit(branch) ⇒ Object
The latest commit id for a branch of the repostiory.
- #latest_commit_date(branch) ⇒ Object
-
#latest_tag(branch) ⇒ Object
The latest tag for a branch of the repository.
- #load ⇒ Object
- #make(command, force = false) ⇒ Object
- #pull ⇒ Object
-
#relative_path ⇒ Object
The relative path is a valid local path name derived from the url.
- #save ⇒ Object
- #short_name ⇒ Object
- #to_json(*_args) ⇒ Object
-
#to_s ⇒ Object
def self.backup.
-
#to_table ⇒ Object
def to_s.
-
#to_table_row(name, value) ⇒ Object
def to_table.
- #update ⇒ Object
- #update_local_clone_directory ⇒ Object
- #work(command, force = false) ⇒ Object
Constructor Details
#initialize(url) ⇒ Repository
Returns a new instance of Repository.
12 13 14 15 16 17 18 19 20 |
# 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")) load end |
Instance Attribute Details
#clone_directory ⇒ Object
Returns the value of attribute clone_directory.
10 11 12 |
# File 'lib/raykit/git/repository.rb', line 10 def clone_directory @clone_directory end |
#latest_commit_exit_status ⇒ Object
The url of the remote repository
9 10 11 |
# File 'lib/raykit/git/repository.rb', line 9 def latest_commit_exit_status @latest_commit_exit_status end |
#latest_commit_id ⇒ Object
The url of the remote repository
9 10 11 |
# File 'lib/raykit/git/repository.rb', line 9 def latest_commit_id @latest_commit_id end |
#url ⇒ Object
The url of the remote repository
9 10 11 |
# File 'lib/raykit/git/repository.rb', line 9 def url @url end |
#work_directory ⇒ Object
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
319 320 321 322 323 324 325 326 327 |
# File 'lib/raykit/git/repository.rb', line 319 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/raykit/git/repository.rb', line 74 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/raykit/git/repository.rb', line 299 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
64 65 66 67 |
# File 'lib/raykit/git/repository.rb', line 64 def self.parse(json) hash = JSON.parse(json) Repository.new(hash["url"]) end |
.work_integrate(url) ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/raykit/git/repository.rb', line 276 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
267 268 269 270 271 272 273 274 |
# File 'lib/raykit/git/repository.rb', line 267 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
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/raykit/git/repository.rb', line 286 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().details() cmd end end |
Instance Method Details
#branches ⇒ Object
The branches for the git repository
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/raykit/git/repository.rb', line 127 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 |
#clobber ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/raykit/git/repository.rb', line 203 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
105 106 107 108 109 110 111 |
# File 'lib/raykit/git/repository.rb', line 105 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_branch ⇒ Object
default branch
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/raykit/git/repository.rb', line 114 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 |
#filename ⇒ Object
22 23 24 |
# File 'lib/raykit/git/repository.rb', line 22 def filename Environment.get_dev_dir("data") + File::SEPARATOR + "repository" + File::SEPARATOR + Raykit::FileSystem::replace_invalid_chars(relative_path.gsub("/", ".").gsub("\\", ".")) + ".json" end |
#get_dev_dir(dir) ⇒ Object
99 100 101 102 |
# File 'lib/raykit/git/repository.rb', line 99 def get_dev_dir(dir) dev_dir = Environment.get_dev_dir(dir) Raykit::Environment::normalize_path("#{dev_dir}/#{relative_path}") end |
#get_make_command(commit_id, command) ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/raykit/git/repository.rb', line 244 def get_make_command(commit_id, command) make_id = "make_#{relative_path.gsub("/", "-")}_#{commit_id}" fcommand = Raykit::FileSystem::replace_invalid_chars(command) filename = "#{Raykit::Environment::log_dir}/#{make_id}_#{fcommand}.json" return Raykit::Command::parse(IO.read(filename)) if (File.exist?(filename)) nil end |
#latest_commit(branch) ⇒ Object
The latest commit id for a branch of the repostiory
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/raykit/git/repository.rb', line 142 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_commit_date(branch) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/raykit/git/repository.rb', line 154 def latest_commit_date(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(/Date:\s+(.*)/) return scan[0][0].to_s end # Dir.chdir end # if checkout_local_clone_directory_branch "" end |
#latest_tag(branch) ⇒ Object
The latest tag for a branch of the repository
167 168 169 170 171 |
# File 'lib/raykit/git/repository.rb', line 167 def latest_tag(branch) return `git describe --abbrev=0`.strip if checkout_local_clone_directory_branch(branch) "" end |
#load ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/raykit/git/repository.rb', line 37 def load if (File.exist?(filename)) data = JSON.parse(File.read(filename)) @latest_commit_id = data["latest_commit_id"] @latest_commit_exit_status = data["latest_commit_exit_status"] end end |
#make(command, force = false) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/raykit/git/repository.rb', line 252 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) update return make_cmd end end |
#pull ⇒ Object
218 219 220 221 |
# File 'lib/raykit/git/repository.rb', line 218 def pull Raykit::Git::Repository::work_pull(url) update_local_clone_directory end |
#relative_path ⇒ Object
The relative path is a valid local path name derived from the url
70 71 72 |
# File 'lib/raykit/git/repository.rb', line 70 def relative_path Repository::get_relative_path(@url) end |
#save ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/raykit/git/repository.rb', line 26 def save # Create the config directory if it doesn't exist. dir = File.dirname(filename) FileUtils.mkdir_p(dir) unless Dir.exist?(dir) File.write(filename, { latest_commit_id: @latest_commit_id, latest_commit_exit_status: @latest_commit_exit_status, }.to_json) end |
#short_name ⇒ Object
56 57 58 |
# File 'lib/raykit/git/repository.rb', line 56 def short_name() @url.split("/").last.gsub(".git", "") end |
#to_json(*_args) ⇒ Object
60 61 62 |
# File 'lib/raykit/git/repository.rb', line 60 def to_json(*_args) JSON.generate({ "url" => @url }) end |
#to_s ⇒ Object
def self.backup
329 330 331 332 333 334 335 336 337 338 |
# File 'lib/raykit/git/repository.rb', line 329 def to_s #short_name latest_commit_id = @latest_commit_id.nil? ? "" : @latest_commit_id # latest_commit(default_branch) latest_commit_exit_status = @latest_commit_exit_status.nil? ? nil : @latest_commit_exit_status # latest_commit(default_branch) symbol = Raykit::Symbols::warning symbol = Raykit::Symbols::success if (!latest_commit_exit_status.nil? && latest_commit_exit_status == 0) symbol = Raykit::Symbols::success if (!latest_commit_exit_status.nil? && latest_commit_exit_status != 0) commit = latest_commit_id[0..8] "#{symbol} #{commit} #{short_name}" end |
#to_table ⇒ Object
def to_s
340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/raykit/git/repository.rb', line 340 def to_table header = "==Repository==" table = header table += "\n" + to_table_row("Name", short_name) table += "\n" + to_table_row("Url", @url) table += "\n" + to_table_row("Default Branch", default_branch) table += "\n" + to_table_row("Latest Commit", latest_commit(default_branch)) table += "\n" + to_table_row("Latest Commit Date", latest_commit_date(default_branch)) table += "\n" + to_table_row("Latest Commit Make", make("rake default").summary(false)) table += "\n" + to_table_row("Clone Directory", local_clone_directory) table += "\n" + to_table_row("Work Directory", get_dev_dir("work")) table += "\n" + to_table_row("Latest Tag", latest_tag(default_branch)) table end |
#to_table_row(name, value) ⇒ Object
def to_table
355 356 357 358 359 |
# File 'lib/raykit/git/repository.rb', line 355 def to_table_row(name, value) max_name_width = 20 max_value_width = 30 Rainbow(name.rjust(max_name_width, " ")).cyan + " | " + Rainbow(value).white.bold end |
#update ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/raykit/git/repository.rb', line 45 def update @latest_commit_id = latest_commit(default_branch) if (@latest_commit_id.nil? || @latest_commit_id.empty?) cmd = get_make_command(@latest_commit_id, "rake default") if (!cmd.nil?) @latest_commit_exit_status = cmd.exitstatus end end save end |
#update_local_clone_directory ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/raykit/git/repository.rb', line 177 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
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/raykit/git/repository.rb', line 223 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) update end # end end |