module Codingapi::Githelper

Public Instance Methods

branch_exists?(local_repo_dir: nil, branch: nil) click to toggle source
# File lib/codingapi/base/githelper.rb, line 12
def branch_exists?(local_repo_dir: nil, branch: nil)
    current=Dir.pwd
    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)
      result = git!(%W(-C #{local_repo_dir} --no-pager branch --list origin/#{branch} --no-color -r))
      return !result.empty?
    else
      return false
    end
    Dir.chdir(current)
end
checkout_branch(local_repo_dir: nil, branch: nil) click to toggle source
# File lib/codingapi/base/githelper.rb, line 24
  def checkout_branch(local_repo_dir: nil, branch: nil)
    current=Dir.pwd
    if File.exist?(local_repo_dir)
      if branch_exists?(local_repo_dir:local_repo_dir, branch:branch)
        puts "#{branch} is exist !!!"
        git!(%W(-C #{local_repo_dir} checkout #{branch}))
        git!(%W(-C #{local_repo_dir} pull))
        return true
      else
        puts "#{branch} is not exist !!!"
      end
    else
      puts "dir is not exist !!!"
    end
    return false
end
getbaregit_to_dir(repo_name:nil, remote_url:nil, path: nil) click to toggle source
# File lib/codingapi/base/githelper.rb, line 41
def getbaregit_to_dir(repo_name:nil, remote_url:nil, path: nil)
  current=Dir.pwd
  puts repo_name
  puts remote_url
  puts path

  if !repo_name.empty? && !remote_url.empty? && !path.empty?
    destdir = File::join(path, repo_name)

    if File.exist?(destdir)
      FileUtils.rm_rf(destdir)
    end
    puts "Cloning `#{remote_url}` bare into `#{destdir}`."
    git! ['clone', '--bare', remote_url, destdir]
  end
  Dir.chdir(current)
  return destdir
end
getcode_to_dir(repo_name:nil, remote_url:nil, path: nil, new_branch:"master") click to toggle source
# File lib/codingapi/base/githelper.rb, line 60
def getcode_to_dir(repo_name:nil, remote_url:nil, path: nil, new_branch:"master")

    current=Dir.pwd
    puts repo_name
    puts remote_url
    puts path
    if !repo_name.empty? && !remote_url.empty? && !path.empty?
      destdir = File::join(path, repo_name)

      if File.exist?(destdir)
        Dir.chdir(destdir)
        args = %W(-C #{destdir} fetch origin)
        args.push('--progress')
        git!(args)
        current_branch = git!(%W(-C #{destdir} rev-parse --abbrev-ref HEAD)).strip
        git!(%W(-C #{destdir} reset --hard origin/#{current_branch}))
        git!(%W(-C #{destdir} pull))
        
        if branch_exists?(local_repo_dir:destdir, branch:new_branch)
          puts "#{new_branch} is exist !!!"
          git!(%W(-C #{destdir} checkout #{new_branch}))
          git!(%W(-C #{destdir} pull))
          # commands << "git checkout #{self.branch.shellescape}"
        else
          puts "#{new_branch} is not exist !!!"
        end

      else
        puts "Cloning `#{remote_url}` into `#{destdir}`."
        git! ['clone', remote_url, destdir]

        if branch_exists?(local_repo_dir:destdir, branch:new_branch)
          puts "#{new_branch} is exist !!!"
          git!(%W(-C #{destdir} checkout #{new_branch}))
          # commands << "git checkout #{self.branch.shellescape}"
        else
          puts "#{new_branch} is not exist !!!"
        end

      end
    end
    Dir.chdir(current)
    return destdir
end
git_addpush_repo(path:nil, message:"res") click to toggle source
# File lib/codingapi/base/githelper.rb, line 112
def git_addpush_repo(path:nil, message:"res")           
  current=Dir.pwd
  Dir.chdir(path)
  files_list = git! ['status', '--porcelain']
  if !files_list.empty?
      git! ['add', '-A']
      git! ['commit', '-m ' + "#{message}"]
      git! ['push']
  else
      puts "Nothing to commit !!!"
  end
  Dir.chdir(current)
end
git_pushmirror_repo(path:nil, new_remote_url:nil) click to toggle source
# File lib/codingapi/base/githelper.rb, line 105
def git_pushmirror_repo(path:nil, new_remote_url:nil)           
  current=Dir.pwd
  Dir.chdir(path)
  git! ['push', '--mirror', new_remote_url]
  Dir.chdir(current)
end