Ravn::CLI::

DevSubcommand module

Public Class Methods

extended( mod )

Extension callback – also extend with regular Subcommand utilities.

# File lib/ravn/cli.rb, line 636
def self::extended( mod )
        super

        mod.extend( Ravn::CLI::Subcommand )
end

Public Instance Methods

all_project_repos()

Return an array of basenames of repos in in the project dir.

# File lib/ravn/cli.rb, line 706
def all_project_repos
        return Dir[ Ravn::CLI.projects_dir + '*/.git' ].map { |d| File.dirname( d ) }
end
git_checkout( rev )

Do a git checkout with the specified rev.

# File lib/ravn/cli.rb, line 815
def git_checkout( rev )
        unless_dryrun( "Checking out rev #{rev}." ) do
                execute( 'git', 'checkout', rev )
        end
end
git_clone_ravn_project( projectname )

Clone the specified Ravn project from Github.

# File lib/ravn/cli.rb, line 716
def git_clone_ravn_project( projectname )
        remote = RAVN_GIT_URL + projectname + '.git'
        execute( 'git', 'clone', remote )
end
git_commits( branchname )

Return a list of the commits in the branch with the given branchname.

# File lib/ravn/cli.rb, line 823
def git_commits( branchname )
        result = ""
        output = read_output_of( 'git', '--no-pager', 'log',
                '--pretty=format:%C(yellow)%h %ad %Creset%s%Cblue [%cn]',
                '--decorate',
                '--date=relative',
                "..origin/#{branchname}" ) or return ["ERROR (no output)"]

        return output.each_line.collect do |commit|
                case commit
                when /:migration:/
                        " %s %s" % [ highlight_string("⚠"), commit ]
                else
                        '  ' + commit
                end
        end
end
git_current_branch()

Return the name of the branch of the git repo in the current directory.

# File lib/ravn/cli.rb, line 723
def git_current_branch
        cmd = %w[git --no-pager symbolic-ref --short HEAD]
        result = read_output_of( cmd ) or raise "Couldn't get current git branch."
        return result.strip
end
git_fetch()

Do a git fetch in the current directory.

# File lib/ravn/cli.rb, line 783
def git_fetch
        unless_dryrun( "Fetching from origin." ) do
                execute( 'git', 'fetch' )
        end
end
git_fetch_all( repos )

Do a ‘git fetch’ on all the specified repos in parallel.

# File lib/ravn/cli.rb, line 791
def git_fetch_all( repos )
        unless_dryrun( "Fetching from origin for all repos." ) do
                pids = repos.map do |repo|
                        repodir = Ravn::CLI.projects_dir + repo
                        spawn( 'git', 'fetch', out: '/dev/null', err: [:child, 1], chdir: repodir )
                end

                with_progress( "Fetching", pids ) do |pid|
                        Process.waitpid( pid )
                end
        end
end
git_pull()

Do a git pull and return the output lines.

# File lib/ravn/cli.rb, line 806
def git_pull
        unless_dryrun( "Pulling from the current repo.", ['dryrun'] ) do
                raw_output = read_output_of( 'git', 'pull' ) or return nil
                return raw_output.strip.each_line.map( &:chomp )
        end
end
git_rev_parse( rev )

Run git rev-parse on the specified rev and return the resulting SHA, or nil if it doesn’t result in a SHA.

# File lib/ravn/cli.rb, line 764
def git_rev_parse( rev )
        cmd = %w[git --no-pager rev-parse] + [ rev.to_s ]
        result = read_output_of( cmd ) or raise "Couldn't parse rev #{rev}"
        self.log.debug "Rev-parse returned: %p" % [ result ]
        result.chomp!

        return nil unless result =~ /\A\p{Xdigit}{40}\z/
        return result
end
git_status()

Fetch the current git status as a Hash keyed by the porcelain status type.

# File lib/ravn/cli.rb, line 739
def git_status
        cmd = %w[git --no-pager status -b --porcelain]
        raw_output = read_output_of( cmd ) or raise "Couldn't get current git status."

        self.log.debug "Status raw output was: %p" % [ raw_output ]

        status = {}
        raw_output.each_line do |line|
                sigil, value = line.strip.split( /\s+/, 2 )
                self.log.debug "Adding %p line to the status: %p" % [ sigil, value ]
                case sigil
                when '##'
                        status[ '##' ] = value
                else
                        status[ sigil ] ||= []
                        status[ sigil ] << value
                end
        end

        return status
end
git_tracking_branch()

Return the name of the tracking branch for the repo in the current directory.

# File lib/ravn/cli.rb, line 731
def git_tracking_branch
        status = git_status()
        return nil unless status['##']
        return status['##'][ /\.\.(\S+)\s/, 1 ]
end
git_up_to_date?()

Returns true if the branch of the git repo in the current directory is up to date.

# File lib/ravn/cli.rb, line 776
def git_up_to_date?
        status = git_status()
        return status['##'] && !status['##'].include?( 'behind' )
end
in_project_dir( subdir=nil, &block )
in_project_directory( subdir=nil, &block )

Set the CWD to either the specified subdir of the configured project directory, if specified, or the project directory itself, yield to the block, then restore the original CWD. Exits the command with an appropriate error message if the configured project directory doesn’t exist.

# File lib/ravn/cli.rb, line 647
def in_project_directory( subdir=nil, &block )
        unless Ravn::CLI.projects_dir.directory?
                Ravn::CLI::Subcommand.exit_now!(
                        "Configured projects dir (%s) does not exist or isn't a directory." %
                                [ Ravn::CLI.projects_dir.to_s ]
                )
        end

        new_cwd = Ravn::CLI.projects_dir
        new_cwd += subdir if subdir

        self.log.debug "In directory %s:" % [ new_cwd ]
        Dir.chdir( new_cwd, &block )
end
Also aliased as: in_project_dir
normalize_project_name( raw_name )

Given a raw_name for a Ravn project, return a normalized form vetted against the configured projects.

# File lib/ravn/cli.rb, line 666
def normalize_project_name( raw_name )
        return raw_name if Ravn::CLI.repos.include?( raw_name )
        return "ravn-#{raw_name}" if !raw_name.start_with?( 'ravn-' ) &&
                Ravn::CLI.repos.include?( "ravn-#{raw_name}")

        return raw_name
end
repos_from_options( options, args )

Return an array of repo names based on provided options and arguments. If options.all, use all repos in the project directory. If not, and repos are passed in as positional arguments, use them. Otherwise, use the default set of repos.

# File lib/ravn/cli.rb, line 679
def repos_from_options( options, args )
        repos = args.dup

        if options.all
                all_repos = all_project_repos()
                repos.concat( all_repos )
        elsif repos.empty?
                repos.concat( Ravn::CLI.repos )
        end

        if options.branch
                self.log.debug "Filtering repos with a %p branch checked out" % [ options.branch ]
                repos.reject! do |repo|
                        branch = in_project_directory( repo ) do
                                git_current_branch()
                        end

                        self.log.debug "  current branch is: %p" % [ branch ]
                        branch != options.branch
                end
        end

        return repos.compact
end