Class: Done::GitRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/done_log/git_repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_url, log_dir, log_file) ⇒ GitRepo

Returns a new instance of GitRepo.



9
10
11
12
13
# File 'lib/done_log/git_repo.rb', line 9

def initialize git_url, log_dir, log_file
  @git_url = git_url
  @log_dir = log_dir
  @log_file = log_file
end

Instance Attribute Details

#git_urlObject (readonly)

Returns the value of attribute git_url.



7
8
9
# File 'lib/done_log/git_repo.rb', line 7

def git_url
  @git_url
end

#log_dirObject (readonly)

Returns the value of attribute log_dir.



7
8
9
# File 'lib/done_log/git_repo.rb', line 7

def log_dir
  @log_dir
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



7
8
9
# File 'lib/done_log/git_repo.rb', line 7

def log_file
  @log_file
end

Instance Method Details

#addObject



15
16
17
18
19
# File 'lib/done_log/git_repo.rb', line 15

def add
  unless File.empty? log_file
    run "git add #{log_file}"
  end
end

#commitObject



21
22
23
24
25
26
27
# File 'lib/done_log/git_repo.rb', line 21

def commit
  unless run "git diff --no-patch --cached --exit-code" # Check if there are any changes
    unless run "git commit #{log_file} -m 'Update #{File.basename log_file}'"
      raise 'Could not commit update to git'
    end
  end
end

#has_remote?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/done_log/git_repo.rb', line 29

def has_remote?
  run "git remote -v | grep -q -F #{git_url.inspect}"
end

#initObject



33
34
35
36
37
38
39
40
41
# File 'lib/done_log/git_repo.rb', line 33

def init
  unless Dir.exist? File.join(log_dir, ".git")
    if git_url
      run "git clone -q #{git_url} #{log_dir}"
    else
      run "git init -q #{log_dir}"
    end
  end
end

#pullObject



43
44
45
46
47
48
49
50
# File 'lib/done_log/git_repo.rb', line 43

def pull
  return unless git_url
  return unless has_remote?

  unless run 'git pull -q'
    raise 'Could not pull from git'
  end
end

#pushObject



52
53
54
55
56
57
58
59
# File 'lib/done_log/git_repo.rb', line 52

def push
  return unless git_url
  return unless has_remote?

  unless run 'git push'
    raise 'Could not push to git'
  end
end

#run(cmd) ⇒ Object



61
62
63
64
65
# File 'lib/done_log/git_repo.rb', line 61

def run cmd
  FileUtils.cd(log_dir) do
    system cmd
  end
end