Class: GitThin::Config

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

Returns a new instance of Config.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/git-thin/command/thin_config.rb', line 149

def initialize(argv)
    super
    @pwd = Dir.pwd
    @types = []
    @configs = {}
    @source_root = argv.option('source_root')
    @store = argv.flag?('store')
    @list = argv.flag?('list')
    @current = argv.flag?('current')
    @reset = argv.flag?('reset')
    @check = argv.flag?('check')

    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
    else
        if @source_root[-1] == '/'
            @source_root = @source_root[0..-2 ]
        end
    end
    if not Dir.exist? @source_root
        @source_root = nil
        return
    end
    if FileTest.exist? @source_root+'/.thinconfig'
        run_shell "git config -lf #{@source_root}/.thinconfig",true ,true do |out,err,status|
            @configs = ThinConfig.prase_config out
        end
    end
    @types =ThinConfig.prase_type_config  argv,@configs
end

Class Method Details

.optionsObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/git-thin/command/thin_config.rb', line 139

def self.options
    [
      ['--source_root', 'Specify the warehouse address manually if necessary.'],
      ['--store', 'Store config to lfs'],
      ['--list', 'List the configs supported by the locally repository, (configured by.thinconfig) '],
      ['--current', 'Displays the current configuration'],
      ['--reset', 'Reset config to thin and lfs'],
      ['--check', 'Check the integrity of the config'],
    ].concat(super)
end

Instance Method Details

#checkObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/git-thin/command/thin_config.rb', line 279

def check
    lfs_files = []
    run_shell "git lfs ls-files",false ,LOGNone do |outs,errs,status|
        if status
            for line in outs
                line[13..-2]
                lfs_files.push line[13..-2]
            end
        end
    end
    unmatch_files = []
    for lfs_file in lfs_files

        if match_config(lfs_file).nil?
            unmatch_files.push lfs_file
        end
    end
    if unmatch_files.length > 0
        logN 'The following LFS files are not configured:'
        for unmatch_file in unmatch_files
            logW unmatch_file
        end
    else
        logN 'All LFS files are configured'
    end
end

#currentObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/git-thin/command/thin_config.rb', line 245

def current

    types = ThinConfig::prase_pwd_config(@configs,@source_root,@pwd)
    if types.length == 0
        types.push ThinConfig::DEFAULT
    end
    for type in types
        config = @configs[type]
        if type == ThinConfig::DEFAULT
            git_config
            config = @git_configs[ThinConfig::DEFAULT]
        end
        if config
            logN "name:#{type}  lfs:#{config.getValue('lfs')}"
        end
    end
end

#git_configObject



194
195
196
197
198
199
200
# File 'lib/git-thin/command/thin_config.rb', line 194

def git_config
    if not @git_configs
        run_shell 'git config -l',true ,true do |outs,errs,status|
            @git_configs = ThinConfig.prase_config outs
        end
    end
end

#listObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/git-thin/command/thin_config.rb', line 222

def list
    logN 'List the configs supported by the locally repository: '
    git_config
    config = @git_configs[ThinConfig::DEFAULT]
    if config
        log = "name:#{config.name}"
        for key in config.keys
            log += " #{key}:#{config.getValue key}"
        end
        logN log
    end
    for config in @configs.values
        log = "name:#{config.name}"
        for key in config.keys
            log += " #{key}:#{config.getValue key}"
        end
        logN log
    end
end

#match_config(match_file) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/git-thin/command/thin_config.rb', line 263

def match_config(match_file)
    for config in @configs.values
        lfs = config.getValue 'lfs'
        if lfs
            lfs = lfs.map do|file|
                file.strip
            end
            for lf in lfs
                if match_file.index(lf) == 0
                    return config
                end
            end
        end
    end
    return nil
end

#resetObject



241
242
243
244
# File 'lib/git-thin/command/thin_config.rb', line 241

def reset
    run_shell "git config --unset thin.#{ThinConfig::DEFAULT} ",true ,true
    run_shell "git config --unset lfs.fetchinclude ",true ,true
end

#runObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/git-thin/command/thin_config.rb', line 305

def run
    Dir.chdir @source_root
    if @list
        list
    elsif @reset
        reset
    elsif @current
        current
    elsif @check
        check
    else
        values = run_config
        if values.length > 0
            logN "git config thin.#{ThinConfig::DEFAULT} "+values.join(",")
        else
            logW "not match analyze config"
        end
    end
    Dir.chdir @pwd
end

#run_configObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/git-thin/command/thin_config.rb', line 201

def run_config
    values=[]
    for type in @types
        config = @configs[type]
        if type == ThinConfig::DEFAULT
            git_config
            config = @git_configs[ThinConfig::DEFAULT]
        end
        if config
            values.push config.getValue('lfs')
        end
    end
    if values.length > 0
        run_shell "git config thin.#{ThinConfig::DEFAULT} "+values.join(','),true ,true
        if @store
            run_shell "git config lfs.fetchinclude "+values.join(',')
        end
    end
    return values
end

#validate!Object



189
190
191
192
# File 'lib/git-thin/command/thin_config.rb', line 189

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