module Git::Si::Util

Public Instance Methods

add_all_svn_files() click to toggle source
# File lib/git/si/util.rb, line 229
def add_all_svn_files
  notice_message "Adding all files present in the svn repository."
  all_svn_files = Git::Si::SvnControl.parse_file_list( get_command_output( Git::Si::SvnControl.list_file_command ) )
  raise Git::Si::GitSiError.new("No files could be found in the svn repository.") if all_svn_files.empty?
  batch_add_files_to_git( all_svn_files )
end
add_files_after_svn_update( updated_files ) click to toggle source
# File lib/git/si/util.rb, line 288
def add_files_after_svn_update( updated_files )
  notice_message "Updating mirror branch to match new data"
  # add updated files
  updated_files = Git::Si::SvnControl.parse_updated_files( updated_files )
  batch_add_files_to_git( updated_files )
end
add_files_to_git( filenames ) click to toggle source
# File lib/git/si/util.rb, line 40
def add_files_to_git( filenames )
  filenames.each do |filename|
    begin
      run_command( Git::Si::GitControl.add_command(filename) )
    rescue
      # errors here are not important enough to stop the whole process
    end
  end
end
are_there_git_changes?() click to toggle source
# File lib/git/si/util.rb, line 186
def are_there_git_changes?
  Git::Si::GitControl.are_there_changes?( get_command_output( Git::Si::GitControl.status_command ) )
end
batch_add_files_to_git( filenames ) click to toggle source
# File lib/git/si/util.rb, line 29
def batch_add_files_to_git( filenames )
  filenames.each_slice(10) do |batch|
    begin
      run_command( Git::Si::GitControl.add_command( batch ) )
    rescue
      # try to batch the files but add them individually if there is an error
      add_files_to_git( batch )
    end
  end
end
configure() click to toggle source
# File lib/git/si/util.rb, line 50
def configure
  Git::Si::SvnControl.svn_binary = options[:svn]
  Git::Si::GitControl.git_binary = options[:git]
end
create_git_repository() click to toggle source
# File lib/git/si/util.rb, line 190
def create_git_repository
  if File.exist? '.git'
    notice_message "Looks like a git repository already exists here."
    return false
  end
  notice_message "Initializing git repository"
  run_command(Git::Si::GitControl.init_command, {:allow_errors => true})
  raise Git::Si::GitError.new("Failed to initialize git repository. I'm not sure why. Check for any errors above.") unless did_last_command_succeed?
  add_all_svn_files()
  true
end
create_gitignore() click to toggle source
# File lib/git/si/util.rb, line 202
def create_gitignore
  # add externals to gitignore
  gitignore_patterns = Git::Si::GitIgnore.ignore_patterns
  gitignore_patterns += Git::Si::SvnControl.parse_external_repos( get_command_output( Git::Si::SvnControl.status_command ) )

  if not File.exist? '.gitignore'
    notice_message "Creating gitignore file."
    create_file('.gitignore', gitignore_patterns.join( "\n" ))
    run_command( Git::Si::GitControl.add_command('.gitignore') )
    return true
  end

  notice_message "Looks like a gitignore file already exists here."
  missing_patterns = Git::Si::GitIgnore.get_missing_lines_from( File.readlines( '.gitignore' ), gitignore_patterns )
  if not missing_patterns.empty?
    using_stderr do
      say "The .gitignore file is missing the following recommended patterns:\n#{missing_patterns.join( "\n" )}"
      if yes?("Do you want to add them? [Y/n] ", :green)
        append_to_file( '.gitignore', missing_patterns.join("\n") )
        run_command( Git::Si::GitControl.add_command('.gitignore') )
        return true
      end
    end
  end
  false
end
create_mirror_branch() click to toggle source
# File lib/git/si/util.rb, line 236
def create_mirror_branch
  begin
    run_command( Git::Si::GitControl.show_branch_command(get_mirror_branch) )
  rescue
    # no problem. It just means the branch does not exist.
  end
  if did_last_command_succeed?
    notice_message "Looks like the mirror branch already exists here."
  else
    notice_message "Creating mirror branch '#{get_mirror_branch}'."
    run_command( Git::Si::GitControl.create_branch_command(get_mirror_branch) )
  end
end
debug(message) click to toggle source
# File lib/git/si/util.rb, line 143
def debug(message)
  $stderr.puts message if options[:debug]
end
delete_committed_branch( local_branch ) click to toggle source
# File lib/git/si/util.rb, line 295
def delete_committed_branch( local_branch )
  run_command( Git::Si::GitControl.checkout_command( 'master' ) )
  do_rebase_action
  run_command( Git::Si::GitControl.delete_branch_command( local_branch ) )
  success_message "branch '#{local_branch}' deleted!"
end
delete_files_after_svn_update( updated_files ) click to toggle source
# File lib/git/si/util.rb, line 281
def delete_files_after_svn_update( updated_files )
  # delete deleted files.
  Git::Si::SvnControl.parse_deleted_files( updated_files ).each do |filename|
    run_command( Git::Si::GitControl.delete_command( filename ) )
  end
end
did_last_command_succeed?() click to toggle source
# File lib/git/si/util.rb, line 9
def did_last_command_succeed?
  $?.success?
end
do_revisions_differ() click to toggle source
# File lib/git/si/util.rb, line 147
def do_revisions_differ
  last_fetched_version = get_svn_revision()
  last_rebased_version = get_git_si_revision()

  if ! last_fetched_version or ! last_rebased_version
    notice_message "Could not determine last git-si version information. This may be fine if you haven't used git-si before."
    return
  end

  debug "comparing last fetched revision #{last_fetched_version} and last rebased revision #{last_rebased_version}"

  if last_fetched_version > last_rebased_version
    raise Git::Si::VersionError.new("This branch is out-of-date (svn revision #{last_rebased_version}; svn is at #{last_fetched_version}). You should do a git si rebase or git si pull.")
  elsif last_fetched_version < last_rebased_version
    return true if ask("This branch is newer (svn revision #{last_rebased_version}) than svn (rev #{last_fetched_version}). That can happen when svn changes have been made directly and may be fine. Do you want to continue? [Y/n] ", :green) =~ /\s*^n/i
  end
