lib/docman/git_util.rb in docman-0.0.5 vs lib/docman/git_util.rb in docman-0.0.6

- old
+ new

@@ -1,46 +1,74 @@ +require 'logger' + module Docman - class GitUtil + module GitUtil + @logger = Logger.new(STDOUT) + + def self.exec(command) + @logger.info command + @logger.info `#{command}`.delete!("\n") + end + def self.get(repo, path, type, version) - if File.directory? path + if File.directory? path and File.directory?(File.join(path, '.git')) Dir.chdir path - `git checkout #{version} && git pull origin #{version}` if type == 'branch' + if type == 'branch' + exec "git checkout #{version}" + exec "git pull origin #{version}" + end if type == 'tag' - `git fetch --tags` - `git checkout "tags/#{version}"` + exec "git fetch --tags" + exec "git checkout tags/#{version}" end else - `git clone #{repo} #{path}` + FileUtils.rm_rf path if File.directory? path + exec "git clone #{repo} #{path}" Dir.chdir path - `git checkout #{version}` + exec "git checkout #{version}" end result = `git rev-parse --short HEAD` + @logger.info "Commit hash: #{result}" result.delete!("\n") end def self.update(path) - `cd #{path} && git pull` + pull path end def self.commit(root_path, path, message) - if self.repo_changed? path - puts message - Dir.chdir root_path - path.slice! "#{root_path}/" - `git pull` - `git add --all #{path}` - `git commit -m "#{message}"` + if repo_changed? path + # puts message + pull root_path + exec %Q(git add --all #{path.slice "#{root_path}/"}) + exec %Q(git commit -m "#{message}") end end + def self.pull(path) + Dir.chdir path + exec 'git pull' + end + + def self.repo?(path) + File.directory? File.join(path, '.git') + end + def self.repo_changed?(path) not Exec.do "#{Application::bin}/dm_repo_clean.sh #{path}" end + def self.last_commit_hash(path, branch) + Dir.chdir path + result = `git rev-parse --short origin/#{branch}` + result.delete!("\n") + end + def self.push(root_path, version) Dir.chdir root_path + `git pull origin #{version}` `git push origin #{version}` end end end \ No newline at end of file