class GitThin::Expire

Public Class Methods

new(argv) click to toggle source
Calls superclass method GitThin::Thin::new
# File lib/git-thin/command/expire.rb, line 25
def initialize(argv)
    super

    @branchs = argv.shift_argument
    if not @branchs
        return
    end
    @source_root = argv.option('source_root')
    @exclude_branchs = argv.option('exclude_branchs')
    if @source_root
        Dir.chdir @source_root
    end
    run_shell 'git rev-parse --show-toplevel',false ,LOGNone do |out,err,status|
        if status == 0
            @source_root = out[0].strip
        end
    end

    if not Dir.exist? @source_root
        @source_root = nil
        return
    end

    if FileTest.exist? @branchs
        File.open(@branchs, "r") do |aFile|
            @branchs = ""
            aFile.each do |line|
                @branchs += line
            end
        end
    end
    @branchs = @branchs.gsub(',',' ')
    @branchs = @branchs.split ' '

    if FileTest.exist? @exclude_branchs
        File.open(@exclude_branchs, "r") do |aFile|
            @exclude_branchs = ""
            aFile.each do |line|
                @exclude_branchs += line
            end
        end
    end
    @exclude_branchs = @exclude_branchs.gsub(',',' ')
    @exclude_branchs = @exclude_branchs.split ' '

    run_shell 'git branch -r',false ,true do |out,err,status|
        if status == 0
            @remote_branchs = out.map { |line| line = line.strip }
            @remote_branchs.delete_if do |item|
                ret = false
                if item.include? '->'
                    ret = true
                end
                ret
            end
        end
    end

end
options() click to toggle source
Calls superclass method
# File lib/git-thin/command/expire.rb, line 18
def self.options
    [
      ['--source_root', 'Specify the warehouse address manually if necessary.'],
      ['--exclude_branchs', 'The name of the excluded branch,file or string.'],
    ].concat(super)

end

Public Instance Methods

expire_branchs() click to toggle source
# File lib/git-thin/command/expire.rb, line 90
def expire_branchs
    Dir.chdir(@source_root)
    error_branchs = []
    exclude_count = 0
    @branchs.each do |branch|
        if @exclude_branchs.include? branch
            logN "#{branch}分支名字黑名单,跳过"
            exclude_count += 1
        else
            if @remote_branchs.include? branch
                index = branch.index('/')
                branch[index] = ' '
                # "git push -d #{branch}"
                run_shell "git push -d #{branch}",true ,true do|out,err,status|
                    if status!= 0
                        error_branchs.push branch
                    else
                        logN "#{branch}分支删除成功"
                    end
                end
            else
                error_branchs.push branch
            end
        end
    end
    error_branchs.each do |branch|
        logE "#{branch}分支删除失败"
    end
    logN "总共#{@remote_branchs.length}个分支,计划删除#{@branchs.length}个分支,黑名单跳过#{exclude_count}个,删除失败#{error_branchs.length}个分支"
end
run() click to toggle source
# File lib/git-thin/command/expire.rb, line 120
def run
    expire_branchs

end
validate!() click to toggle source
Calls superclass method
# File lib/git-thin/command/expire.rb, line 85
def validate!
    super
    help! 'validate SOURCE_ROOT is required.' unless @source_root
end