Class: Git::MakeMirror::App

Inherits:
Object
  • Object
show all
Defined in:
lib/git/make_mirror.rb

Instance Method Summary collapse

Instance Method Details

#configure_repository_localObject



82
83
84
85
86
87
# File 'lib/git/make_mirror.rb', line 82

def configure_repository_local
	hook_file = '.git/hooks/post-receive'
	sh 'git config receive.denyCurrentBranch ignore'
	FileUtils.cp local_hook_file, hook_file
	sh "chmod 775 #{hook_file}"
end

#copy_hookObject



89
90
91
92
93
# File 'lib/git/make_mirror.rb', line 89

def copy_hook
	remote_hook_file = File.join remote[:path], '.git/hooks/post-receive'
	server.scp local_hook_file, remote_hook_file
	server.exec "chmod 775 #{remote_hook_file}"
end

#create_repositoryObject



73
74
75
76
77
78
79
80
# File 'lib/git/make_mirror.rb', line 73

def create_repository
	server.exec [
		"mkdir -p #{remote[:path]}",
		"cd #{remote[:path]}",
		'git init',
		'git config receive.denyCurrentBranch ignore'
	]
end

#local_hook_fileObject



52
53
54
# File 'lib/git/make_mirror.rb', line 52

def local_hook_file
	@local_hook_file ||= File.join(local_hooks_dir, 'post-receive.rb')
end

#local_hooks_dirObject



48
49
50
# File 'lib/git/make_mirror.rb', line 48

def local_hooks_dir
	@hooks_dir ||= File.join(File.dirname(__FILE__), 'hooks')
end

#mainObject



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
# File 'lib/git/make_mirror.rb', line 8

def main
	opts = Trollop::options do
		version "git-make-mirror #{Git::MakeMirror::VERSION} (c) 2017 @reednj ([email protected])"
		banner "Usage: git make-mirror [options] [remote]"
		opt :remote, "create a remote with the given name in the local repository", :type => :string
		opt :local, "configure a local repository to receive pushes", :default => false
		opt :push, "push to the newly created mirror"
	end
	
	Trollop::educate if (remote_url.nil? || remote_url == '') && !opts[:local]

	if opts[:local]
		configure_repository_local
		return
	end
	
	git_remote_name = nil

	puts "creating remote repository"
	self.create_repository

	puts "copying post-receive hook"
	self.copy_hook

	if !opts[:remote].nil?
		git_remote_name = opts[:remote]
		sh "git remote add #{git_remote_name} #{remote_url}"
	end

	if opts[:push]
		sh "git push #{git_remote_name || remote_url} master"
	end

end

#parse_remote(remote) ⇒ Object



68
69
70
71
# File 'lib/git/make_mirror.rb', line 68

def parse_remote(remote)
	a = remote.split(':')
	{ :host => a[0], :path => a[1] }
end

#remoteObject



60
61
62
# File 'lib/git/make_mirror.rb', line 60

def remote
	@remote ||= parse_remote(self.remote_url)
end

#remote_urlObject



56
57
58
# File 'lib/git/make_mirror.rb', line 56

def remote_url
	(ARGV.last || '').strip
end

#serverObject



64
65
66
# File 'lib/git/make_mirror.rb', line 64

def server
	@server ||= SSHCmd.new remote[:host]
end

#sh(cmd) ⇒ Object



43
44
45
46
# File 'lib/git/make_mirror.rb', line 43

def sh(cmd)
	puts cmd
	system cmd
end