Class: GitThin::Detect

Inherits:
Thin
  • Object
show all
Includes:
GitThinUtils
Defined in:
lib/git-thin/command/detect.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) ⇒ Detect

Returns a new instance of Detect.



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
84
# File 'lib/git-thin/command/detect.rb', line 28

def initialize(argv)
    super
    @expired_time = argv.option('expired_time','5184000').to_i
    @output_type = argv.option('output_type','txt')
    
    @excludes = argv.option('exclude','').split(',').map do |item|
        Regexp.new item.strip
    end
    @includes = argv.option('include','').split(',').map do |item|
        Regexp.new item.strip
    end
    @source_root = argv.option('source_root')
    if @source_root
        Dir.chdir @source_root
    end
    @master = argv.shift_argument
    if not @master
        # @master = 'master'
        return
    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
    run_shell 'git branch -r',false ,true do |out,err,status|
        if status == 0
            @branchs = out.map { |line| line = line.strip }
            @branchs.delete_if do |item|
                ret = false
                if item.include? '->'
                    ret = true
                end
                ret
            end
        end
    end

    run_shell "git log --pretty=format:%H origin/#{@master}",false ,true do |out,err,status|
        if status == 0
            @master_commits = out.map { |line|
                line.strip
            }
        end
    end
    run_shell "git log -1 --pretty=format:%ad origin/#{@master}" do |out,err,status|
        if status == 0 && out.length > 0
            commit_time = out[0].strip
            @master_time = Time.parse commit_time
        end
    end

end

Class Method Details

.optionsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/git-thin/command/detect.rb', line 18

def self.options
    [
      ['--expired_time', 'in sec. (defalut:5184000)'],
      ['--source_root', 'git repository root.'],
      ['--exclude', 'exclude branch.'],
      ['--include', 'include branch.'],
      ['--output_type', 'output type. json|txt'],
    ].concat(super)

end

Instance Method Details

#export_expiredObject



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/git-thin/command/detect.rb', line 92

def export_expired
    Dir.chdir(@source_root)
    run_shell 'git remote update',true ,true
    expired_branch = []
    error_branch = []
    match_black = []
    @branchs.each_index do |index|
        branch = @branchs[index]
        logN 'check branch:'+branch
        include = true
        if @includes.length > 0
            include = false
            for inc in @includes
                if inc =~ branch
                    include = true
                    break
                end
            end
        end
        if include
            exclude = false
            for exd in @excludes
                if exd =~ branch
                    exclude = true
                    match_black.push[branch]
                    break
                end
            end
            if not exclude
                run_shell "git log -1 --pretty=format:\"%H|%ad|%ce\" #{branch}",true ,false do |out,err,status|
                    if status == 0 && out.length > 0
                        items = out[0].split '|'
                        commit = items[0].strip
                        commit_time = items[1].strip
                        time = Time.parse commit_time
                        delay = @master_time.to_i - time.to_i
                        if delay > @expired_time.to_i
                            email = items[2]
                            if not email
                                email = ''
                            end
                            detached_count = 0
                            run_shell "git log --pretty=%ae #{@master}..#{branch}",true ,true do |out,stderr|
                                detached_count = out.length
                            end
                            expired_branch.push ({ "email"=>email,"branch"=>branch,"commit"=>commit,"detached_count"=>detached_count,"delay"=>delay/(60*60*24) })
                            logW 'find expired branch:' +branch
                        end

                    else
                        error_branch.push branch
                    end
                end
            end
        end

    end
    expired_branch.sort! do |a,b|
        a["email"] <=> b["email"]
    end
    if expired_branch.length > 0
        if @output_type == "txt"
            output_txt expired_branch
        elsif @output_type == "json"
            output_json expired_branch
        else
            output_json expired_branch
            output_txt expired_branch
        end

    else
        logW 'not match branch'
    end

    for branch in error_branch
        logE "Error fetch branch:#{branch},please check manually"
    end
    for branch in match_black
        logE "branch #{branch} match exclude"
    end
end

#output_json(expired_branch) ⇒ Object



173
174
175
176
177
# File 'lib/git-thin/command/detect.rb', line 173

def output_json(expired_branch)
    File.open("expired.json", "w+") do |aFile|
        aFile.syswrite(expired_branch.to_json)
    end
end

#output_txt(expired_branch) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/git-thin/command/detect.rb', line 179

def output_txt(expired_branch)
    File.open("expired.text", "w+") do |aFile|
        aFile.syswrite(expired_branch[0].keys.join("\t")+"\n")
        expired_branch.each { |item|
            aFile.syswrite(item.values.join("\t")+"\n")
        }
    end
end

#runObject



187
188
189
190
# File 'lib/git-thin/command/detect.rb', line 187

def run
    export_expired

end

#validate!Object



86
87
88
89
90
# File 'lib/git-thin/command/detect.rb', line 86

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