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



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



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

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



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

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

#get_sha(filename) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/raykit/git/directory.rb', line 107

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



89
90
91
92
93
94
95
96
# File 'lib/raykit/git/directory.rb', line 89

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

#has_tag(name) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/raykit/git/directory.rb', line 98

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_timeObject



75
76
77
78
79
# File 'lib/raykit/git/directory.rb', line 75

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

#latest_tag(_branch) ⇒ Object

The latest tag for a branch of the repository



82
83
84
85
86
87
# File 'lib/raykit/git/directory.rb', line 82

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

#outstanding_commit?Boolean



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

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

#pullObject



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

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



45
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
# File 'lib/raykit/git/directory.rb', line 45

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



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

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



139
140
141
142
# File 'lib/raykit/git/directory.rb', line 139

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

#user_can_commitObject



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

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

  true
end

#user_emailObject



120
121
122
# File 'lib/raykit/git/directory.rb', line 120

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

#user_nameObject



116
117
118
# File 'lib/raykit/git/directory.rb', line 116

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