Sha256: 8475bbe3549f53a5469f7dcf3b6f2ac93602edfed1012ba728d12ef4bb45234c

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

require "pathname"

module GitCommands
  using Colorize
  class Branch
    MASTER = "master"
    ORIGIN = "origin/"

    def self.strip_origin(name)
      name.strip.split(ORIGIN).last
    end

    def self.by_file(path)
      return [] unless valid_path?(path)
      File.foreach(path).map do |name|
        new(name.strip)
      end.select(&:valid?)
    end

    def self.by_pattern(pattern)
      return [] unless pattern.index("*")
      `git branch -r --list #{ORIGIN}#{pattern}`.split("\n").map do |name|
        new(strip_origin(name))
      end.reject(&:master?)
    end

    def self.by_names(names_list)
      String(names_list).split(",").map do |name|
        new(name.strip)
      end.select(&:valid?)
    end

    def self.factory(src)
      return [] unless src
      branches = by_file(src)
      branches = by_pattern(src) if branches.empty?
      branches = by_names(src) if branches.empty?
      branches
    end

    def self.valid_path?(path)
      path = Pathname.new(path)
      warn "'#{path}' must be an absolute path!".red unless path.absolute?
      path.absolute? && path.file?
    end

    attr_reader :name

    def initialize(name)
      @name = name
    end

    def to_s
      @name
    end

    def valid?
      return false if master?
      return false unless exists?
      true
    end

    def ==(other)
      self.name == other.name
    end

    def master?
      @name == MASTER
    end

    def exists?(remote = true)
      origin = ORIGIN if remote
      `git rev-parse --verify #{origin}#{@name} 2> /dev/null`.match(/^[0-9a-z]+/)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
git_commands-3.3.3 lib/git_commands/branch.rb
git_commands-3.3.2 lib/git_commands/branch.rb
git_commands-3.3.1 lib/git_commands/branch.rb