class Raykit::Git::Directory

Functionality to manage a local clone of a git repository

def branch

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

def detached?

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

def get_sha(filename)

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

end

def get_tag_commit_id(name)

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

def has_tag(name)

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

def initialize(directory)

def initialize(directory)
  @directory = directory
end

def last_modified_src_time

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

def last_modified_time

def last_modified_time
  Dir.chdir(@directory) do
    File.mtime(Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) })
  end
end

def latest_tag(_branch)

The latest tag for a branch of the repository
def latest_tag(_branch)
  Dir.chdir(directory) do
    return `git describe --abbrev=0`.strip
  end
  ""
end

def outstanding_commit?

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

def pull

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

def rake(task)

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

def remote

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

def repository

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

def src_files

git ls-tree -r master --name-only
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
    }
  end
  files
end

def tag_version(version)

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

def user_can_commit

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

def user_email

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

def user_name

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