Module: Gitsync
- Defined in:
- lib/gitsync.rb,
lib/gitsync/version.rb
Overview
TODO: indicate remote not only origin
Constant Summary collapse
- SYNC_BRANCH =
'git-sync-up'.freeze
- VERSION =
"0.1.1"
Class Method Summary collapse
- .apply_remote_syncup_branch ⇒ Object
- .create_syncup_branch ⇒ Object
- .delete_remote_syncup_branch ⇒ Object
- .down ⇒ Object
- .fail_msg(head, msg = nil) ⇒ Object
- .fetch_remote_syncup_branch ⇒ Object
- .prune_remote ⇒ Object
- .push_syncup_branch ⇒ Object
- .raise_if_git_not_inited ⇒ Object
- .raise_if_remote_syncup_branch_not_exist ⇒ Object
- .raise_if_syncup_branch_exist ⇒ Object
- .stash_all ⇒ Object
- .tear_down ⇒ Object
- .up ⇒ Object
Class Method Details
.apply_remote_syncup_branch ⇒ Object
114 115 116 117 118 119 120 121 |
# 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 ⇒ Object
87 88 89 90 91 92 93 94 |
# 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 ⇒ Object
123 124 125 126 127 128 129 130 |
# 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 ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# 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. end |
.fail_msg(head, msg = nil) ⇒ Object
147 148 149 |
# File 'lib/gitsync.rb', line 147 def self.fail_msg(head, msg = nil) "fatal: #{head}\n#{msg}" end |
.fetch_remote_syncup_branch ⇒ Object
105 106 107 108 109 110 111 112 |
# 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 ⇒ Object
40 41 42 43 44 45 46 |
# 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 ⇒ Object
96 97 98 99 100 101 102 103 |
# 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 ⇒ Object
33 34 35 36 37 38 |
# 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 ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# 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 ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# 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 ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# 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 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 ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# 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. end |