class GitThin::Export

Public Class Methods

new(argv) click to toggle source
Calls superclass method GitThin::Thin::new
# File lib/git-thin/command/export.rb, line 29
def initialize(argv)
    super
    @verbose = argv.flag?('verbose',LOGNone )
    if @verbose
        @verbose = LOGC|LOGN
    end
    @force = argv.flag?('force')
    @support_types = ["local","record","git","lfs"]
    @type = argv.option('type')
    if not @support_types.include? @type
        @type = nil
        logE 'type not support'
        return
    end
    @source_root = argv.option('source_root')
    @target_git = argv.option('target_git')
    if @type=='git' && !@target_git
        logE 'need a target_git if type is git'
        return
    end
  if not @source_root
      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 File.exist? @source_root+'/.git'
          UI.puts "git repository not found"
          exit 1
      end
  end
  if not Dir.exist? @source_root
      @source_root = nil
      return
  end
end
options() click to toggle source
Calls superclass method
# File lib/git-thin/command/export.rb, line 19
def self.options
    [
      ['--force', 'Overwrite existing branch.'],
      ['--target_git', 'Export to the target Git repository'],
      ['--type', 'Export "local","record","git" or "lfs".defalue all'],
      ['--source_root', 'Specify the warehouse address manually if necessary.'],
    ].concat(super)

end

Public Instance Methods

export_git() click to toggle source
# File lib/git-thin/command/export.rb, line 179
def export_git
    fetch_branch
    Dir.chdir(@source_root)
    progress = 0.0
    run_shell "git remote",false ,true do |out,err,status|
        @remote=out[0].strip
    end
    run_shell "git remote add tmp "+@target_git,true ,true
    run_shell "git fetch #{@remote} -pf",true ,true

    error_branch = []
    success_branch = []
    @branchs.each_index do|index|
        branch = @branchs[index]
        if index*1.0/@branchs.length - progress > 0.001
            progress = index*1.0/@branchs.length
            set_progress(progress*1000.to_int)
            # logN "#{format("%0.2f",progress*100).to_f}%"
        end
        branch=branch[@remote.length+1..-1]
        run_shell "git checkout -B #{branch} #{@remote}/#{branch} -f",true ,true do |out,err,status|
            if status == 0 && out.length > 0
                head=nil
                run_shell "git rev-parse HEAD",true ,true do |out,err,status|
                    if status == 0
                        head = out[0].strip
                    end
                end
                need_update = true
                if head
                    run_shell "git log tmp/#{branch} -1 --pretty=format:%H",true ,true do|out,err,status|
                        if out.length > 0 && out[0].index(head) == 0
                            need_update = false
                        end
                    end
                end
                if need_update
                    run_shell "git push -u tmp --no-verify",true ,true do|out,err,status|
                        if status == 0
                            success_branch.push branch
                        else
                            logE err.join('')
                            error_branch.push branch
                        end
                    end
                else
                    logW 'jump branch:'+branch
                end

            else
                logE err.join('')
                error_branch.push branch
            end
        end
    end
    logN 'success branchs:\n'+success_branch.join('')
    logN 'error branchs:\n'+error_branch.join('')


end
export_lfs() click to toggle source
# File lib/git-thin/command/export.rb, line 173
def export_lfs
    Dir.chdir(@source_root)
    run_shell 'git lfs install',true ,true
    run_shell 'git lfs fetch --all',false ,false ,1

end
export_local() click to toggle source
# File lib/git-thin/command/export.rb, line 95
def export_local
    Dir.chdir(@source_root)
    run_shell 'git lfs fetch --all',false ,LOGA ,1

    fetch_branch
    run_shell "git remote",false ,LOGNone do |out,err,status|
        @remote=out[0].strip
    end

    progress = 0.0
    logP "当前进度:#{format("%0.2f",progress*100).to_f}%"
    @branchs.each_index do|index|
        branch = @branchs[index]
        if index*1.0/@branchs.length - progress > 0.001
            progress = index*1.0/@branchs.length
            logP "当前进度:#{format("%0.2f",progress*100).to_f}%"
        end
        branch=branch[@remote.length+1..-1]
        run_shell "git checkout -B #{branch} #{@remote}/#{branch} -f",true ,@verbose
    end

    # run_shell 'git lfs install',true ,true
    # run_shell 'git lfs fetch --all',false ,false ,1
end
export_record() click to toggle source
# File lib/git-thin/command/export.rb, line 119
def export_record
    fetch_branch
    Dir.chdir(@source_root)
    if Dir.exist? ('branchs.bak')
        FileUtils.rm_rf('branchs.bak')
    end
    FileUtils.mkdir('branchs.bak')
    FileUtils.mkdir('branchs.bak/branchs')


    File.open("branchs.bak/branchs.json", "w+") do |aFile|
        aFile.syswrite(@branchs.join("\n"))
    end

    logN '开始导出分支'
    progress = 0.0
    @branchs.each_index do|index|
        branch = @branchs[index]
        if index*1.0/@branchs.length - progress > 0.001
            progress = index*1.0/@branchs.length
            logP "当前进度:#{format("%0.2f",progress*100).to_f}%"
        end

        run_shell "git log --oneline #{branch}",true ,LOGNone do |out,err,status|
            items = branch.split '/'
            name = items[-1]
            items.delete_at -1
            FileUtils.mkdir_p "branchs.bak/branchs/#{items.join '/'}"
            File.open("branchs.bak/branchs/#{branch}", "w+") do |aFile|
                aFile.syswrite(out.join(""))
            end
        end
    end

    logN '开始导出tags'
    run_shell 'git ls-remote -t --refs',false ,LOGNone do |out,err,status|
        if status == 0
            @tags = {}
            out.each { |item|
                items = item.split ' '
                ref = items[0]
                tag = items[1]
                tag = tag['refs/tags/'.length..-1]
                @tags[tag] = ref
            }

        end
    end
    File.open("branchs.bak/tags.json", "w+") do |aFile|
        aFile.syswrite(@tags.to_json)
    end
    logN '导出完成'

end
fetch_branch() click to toggle source
# File lib/git-thin/command/export.rb, line 67
def fetch_branch
    Dir.chdir(@source_root)
    run_shell 'git branch -r',false ,LOGNone 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 branch',false ,LOGNone do |out,err,status|
        if status == 0
            @local_branchs = out.map { |line| line = line.strip }
        end
    end
end
run() click to toggle source
# File lib/git-thin/command/export.rb, line 239
def run
    if @type.nil? || @type == "local"
        export_local
    end
    if @type.nil? || @type == "record"
        export_record
    end
    if @type.nil? || @type == "lfs"
        export_lfs
    end
    if @type=='git' && @target_git
        export_git
    end


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