Class: GitThin::Export

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



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
# 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

Class Method Details

.optionsObject



19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#export_gitObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 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_lfsObject



173
174
175
176
177
178
# 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_localObject



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

run_shell ‘git lfs install’,true ,true run_shell ‘git lfs fetch –all’,false ,false ,1



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/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_branchObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

#runObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# 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!Object



89
90
91
92
93
# 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