end
error_message(message) click to toggle source
# File lib/git/si/util.rb, line 139
def error_message(message)
  $stderr.puts set_color message, :red
end
get_command_output(command, options={}) click to toggle source
# File lib/git/si/util.rb, line 25
def get_command_output(command, options={})
  run_command(command, options.merge( capture: true ))
end
get_git_si_revision() click to toggle source

Return the most recent svn revision number stored in git

# File lib/git/si/util.rb, line 56
def get_git_si_revision
  info = get_command_output(Git::Si::GitControl.log_command('--pretty=%B'))
  return Git::Si::GitControl.parse_last_svn_revision(info)
end
get_local_branch() click to toggle source
# File lib/git/si/util.rb, line 74
def get_local_branch
  git_branches = get_command_output(Git::Si::GitControl.branch_command)
  local_branch = Git::Si::GitControl.parse_current_branch(git_branches)
  raise Git::Si::GitError.new("Could not find local branch name.") unless local_branch
  return local_branch
end
get_mirror_branch() click to toggle source
# File lib/git/si/util.rb, line 100
def get_mirror_branch
  return 'MIRRORBRANCH'
end
get_svn_revision() click to toggle source

Return the most recent svn revision number

# File lib/git/si/util.rb, line 62
def get_svn_revision
  svn_info = get_command_output(Git::Si::SvnControl.info_command)
  return Git::Si::SvnControl.parse_last_revision(svn_info)
end
get_svn_root() click to toggle source
# File lib/git/si/util.rb, line 67
def get_svn_root
  svn_info = get_command_output(Git::Si::SvnControl.info_command, {:allow_errors => true})
  root_dir = Git::Si::SvnControl.parse_root_path(svn_info)
  raise Git::Si::SvnError.new("Could not find the svn root directory.") unless root_dir
  root_dir
end
in_svn_root() { || ... } click to toggle source
# File lib/git/si/util.rb, line 81
def in_svn_root(&block)
  root_dir = get_svn_root
  notice_message "Changing directory to svn root: #{root_dir}"
  Dir.chdir(root_dir) do
    yield
  end
end
is_file_in_git?( filename ) click to toggle source
# File lib/git/si/util.rb, line 268
def is_file_in_git?( filename )
  not get_command_output( Git::Si::GitControl.list_file_command( filename ) ).empty?
end
notice_message(message) click to toggle source
# File lib/git/si/util.rb, line 135
def notice_message(message)
  $stderr.puts set_color message, :yellow unless options[:quiet]
end
on_local_branch() { || ... } click to toggle source
# File lib/git/si/util.rb, line 89
def on_local_branch(&block)
  begin
    in_svn_root do
      yield
    end
  rescue Git::Si::GitSiError => err
    error_message err
    exit false
  end
end
on_mirror_branch() { || ... } click to toggle source
# File lib/git/si/util.rb, line 104
def on_mirror_branch(&block)
  local_branch = get_local_branch()
  run_command( Git::Si::GitControl.checkout_command(get_mirror_branch) )
  begin
    in_svn_root do
      yield
    end
  rescue Git::Si::GitSiError => err
    error_message err
    exit false
  ensure
    run_command( Git::Si::GitControl.checkout_command(local_branch) )
  end
end
print_colordiff(diff) click to toggle source
revert_files_to_svn_update( updated_files ) click to toggle source
# File lib/git/si/util.rb, line 272
def revert_files_to_svn_update( updated_files )
  notice_message "Reverting any local changes in mirror branch"
  # revert everything, but sometimes that doesn't work, so revert conflicts too.
  run_command( Git::Si::SvnControl.revert_command )
  Git::Si::SvnControl.parse_conflicted_files( updated_files ).each do |filename|
    run_command( Git::Si::SvnControl.revert_command( filename ) )
  end
end
run_command(command, options={}) click to toggle source
# File lib/git/si/util.rb, line 13
def run_command(command, options={})
  output = ''
  debug "run_command `#{command}`, options: #{options}"
  if STDOUT.tty? and not @silent
    output = run(command, options)
  else
    output = run(command, options.update(verbose: false, capture: true))
  end
  raise Git::Si::ShellError.new("There was an error while trying to run the command: #{command}. Look above for any errors.") if not options[:allow_errors] and not did_last_command_succeed?
  return output
end
stash_local_changes() click to toggle source
# File lib/git/si/util.rb, line 250
def stash_local_changes
  on_local_branch do
    if are_there_git_changes?
      notice_message "Preserving uncommitted changed files"
      run_command( Git::Si::GitControl.stash_command )
      return true
    end
  end
  false
end
success_message(message) click to toggle source
# File lib/git/si/util.rb, line 131
def success_message(message)
  $stderr.puts set_color message, :green
end
unstash_local_changes( did_stash_changes ) click to toggle source
# File lib/git/si/util.rb, line 261
def unstash_local_changes( did_stash_changes )
  if did_stash_changes
    notice_message "Restoring uncommitted changed files"
    run_command( Git::Si::GitControl.unstash_command )
  end
end
using_stderr() { || ... } click to toggle source
# File lib/git/si/util.rb, line 119
def using_stderr(&block)
  old_stdout = $stdout
  $stdout = $stderr
  @silent = true
  begin
    yield
  ensure
    $stdout = old_stdout
    @silent = false
  end
end