class OhlohScm::Git::Scm

Public Class Methods

new(core:, url:, branch_name:, username:, password:) click to toggle source
Calls superclass method OhlohScm::Scm::new
# File lib/ohloh_scm/git/scm.rb, line 6
def initialize(core:, url:, branch_name:, username:, password:)
  super
  @branch_name = branch_name
end

Public Instance Methods

branch_name_or_default() click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 31
def branch_name_or_default
  branch_name || 'master'
end
checkout_files(names) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 26
def checkout_files(names)
  filenames = names.map { |name| "*#{name}" }.join(' ')
  run "cd #{url} && git checkout $(git ls-files #{filenames})"
end
pull(from, callback) click to toggle source

Example:

remote_core = OhlohScm::Factory.get_core(url: 'https://github.com/ruby/ruby')
local_core = OhlohScm::Factory.get_core(url: '/tmp/ruby-src')
local_core.scm.pull(remote_core.scm)
# File lib/ohloh_scm/git/scm.rb, line 15
def pull(from, callback)
  case from
  when Cvs::Scm then convert_to_git(from, callback)
  else clone_or_fetch(from, callback)
  end
end
vcs_path() click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 22
def vcs_path
  "#{url}/.git"
end

Private Instance Methods

check_empty_repository(commits) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 120
def check_empty_repository(commits)
  raise 'Empty repository' if !activity.read_token && commits.empty?
end
checkout(commit) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 146
def checkout(commit)
  commit.scm.checkout(commit, url)
rescue StandardError
  handle_checkout_error(commit)
end
clean_and_checkout_branch() click to toggle source

We need very high reliability and this sequence gets the job done every time. rubocop:disable Metrics/AbcSize

# File lib/ohloh_scm/git/scm.rb, line 65
def clean_and_checkout_branch
  return unless status.scm_dir_exist?

  run "cd '#{url}' && git clean -f -d -x --exclude='*.nfs*'"
  return unless status.branch?(branch_name)

  run "cd '#{url}' && git reset --hard HEAD --"
  run "cd '#{url}' && git checkout #{branch_name} --"
  run "cd '#{url}' && git reset --hard heads/#{branch_name} --"
end
clean_up_disk() click to toggle source

Deletes everything but the .git folder in the working directory.

# File lib/ohloh_scm/git/scm.rb, line 86
def clean_up_disk
  return unless Dir.exist?(url)

  run "cd #{url} && "\
        "find . -maxdepth 1 -not -name .git -not -name '*.nfs*' -not -name . -print0"\
        ' | xargs -0 rm -rf --'
end
clone_and_create_tracking_branch(remote_scm) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 54
def clone_and_create_tracking_branch(remote_scm)
  unless status.scm_dir_exist? || status.exist?
    run "rm -rf '#{url}'"
    run "git clone -q -n '#{remote_scm.url}' '#{url}'"
  end
  create_tracking_branch(remote_scm.branch_name) # ensure the correct branch exists locally
  clean_and_checkout_branch # switch to the correct branch
end
clone_or_fetch(remote_scm, callback) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 37
def clone_or_fetch(remote_scm, callback)
  callback.update(0, 1)
  if status.exist? && status.branch?(branch_name)
    clean_and_checkout_branch # must be on correct branch, but we want to be careful.
    fetch_new_commits(remote_scm)
  else
    clone_and_create_tracking_branch(remote_scm)
  end
  clean_up_disk
  callback.update(1, 1)
end
convert(commits, callback) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 113
def convert(commits, callback)
  commits.each_with_index do |r, i|
    callback.update(i, commits.size)
    create_git_commit(r, i, commits.size)
  end
end
convert_to_git(remote_scm, callback) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 94
def convert_to_git(remote_scm, callback)
  callback.update(0, 1)

  commits = remote_scm.activity.commits(after: activity.read_token)
  check_empty_repository(commits)

  if commits && !commits.empty?
    setup_dir_and_convert_commits(commits, callback)
  else
    logger.info { 'Already up-to-date.' }
  end
end
create_git_commit(commit, index, size) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 139
def create_git_commit(commit, index, size)
  logger.info { "Downloading revision #{commit.token} (#{index + 1} of #{size})... " }
  checkout(commit)
  logger.debug { "Committing revision #{commit.token} (#{index + 1} of #{size})... " }
  activity.commit_all(commit)
end
create_tracking_branch(branch_name) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/ohloh_scm/git/scm.rb, line 77
def create_tracking_branch(branch_name)
  return if branch_name.to_s.empty?
  return if activity.branches.include?(branch_name)

  run("cd '#{url}' && git remote update && "\
      "git branch -f #{branch_name} origin/#{branch_name}")
end
fetch_new_commits(remote_scm) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 49
def fetch_new_commits(remote_scm)
  run "cd '#{url}' && git fetch --tags --force --update-head-ok "\
        "'#{remote_scm.url}' #{branch_name}:#{branch_name}"
end
handle_checkout_error(commit) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 130
def handle_checkout_error(commit)
  logger.error { $ERROR_INFO.inspect }
  # If we fail to checkout, it's often because there is junk of some kind
  # in our working directory.
  logger.info { 'Checkout failed. Cleaning and trying again...' }
  clean_up_disk
  commit.scm.checkout(commit, url)
end
set_up_working_directory() click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 124
def set_up_working_directory
  # Start by making sure we are in a known good state. Set up our working directory.
  clean_up_disk
  clean_and_checkout_branch
end
setup_dir_and_convert_commits(commits, callback) click to toggle source
# File lib/ohloh_scm/git/scm.rb, line 107
def setup_dir_and_convert_commits(commits, callback)
  set_up_working_directory
  convert(commits, callback)
  callback.update(commits.size, commits.size)
end