Class: MGit::Info

Inherits:
BaseCommand show all
Defined in:
lib/m-git/command/info.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::HIGH_PRIORITY_OPT_LIST, BaseCommand::SELECTABLE_OPT_LIST

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#all_repos, cmd, #exec_light_repos, #generate_config_repo, inherited, #initialize, #locked_repos, #run

Constructor Details

This class inherits a constructor from MGit::BaseCommand

Class Method Details

.descriptionObject



64
65
66
# File 'lib/m-git/command/info.rb', line 64

def self.description
  "输出指定仓库的信息。"
end

.usageObject



68
69
70
# File 'lib/m-git/command/info.rb', line 68

def self.usage
  "mgit info <repo>... [-h]"
end

Instance Method Details

#calculate_size(repo) ⇒ Object



48
49
50
51
52
# File 'lib/m-git/command/info.rb', line 48

def calculate_size(repo)
  success, output = repo.execute("du -sh #{repo.path} | awk '{print $1}'")
  return '计算失败'.red unless success
  output.chomp
end

#check_stash(repo) ⇒ Object



54
55
56
57
58
# File 'lib/m-git/command/info.rb', line 54

def check_stash(repo)
  success, output = repo.execute("git -C \"#{repo.path}\" stash list")
  return "查询失败".red unless success
  output.length > 0 ? '有内容' : '无内容'
end

#enable_short_basic_optionObject



60
61
62
# File 'lib/m-git/command/info.rb', line 60

def enable_short_basic_option
  false
end

#execute(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/m-git/command/info.rb', line 9

def execute(argv)
  Output.puts_start_cmd

  query_repo_names = parse_repo_name(argv)
  quere_repos = (all_repos + locked_repos).select { |e| query_repo_names.include?(e.name.downcase) }
  if quere_repos.length > 0
    quere_repos.each { |repo|
      puts Output.generate_title_block(repo.name) {
        info = []
        info.push(['仓库位置'.bold, ["#{repo.path}"]])
        info.push(['占用磁盘大小'.bold, ["#{calculate_size(repo)}"]])
        info.push(['创建时间'.bold, ["#{File.ctime(repo.path)}"]])

        current_branch = repo.status_checker.current_branch(strict_mode:false)
        branch_message = repo.status_checker.branch_message
        info.push(['当前分支'.bold, ["#{current_branch.nil? ? '' : current_branch}"]])
        info.push(['分支状态'.bold, ["#{branch_message}"]])

        info.push(['文件改动'.bold, ["#{repo.status_checker.status != Repo::Status::GIT_REPO_STATUS[:clean] ? '有本地改动,请用status指令查看细节' : '本地无修改'}"]])
        info.push(['Stash状态'.bold, ["#{check_stash(repo)}"]])
        Output.generate_table_combination(info) + "\n\n"
      }
    }
    Output.puts_succeed_cmd(argv.absolute_cmd)
  else
    Output.puts_fail_message("未找到与输入仓库名匹配的仓库,请重试!")
  end

end

#parse_repo_name(argv) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/m-git/command/info.rb', line 39

def parse_repo_name(argv)
  return nil if argv.git_opts.nil?
  repos = argv.git_opts.split(' ')
  extra_opts = repos.select { |e| argv.is_option?(e) }
  Foundation.help!("输入非法参数:#{extra_opts.join('')}。请通过\"mgit #{argv.cmd} --help\"查看用法。") if extra_opts.length > 0
  Foundation.help!("未输入查询仓库名!请使用这种形式查询:mgit info repo1 repo2 ...") if repos.length == 0
  repos.map { |e| e.downcase }
end