Module: Docman::GitUtil

Defined in:
lib/docman/git_util.rb

Class Method Summary collapse

Class Method Details

.branchObject



114
115
116
# File 'lib/docman/git_util.rb', line 114

def self.branch()
  exec "rev-parse --abbrev-ref HEAD", false
end

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



64
65
66
67
68
69
70
71
72
73
# File 'lib/docman/git_util.rb', line 64

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/docman/git_util.rb', line 91

def self.commit(root_path, path, message, tag = nil)
  if repo_changed? path
    # pull root_path
    Dir.chdir root_path
    exec %Q(add --all #{path.slice "#{root_path}/"})
    if repo_changed? path
      if Application::instance.options.has_key?('debug')
        exec %Q(commit --allow-empty --no-verify -m "#{message}")
      else
        exec %Q(commit --allow-empty --quiet --no-verify -m "#{message}")
      end
    end

    self.tag(root_path, tag) if tag
    Docman::Application.instance.commit_count = Docman::Application.instance.commit_count + 1
  end
end

.exec(command, show_result = true) ⇒ Object



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

def self.exec(command, show_result = true)
  @logger.info "#{@git} #{command} in #{Dir.pwd}"
  result = `#{@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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docman/git_util.rb', line 40

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)
    exec 'fetch --tags'
    if type == 'branch'
      #exec "fetch"
      exec "checkout #{version}"
      exec "pull origin #{version}"
    end
    if type == 'tag'
      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



126
127
128
129
130
# File 'lib/docman/git_util.rb', line 126

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, branch = 'HEAD') ⇒ Object



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

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

.pull(path, options = nil) ⇒ Object



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

def self.pull(path, options = nil)
  Dir.chdir path
  exec "pull#{options}"
end

.push(root_path, version, no_pull = false) ⇒ Object



132
133
134
135
136
# File 'lib/docman/git_util.rb', line 132

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

.repo?(path) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/docman/git_util.rb', line 118

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

.repo_changed?(path) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/docman/git_util.rb', line 122

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

.reset_repo(path) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/docman/git_util.rb', line 30

def self.reset_repo(path)
  Dir.chdir path
  exec 'reset --hard'
  if Application::instance.options.has_key?('debug')
    exec 'clean -f -d -x --dry-run'
    exec 'status'
  end
  exec 'clean -f -d -x'
end

.squash_commits(commit_count, message = nil, tag = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/docman/git_util.rb', line 19

def self.squash_commits(commit_count, message = nil, tag = nil)
  message = "$(git log --format=%B --reverse HEAD..HEAD@{1})" unless message
  exec "reset --soft HEAD~#{commit_count}"
  if Application::instance.options.has_key?('debug')
    exec "commit --no-verify -m \"#{message}\""
  else
    exec "commit --quiet --no-verify -m \"#{message}\""
  end
  self.tag(Dir.pwd, tag) if tag
end

.tag(root_path, tag) ⇒ Object



138
139
140
141
142
# File 'lib/docman/git_util.rb', line 138

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, options) ⇒ Object



85
86
87
88
89
# File 'lib/docman/git_util.rb', line 85

def self.update(path, options)
  @logger.info "Update #{path} #{options}"
  Dir.chdir path
  exec("pull #{options}")
end