Sha256: d2401cfaaf51788623e1c10214d336735f4bf5dcd99d9f5bf58959ec40314fab

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'logger'

module Docman

  module GitUtil

    @logger = Logger.new(STDOUT)

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

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

    def self.get(repo, path, type, version)
      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 "git checkout #{version}"
          exec "git pull origin #{version}"
        end
        if type == 'tag'
          exec 'git fetch --tags'
          exec "git checkout tags/#{version}"
        end
      else
        FileUtils.rm_rf path if File.directory? path
        exec "git clone #{repo} #{path}"
        Dir.chdir path
        exec "git checkout #{version}"
      end
      result = `git rev-parse --short HEAD`
      @logger.info "Commit hash: #{result}"
      result.delete!("\n")
    end

    def self.update(path)
      pull path
    end

    def self.commit(root_path, path, 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docman-0.0.13 lib/docman/git_util.rb