Class: GitThin::Expire

Inherits:
Thin
  • Object
show all
Includes:
GitThinUtils
Defined in:
lib/git-thin/command/expire.rb

Constant Summary

Constants included from GitThinUtils

GitThinUtils::LOGA, GitThinUtils::LOGC, GitThinUtils::LOGN, GitThinUtils::LOGNone, GitThinUtils::LOGPRUNE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitThinUtils

#logC, #logE, #logInner, #logN, #logP, #logW, #print_console, #run_shell, #set_progress

Methods inherited from Thin

run

Constructor Details

#initialize(argv) ⇒ Expire

Returns a new instance of Expire.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

Class Method Details

.optionsObject



18
19
20
21
22
23
24
# 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

Instance Method Details

#expire_branchsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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

#runObject



120
121
122
123
# File 'lib/git-thin/command/expire.rb', line 120

def run
    expire_branchs

end

#validate!Object



85
86
87
88
# File 'lib/git-thin/command/expire.rb', line 85

def validate!
    super
    help! 'validate SOURCE_ROOT is required.' unless @source_root
end