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.



11
12
13
14
15
# File 'lib/raykit/git/repository.rb', line 11

def initialize(url)
  @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.



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

def clone_directory
  @clone_directory
end

#urlObject

The url of the remote repository



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

def url
  @url
end

#work_directoryObject

Returns the value of attribute work_directory.



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

def work_directory
  @work_directory
end

Class Method Details

.make_url(url, commit_id, command) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/raykit/git/repository.rb', line 213

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



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

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

.work_integrate(url) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/raykit/git/repository.rb', line 190

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



181
182
183
184
185
186
187
188
# File 'lib/raykit/git/repository.rb', line 181

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



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/raykit/git/repository.rb', line 63

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/raykit/git/repository.rb', line 127

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



41
42
43
44
45
46
47
# File 'lib/raykit/git/repository.rb', line 41

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/raykit/git/repository.rb', line 50

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



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

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



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

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



91
92
93
94
95
# File 'lib/raykit/git/repository.rb', line 91

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

  ""
end

#make(command, force = false) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/raykit/git/repository.rb', line 167

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



142
143
144
145
# File 'lib/raykit/git/repository.rb', line 142

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



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

def relative_path
  @url.gsub("https://", "").gsub(".git", "").gsub("http://", "")
end

#short_nameObject



17
18
19
# File 'lib/raykit/git/repository.rb', line 17

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

#to_json(*_args) ⇒ Object



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

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

#update_local_clone_directoryObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/raykit/git/repository.rb', line 101

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/raykit/git/repository.rb', line 147

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