# This is very basic git wrapper for internal Nixenvironment gem needs # It's used instead of popular 'git' gem (https://rubygems.org/gems/git) # because 'git' gem is redundant and also doesn't satisfy all Nixenvironment gem needs require_relative 'cmd_executor' module Nixenvironment class Git < CmdExecutor @binary_name = 'git' HEAD = 'HEAD' REMOTE_MASTER = 'refs/heads/master' DEFAULT_REFSPEC = "#{HEAD}:#{REMOTE_MASTER}" CLONE_CMD = 'clone' FETCH_CMD = 'fetch' CHECKOUT_CMD = 'checkout' COMMIT_CMD = 'commit' PUSH_CMD = 'push' REMOTE_ADD_CMD = 'remote add' STATUS_CMD = 'status' TAG_CMD = 'tag' LS_REMOTE = 'ls-remote' RECURSIVE_OPT = '--recursive' TAGS_OPT = '--tags' ORPHAN_OPT = '--orphan' MESSAGE_OPT = '--message' FORCE_OPT = '--force' PORCELAIN_OPT = '--porcelain' # options # :recursive, :r => after the clone is created, initialize all submodules within def self.clone(url, working_dir = nil, options = {}) arr_opts = [] arr_opts << RECURSIVE_OPT if options[:recursive] || options[:r] arr_opts << url arr_opts << working_dir if working_dir.present? execute(CLONE_CMD, arr_opts) end # options # :tags, :t => fetch tags def self.fetch(options = {}) arr_opts = [] arr_opts << TAGS_OPT if options[:tags] || options[:t] execute(FETCH_CMD, arr_opts) end # options # :orphan => orphan branch def self.checkout(branch, options = {}) arr_opts = [] arr_opts << ORPHAN_OPT if options[:orphan] arr_opts << branch execute(CHECKOUT_CMD, arr_opts) end # options # :message, :m => commit message def self.commit(options = {}) message = options[:message] || options[:m] arr_opts = [] arr_opts << "#{MESSAGE_OPT} '#{message}'" if message execute(COMMIT_CMD, arr_opts) end # options # :force, :f => force push, ignore checks def self.push(remote = nil, refspec = nil, options = {}) arr_opts = [] arr_opts << FORCE_OPT if options[:force] || options[:f] arr_opts << remote if remote.present? arr_opts << refspec if refspec.present? execute(PUSH_CMD, arr_opts) end def self.remote_add(name, url) arr_opts = [] arr_opts << name arr_opts << url execute(REMOTE_ADD_CMD, arr_opts) end # options # :porcelain => give the output in an easy-to-parse format def self.status(options = {}) arr_opts = [] arr_opts << PORCELAIN_OPT if options[:porcelain] execute(STATUS_CMD, arr_opts) end def self.tag tags = execute(TAG_CMD).lines tags.map!(&:strip!) tags.sort_by!(&:to_i) end def self.ls_remote(url) arr_opts = [] arr_opts << url execute(LS_REMOTE, arr_opts) end private_class_method :execute end end