This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
sync_local_repository.rb
123 lines (101 loc) · 3.73 KB
/
sync_local_repository.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#--
# Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#++
module Vagrant
module Openshift
module Action
class SyncLocalRepository
include CommandHelper
def initialize(app, env, options={})
@app = app
@env = env
@options = options
end
def call(env)
env[:machine].env.ui.info("Synchronizing local sources")
repo_name = @options[:repo]
local_repo = Pathname.new(File.expand_path(File.join(env[:machine].env.root_path, "..", repo_name)))
unless local_repo.exist?
local_repo = Pathname.new(File.expand_path(File.join(env[:machine].env.root_path, repo_name)))
unless local_repo.exist?
env[:machine].ui.warn "Missing local clone of repository #{repo_name}"
end
end
Dir.chdir(local_repo) { sync_repo(env[:machine], repo_name) }
@app.call(env)
end
private
def sync_repo(machine, repo_name)
begin
temp_commit
# Get the current branch
branch = get_branch
puts "Synchronizing [#{repo_name}@#{branch}] from #{File.basename(FileUtils.pwd)}..."
ssh_user = machine.ssh_info[:username]
ssh_host = machine.ssh_info[:host]
ssh_port = machine.ssh_info[:port]
command = "export GIT_SSH=#{Constants.git_ssh};\n"
if branch == 'origin/master'
command += "git push -q ssh://#{ssh_user}@#{ssh_host}:#{ssh_port}#{Constants.build_dir + repo_name}-bare master:master --tags --force;\n"
end
command += "git push -q ssh://#{ssh_user}@#{ssh_host}:#{ssh_port}#{Constants.build_dir + repo_name}-bare #{branch}:master --tags --force"
exit_status = 1
retries = 0
while exit_status != 0
system(command)
exit_status = $?.exitstatus
exit exit_status if exit_status != 0 && retries >= 2
retries += 1
end
ensure
reset_temp_commit
end
end
def temp_commit
# Warn on uncommitted changes
%x[git diff-index --quiet HEAD]
if $?.exitstatus != 0
# Perform a temporary commit
puts "Creating temporary commit to build"
begin
%x[git commit -m "Temporary commit #1 - index changes"]
ensure
(@temp_commit ||= []).push("git reset --soft HEAD^") if $?.exitstatus == 0
end
begin
`git commit -a -m "Temporary commit #2 - non-index changes"`
ensure
(@temp_commit ||= []).push("git reset --mixed HEAD^") if $?.exitstatus == 0
end
puts @temp_commit ? "No-op" : "Done"
end
end
def reset_temp_commit
if @temp_commit
puts "Undoing temporary commit..."
while undo = @temp_commit.pop
%x[#{undo}]
end
@temp_commit = nil
puts "Done."
end
end
def get_branch
(%x[git status | head -n1].chomp =~ /.*branch (.*)/) ? $1 : 'origin/master'
end
end
end
end
end