cloning.rb revision 5f7e70527f0873b2c50827d17b65c52088c70266
3946N/Arequire 'subprocess'
3946N/A
3946N/Amodule GitRepository::Cloning
3946N/A extend ActiveSupport::Concern
3946N/A
3946N/A # Struct for old and new OID
3946N/A Ref = Struct.new(:previous, :current)
3946N/A
3946N/A def clone(url)
3946N/A set_section %w( remote origin ),
3946N/A url: url,
3946N/A fetch: '+refs/*:refs/*',
3946N/A mirror: 'true'
3946N/A
3946N/A pull
3946N/A end
3946N/A
3946N/A def clone_svn(url)
3946N/A options = { url: url }
3946N/A
6074N/A # Do we have a standard layout?
6074N/A if self.class.svn_ls(url).split("\n") == %w( branches/ tags/ trunk/ )
5322N/A options.merge! \
3946N/A fetch: 'trunk:refs/remotes/trunk',
6074N/A branches: 'branches/*:refs/remotes/*',
3946N/A tags: 'tags/*:refs/remotes/tags/*'
3946N/A else
3946N/A options.merge! \
6074N/A fetch: ':refs/remotes/git-svn'
3946N/A end
3946N/A
3946N/A set_section %w( svn-remote svn ), options
3946N/A pull_svn
3946N/A end
3946N/A
6074N/A def pull
3946N/A with_head_change do
3946N/A git_exec 'remote', 'update'
3946N/A end
6074N/A end
6074N/A
3946N/A # Fetches the latest commits and resets the local master
3946N/A def pull_svn
6074N/A git_exec 'svn', 'fetch'
5322N/A
3946N/A if svn_has_trunk?
3946N/A reset_branch 'master', "remotes/trunk"
3946N/A else
3946N/A reset_branch 'master', "remotes/git-svn"
3946N/A end
3946N/A end
3946N/A
3946N/A def svn_has_trunk?
3946N/A get_config('svn-remote.svn.fetch').starts_with?('trunk:')
3946N/A end
3946N/A
3946N/A # Sets the reference of a local branch
3946N/A def reset_branch(branch, ref)
3946N/A with_head_change do
3946N/A git_exec 'branch', '-f', branch, ref
4127N/A end
5322N/A end
5322N/A
6077N/A module ClassMethods
3946N/A def is_git_repository?(address)
3946N/A !!(exec 'git', 'ls-remote', address)
3946N/A rescue Subprocess::Error => e
3946N/A if e.status == 128
3946N/A false
3946N/A else
3946N/A raise e
6074N/A end
6074N/A end
6074N/A
6074N/A def svn_ls(address)
6074N/A exec 'svn', 'ls', address
6074N/A end
3946N/A
3946N/A def is_svn_repository?(address)
3946N/A svn_ls address
3946N/A true
3946N/A rescue Subprocess::Error
3946N/A false
3946N/A end
3946N/A
3946N/A def exec(*args)
6074N/A Subprocess.run *args
6074N/A end
3946N/A end
6074N/A
6074N/A protected
6074N/A
6074N/A # Executes a git command
3946N/A def git_exec(*args)
3946N/A args.unshift 'git'
6074N/A args.push \
6074N/A GIT_DIR: local_path.to_s,
6074N/A LANG: 'C'
6074N/A
6074N/A Subprocess.run *args
6074N/A end
3946N/A
3946N/A # Yields the given block and returns the head OID
5322N/A # before and after yielding the block.
5322N/A def with_head_change(*args)
3946N/A old_oid = head_oid rescue nil
3946N/A yield
3946N/A Ref.new(old_oid, head_oid)
3946N/A end
3946N/A
3946N/A def local_path
3946N/A repo.path
3946N/A end
3996N/Aend
3996N/A