Class: Raykit::Git::Directory

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

Overview

Functionality to manage a local clone of a git repository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Directory

Returns a new instance of Directory.



12
13
14
# File 'lib/raykit/git/directory.rb', line 12

def initialize(directory)
  @directory = directory
end

Instance Attribute Details

#directoryObject

The directory name of the local repository clone



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

def directory
  @directory
end

Instance Method Details

#branchObject



209
210
211
212
213
214
215
# File 'lib/raykit/git/directory.rb', line 209

def branch
  Dir.chdir(directory) do
    scan = `git branch`.scan(/\*\s([\w.-]+)/)
    return scan[0][0].to_s if !scan.nil? && scan.length.positive? && scan[0].length.positive?
  end
  "master"
end

#detached?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/raykit/git/directory.rb', line 27

def detached?
  Dir.chdir(directory) do
    status = `git status`.strip
    if status.include?("detached")
      true
    else
      false
    end
  end
end

#get_sha(filename) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/raykit/git/directory.rb', line 185

def get_sha(filename)
  if File.exist?(filename)
    Digest::SHA256.file(filename).hexdigest.to_s
  else
    "#{filename} not found"
  end
  # ''
end

#get_tag_commit_id(name) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/raykit/git/directory.rb', line 167

def get_tag_commit_id(name)
  cmd = Raykit::Command.new("git rev-parse \"#{name}\"").run
  if !cmd.output.include?("fatal") && !cmd.error.include?("fatal")
    cmd.output.strip
  else
    ""
  end
end

#get_unique_ruby_constantsObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/raykit/git/directory.rb', line 244

def get_unique_ruby_constants
  # Define the regular expression pattern
  pattern = /\b([A-Z][A-Z0-9_]*)\b\s*=/

  constants = []

  # Loop through each .rb file in the specified directory
  Dir.glob("#{directory}/**/*.rb").each do |file|
    # Read the contents of the file
    content = File.read(file)

    # Scan the content for constants and add the matches to the array.
    content.scan(pattern) do |match|
      # Add matched constant name to the constants array.
      constants << match[0]
    end
    # Scan the content for constants and add the matches to the array
    #constants.concat(content.scan(pattern))
  end

  # Return unique constants
  constants.uniq
end

#has_tag(name) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/raykit/git/directory.rb', line 176

def has_tag(name)
  cmd = Raykit::Command.new("git rev-parse \"#{name}\"").run
  if !cmd.output.include?("fatal") && !cmd.error.include?("fatal")
    true
  else
    false
  end
end

#last_modified_artifact_timeObject



155
156
157
# File 'lib/raykit/git/directory.rb', line 155

def last_modified_artifact_time
  self.last_modified_time
end

#last_modified_filenameObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/raykit/git/directory.rb', line 96

def last_modified_filename
  Dir.chdir(@directory) do
    # TODO: consider handling the case where glob can fail because of filename length
    #       Errno::ENAMETOOLONG: File name too long @ dir_s_glob - /Users/username/...
    #
    begin
      # Use Dir.glob to find all files, select the actual files, find the one with the latest modification time, and return its name
      files = Dir.glob("**/*.*").select { |f| File.file?(f) }

      # Find the file with the latest modification time
      latest_file = files.max_by { |f| File.mtime(f) }

      return latest_file

      #Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }
    rescue Errno::ENAMETOOLONG => e
      puts "Error: #{e.message}"
      # return the first src file found
      src_files.each { |f| return f if File.file?(f) }
      #return handle_long_filename_error
    end
  end
end

#last_modified_src_fileObject

def last_modified_src_time

#File.mtim(src_files.max_by { |f| File.mtime(f) }#
Dir.chdir(@directory) do
  File.mtime(self.src_files.select { |f| File.file?(f) }.max_by { |f| File.mtime(f) })
end

end



146
147
148
149
150
151
152
153
# File 'lib/raykit/git/directory.rb', line 146

def last_modified_src_file
  Dir.chdir(@directory) do
    # Select the source files that are actual files, not directories
    src_files.select { |f| File.file?(f) }
    # Find the max by modification time
             .max_by { |f| File.mtime(f) }
  end
end

#last_modified_src_timeObject



132
133
134
135
136
137
# File 'lib/raykit/git/directory.rb', line 132

def last_modified_src_time
  Dir.chdir(@directory) do
    last_file = self.src_files.select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }
    last_file ? File.mtime(last_file) : Time.at(0) # Return a default time if no file is found
  end
