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

Class Method Details

.apply_remote_syncup_branchObject



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_branchObject



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_branchObject



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

.downObject



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.message
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_branchObject



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_remoteObject

Raises:



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_branchObject



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_initedObject

Raises:



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_existObject



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_existObject



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_allObject



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_downObject



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

.upObject



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.message
end