class Git::MakeMirror::App

Public Instance Methods

configure_repository_local() click to toggle source
# 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_hook() click to toggle source
# 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_repository() click to toggle source
# 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_file() click to toggle source
# 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_dir() click to toggle source
# File lib/git/make_mirror.rb, line 48
def local_hooks_dir
        @hooks_dir ||= File.join(File.dirname(__FILE__), 'hooks')
end
main() click to toggle source
# File lib/git/make_mirror.rb, line 8
def main
        opts = Trollop::options do
                version "git-make-mirror #{Git::MakeMirror::VERSION} (c) 2017 @reednj (reednj@gmail.com)"
                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) click to toggle source
# File lib/git/make_mirror.rb, line 68
def parse_remote(remote)
        a = remote.split(':')
        { :host => a[0], :path => a[1] }
end
remote() click to toggle source
# File lib/git/make_mirror.rb, line 60
def remote
        @remote ||= parse_remote(self.remote_url)
end
remote_url() click to toggle source
# File lib/git/make_mirror.rb, line 56
def remote_url
        (ARGV.last || '').strip
end
server() click to toggle source
# File lib/git/make_mirror.rb, line 64
def server
        @server ||= SSHCmd.new remote[:host]
end
sh(cmd) click to toggle source
# File lib/git/make_mirror.rb, line 43
def sh(cmd)
        puts cmd
        system cmd
end