end

#last_modified_timeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/raykit/git/directory.rb', line 76

def last_modified_time
  Dir.chdir(@directory) do

    # Find the most recently modified file
    most_recent_file = Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }

    # Use File.mtime if a file was found, otherwise handle the nil case
    if most_recent_file
      last_modified_time = File.mtime(most_recent_file)
    else
      # Handle the case where no files are found. This could be setting a default value,
      # raising a custom error, or any other appropriate action depending on your needs.
      # For example, setting last_modified_time to Time.now or raising a custom error.
      last_modified_time = Time.now # Or any other appropriate action
    end

    #File.mtime(Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) })
  end
end

#latest_tag(_branch) ⇒ Object

The latest tag for a branch of the repository



160
161
162
163
164
165
# File 'lib/raykit/git/directory.rb', line 160

def latest_tag(_branch)
  Dir.chdir(directory) do
    return `git describe --abbrev=0`.strip
  end
  ""
end

#outstanding_commit?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'lib/raykit/git/directory.rb', line 16

def outstanding_commit?
  Dir.chdir(directory) do
    return false if !File.exist?(".git")
    if user_can_commit
      return !`git status`.include?("nothing to commit,")
    else
      return false
    end
  end
end

#pullObject



38
39
40
41
42
43
44
# File 'lib/raykit/git/directory.rb', line 38

def pull
  Dir.chdir(directory) do
    diff = `git diff`.strip
    status = `git status`.strip
    PROJECT.run("git pull", false) if diff.length.zero? && status.include?("nothing to commit")
  end
end

#rake(task) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/raykit/git/directory.rb', line 46

def rake(task)
  unless Dir.exist?(@directory)
    puts "directory not found."
    return 1
  end
  debug = false
  sub_dir = "work"
  sub_dir = "make" if @directory.include?("/make/")
  rake_log = repository.get_dev_dir("log") + "/#{sub_dir}.rake.#{task}.json"

  puts "log filename #{rake_log}" if debug
  if File.exist?(rake_log) && (File.mtime(rake_log) > last_modified_time)
    puts "using logged data" if debug
    cmd = Raykit::Command.parse(File.read(rake_log))
    cmd.summary
    return
  end

  Dir.chdir(@directory) do
    if File.exist?("rakefile.rb")
      cmd = Raykit::Command.new("rake #{task}")
      cmd.summary
      FileUtils.mkdir_p(File.dirname(rake_log))
      File.open(rake_log, "w") { |f| f.write(JSON.generate(cmd.to_hash)) }
    else
      puts "rakefile.rb not found"
    end
  end
end

#remoteObject



222
223
224
225
226
227
228
229
# File 'lib/raykit/git/directory.rb', line 222

def remote
  if Dir.exist?(directory)
    Dir.chdir(directory) do
      return Command.new("git config --get remote.origin.url").run.output.strip if Dir.exist?(".git")
    end
  end
  ""
end

#repositoryObject



217
218
219
220
# File 'lib/raykit/git/directory.rb', line 217

def repository
  @repository = Raykit::Git::Repository.new(remote) if @repository.nil?
  @repository
end

#src_filesObject

git ls-tree -r master –name-only



121
122
123
124
125
126
127
128
129
130
# File 'lib/raykit/git/directory.rb', line 121

def src_files
  files = Array.new
  Dir.chdir(@directory) do
    `git ls-tree -r #{branch} --name-only`.each_line { |line|
      file = line.strip
      files << file if file.length > 0 && File.exist?(file)
    }
  end
  files
end

#tag_version(version) ⇒ Object



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

def tag_version(version)
  if has_tag "v#{version}"
    puts "  git tag v#{version} already exists"
  else
    if outstanding_commit?
      raise "outstanding commit, will not tag"
    else
      puts "  git tag v#{version} does not exist"
      run("git tag -a v#{version} -m\"version #{version}\"")
    end
  end
end

#user_can_commitObject



202
203
204
205
206
207
# File 'lib/raykit/git/directory.rb', line 202

def user_can_commit
  return false if user_name.length.zero?
  return false if user_email.length.zero?

  true
end

#user_emailObject



198
199
200
# File 'lib/raykit/git/directory.rb', line 198

def user_email
  `git config --get user.email`.strip
end

#user_nameObject



194
195
196
# File 'lib/raykit/git/directory.rb', line 194

def user_name
  `git config --get user.name`.strip
end