module Gitsync

TODO: indicate remote not only origin

Constants

SYNC_BRANCH
VERSION

Public Class Methods

apply_remote_syncup_branch() click to toggle source
# File lib/gitsync.rb, line 114
def self.apply_remote_syncup_branch
  puts 'try apply remote sync-up branch...'
  result = CommandTool.exccmd "git stash apply origin/#{SYNC_BRANCH} --index"
  unless result[:succ]
    raise GitsyncError, fail_msg('apply syncup branch failed', result[:msg])
  end
  puts 'apply success!'
end
create_syncup_branch() click to toggle source
# File lib/gitsync.rb, line 87
def self.create_syncup_branch
  puts 'try create sync-up branch...'
  result = CommandTool.exccmd "git branch #{SYNC_BRANCH} stash@{0}"
  unless result[:succ]
    raise GitsyncError, fail_msg('create syncup branch failed', result[:msg])
  end
  puts "create branch '#{SYNC_BRANCH}' success!"
end
delete_remote_syncup_branch() click to toggle source
# File lib/gitsync.rb, line 123
def self.delete_remote_syncup_branch
  puts 'try delete remote sync-up branch...'
  result = CommandTool.exccmd "git push origin --delete #{SYNC_BRANCH}"
  unless result[:succ]
    raise GitsyncError, fail_msg('delete syncup branch failed', result[:msg])
  end
  puts 'delete success!'
end
down() click to toggle source
# File lib/gitsync.rb, line 21
def self.down
  raise_if_git_not_inited
  prune_remote
  raise_if_remote_syncup_branch_not_exist
  fetch_remote_syncup_branch
  apply_remote_syncup_branch
  delete_remote_syncup_branch
  prune_remote
rescue GitsyncError => e
  puts e.message
end
fail_msg(head, msg = nil) click to toggle source
# File lib/gitsync.rb, line 154
def self.fail_msg(head, msg = nil)
  "fatal: #{head}\n#{msg}"
end
fetch_remote_syncup_branch() click to toggle source
# File lib/gitsync.rb, line 105
def self.fetch_remote_syncup_branch
  puts 'try fetch remote sync-up branch...'
  result = CommandTool.exccmd "git fetch origin #{SYNC_BRANCH}"
  unless result[:succ]
    raise GitsyncError, fail_msg('fetch syncup branch failed', result[:msg])
  end
  puts 'fetch success!'
end
prune_remote() click to toggle source
# File lib/gitsync.rb, line 40
def self.prune_remote
  puts 'try prune remote'
  result = CommandTool.exccmd 'git remote prune origin'
  raise GitsyncError, fail_msg('git remote prune failed', result[:msg]) unless
      result[:succ]
  puts 'remote prune success!'
end
push_syncup_branch() click to toggle source
# File lib/gitsync.rb, line 96
def self.push_syncup_branch
  puts 'try push sync-up branch...'
  result = CommandTool.exccmd "git push origin #{SYNC_BRANCH}:#{SYNC_BRANCH}"
  unless result[:succ]
    raise GitsyncError, fail_msg('push syncup branch failed', result[:msg])
  end
  puts "push branch '#{SYNC_BRANCH}' success!"
end
raise_if_git_not_inited() click to toggle source
# File lib/gitsync.rb, line 33
def self.raise_if_git_not_inited
  puts 'check git repository exist...'
  result = CommandTool.exccmd('git branch')
  raise GitsyncError, fail_msg('git is not inited') unless result[:succ]
  puts 'exist!'
end
raise_if_remote_syncup_branch_not_exist() click to toggle source
# File lib/gitsync.rb, line 65
def self.raise_if_remote_syncup_branch_not_exist
  puts 'check sync-up branch exist in remote...'
  result = CommandTool.exccmd('git ls-remote --heads --exit-code ' \
                                  "origin #{SYNC_BRANCH}")
  unless result[:succ]
    raise GitsyncError, fail_msg('sync-up branch is not exist in remote',
                                 result[:msg])
  end
  puts 'found!'
end
raise_if_syncup_branch_exist() click to toggle source
# File lib/gitsync.rb, line 48
def self.raise_if_syncup_branch_exist
  puts 'check is sync-up branch already exist...'
  result = CommandTool.exccmd('git ls-remote --heads --exit-code ' \
                                  "origin #{SYNC_BRANCH}")
  if result[:succ]
    raise GitsyncError, fail_msg('syncup branch is already exist in remote',
                                 result[:msg])
  end

  result = CommandTool.exccmd("git branch | grep -w #{SYNC_BRANCH}")
  if result[:succ]
    raise GitsyncError, fail_msg('syncup branch is already exist in local',
                                 result[:msg])
  end
  puts 'not exist!'
end
stash_all() click to toggle source
# File lib/gitsync.rb, line 76
def self.stash_all
  puts 'try create temporary stash...'
  result = CommandTool.exccmd 'git stash --include-untracked'
  if !result[:succ]
    raise GitsyncError, fail_msg('stash failed', result[:msg])
  elsif result[:msg].include? 'No local changes to save'
    raise GitsyncError, fail_msg('no local changes to sync up')
  end
  puts 'stashing success!'
end
tear_down() click to toggle source
# File lib/gitsync.rb, line 132
def self.tear_down
  puts 'try get rid of local sync-up branch...'
  result = CommandTool.exccmd "git branch -D #{SYNC_BRANCH}"
  unless result[:succ]
    raise GitsyncError, fail_msg('delete syncup branch failed', result[:msg])
  end
  puts "delete branch '#{SYNC_BRANCH}' success!"
  puts 'try hide remote sync-up branch from local repository...'
  result = CommandTool.exccmd 'git branch --remotes ' \
                              "--delete origin/#{SYNC_BRANCH}"
  unless result[:succ]
    raise GitsyncError, fail_msg('hide syncup branch failed', result[:msg])
  end
  puts "hide branch '#{SYNC_BRANCH}' from local repository success!"
  puts 'try get rid of temporary stash...'
  result = CommandTool.exccmd 'git stash drop stash@{0}'
  unless result[:succ]
    raise GitsyncError, fail_msg('drop stash failed', result[:msg])
  end
  puts 'drop temporary stash success!'
end
up() click to toggle source
# File lib/gitsync.rb, line 9
def self.up
  raise_if_git_not_inited
  prune_remote
  raise_if_syncup_branch_exist
  stash_all
  create_syncup_branch
  push_syncup_branch
  tear_down
rescue GitsyncError => e
  puts e.message
end