class GitThin::Detect

Public Class Methods

new(argv) click to toggle source
Calls superclass method GitThin::Thin::new
# 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
options() click to toggle source
Calls superclass method
# 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

Public Instance Methods

export_expired() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
run() click to toggle source
# File lib/git-thin/command/detect.rb, line 187
def run
    export_expired

end
validate!() click to toggle source
Calls superclass method
# 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