Module: Docman::GitUtil

Defined in:
lib/docman/git_util.rb

Class Method Summary collapse

Class Method Details

.clone_repo(repo, path, type, version, single_branch = nil, depth = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/docman/git_util.rb', line 48

def self.clone_repo(repo, path, type, version, single_branch = nil, depth = nil)
  if type == 'branch'
    single_branch = single_branch ? "-b #{version} --single-branch" : ''
    depth = (depth and depth.is_a? Integer) ? "--depth #{depth}" : ''
  else
    single_branch=''
    depth=''
  end
  exec("clone #{single_branch} #{depth} #{repo} #{path}")
end

.commit(root_path, path, message, tag = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/docman/git_util.rb', line 73

def self.commit(root_path, path, message, tag = nil)
  if repo_changed? path
    # puts message
    pull root_path
    exec %Q(add --all #{path.slice "#{root_path}/"})
    exec %Q(commit -m "#{message}") if repo_changed? path
    self.tag(root_path, tag) if tag
  end
end

.exec(command, show_result = true) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/docman/git_util.rb', line 10

def self.exec(command, show_result = true)
  @logger.info "#{@git} #{command}"
  result = `#{@git} #{command}`.delete!("\n")
  @logger.info result if show_result and result
  raise "ERROR: #{result}" unless $?.exitstatus == 0
  result
end

.get(repo, path, type, version, single_branch = nil, depth = nil, reset = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/docman/git_util.rb', line 24

def self.get(repo, path, type, version, single_branch = nil, depth = nil, reset = false)
  FileUtils.rm_rf path if reset and File.directory? path
  if File.directory? path and File.directory?(File.join(path, '.git'))
    Dir.chdir path
    self.reset_repo(path) #if self.repo_changed?(path)
    if type == 'branch'
      exec "fetch"
      exec "checkout #{version}"
      exec "pull origin #{version}"
    end
    if type == 'tag'
      exec 'fetch --tags'
      exec "checkout tags/#{version}"
    end
  else
    FileUtils.rm_rf path if File.directory? path
    clone_repo(repo, path, type, version, single_branch, depth)
    Dir.chdir path
    exec "checkout #{version}"
  end
  result = type == 'branch' ? self.last_revision(path) : version
  result
end

.last_commit_hash(path, branch) ⇒ Object



96
97
98
99
100
# File 'lib/docman/git_util.rb', line 96

def self.last_commit_hash(path, branch)
  Dir.chdir path
  result = `git rev-parse --short origin/#{branch}`
  result.delete!("\n")
end

.last_revision(path = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/docman/git_util.rb', line 59

def self.last_revision(path = nil)
  result = nil
  if self.repo? path
    Dir.chdir path unless path.nil?
    result = `git rev-parse --short HEAD`
    result.delete!("\n")
  end
  result
end

.pull(path) ⇒ Object



83
84
85
86
# File 'lib/docman/git_util.rb', line 83

def self.pull(path)
  Dir.chdir path
  exec 'pull'
end

.push(root_path, version) ⇒ Object



102
103
104
105
106
# File 'lib/docman/git_util.rb', line 102

def self.push(root_path, version)
  Dir.chdir root_path
  exec "pull origin #{version}"
  exec "push origin #{version}"
end

.repo?(path) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/docman/git_util.rb', line 88

def self.repo?(path)
  File.directory? File.join(path, '.git')
end

.repo_changed?(path) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/docman/git_util.rb', line 92

def self.repo_changed?(path)
  not Exec.do "#{Application::bin}/dm_repo_clean.sh #{path}"
end

.reset_repo(path) ⇒ Object



18
19
20
21
22
# File 'lib/docman/git_util.rb', line 18

def self.reset_repo(path)
  Dir.chdir path
  exec 'reset --hard'
  exec 'clean -f -d'
end

.tag(root_path, tag) ⇒ Object



108
109
110
111
112
# File 'lib/docman/git_util.rb', line 108

def self.tag(root_path, tag)
  Dir.chdir root_path
  exec %Q(tag -a -m "Tagged to #{tag}" "#{tag}")
  exec "push origin #{tag}"
end

.update(path) ⇒ Object



69
70
71
# File 'lib/docman/git_util.rb', line 69

def self.update(path)
  pull path
end