Class: MGit::Status

Inherits:
BaseCommand show all
Defined in:
lib/m-git/command/status.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



126
127
128
# File 'lib/m-git/command/status.rb', line 126

def self.description
  "输出所有仓库的状态。包括:\"分支\",\"暂存区\",\"工作区\",\"特殊(未跟踪和被忽略)\",\"冲突\"。"
end

.usageObject



130
131
132
# File 'lib/m-git/command/status.rb', line 130

def self.usage
  "mgit status [(-m|-e) <repo>...] [-h]"
end

Instance Method Details

#enable_repo_selectionObject



118
119
120
# File 'lib/m-git/command/status.rb', line 118

def enable_repo_selection
  true
end

#enable_short_basic_optionObject



122
123
124
# File 'lib/m-git/command/status.rb', line 122

def enable_short_basic_option
  true
end

#execute(argv) ⇒ Object



8
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/m-git/command/status.rb', line 8

def execute(argv)
  Output.puts_processing_message("正在检查各仓库状态...")

  status_info = {}
  mutex = Mutex.new
  mutex_branch = Mutex.new
  mutex_modification = Mutex.new
  mutex_other = Mutex.new

  branch_notice = []
  modification_notice = []
  other_notice = []
  in_progress_notice = []

  task_count = 0
  repo_combo = all_repos + locked_repos
  Output.update_progress(repo_combo.length, task_count)
  Workspace.concurrent_enumerate(repo_combo) { |repo|
    status_msg = ''
    info = []

    # 非锁定仓库进行常规分支检查
    if !locked_repos.include?(repo) &&
      repo.status_checker.branch_status != Repo::Status::GIT_BRANCH_STATUS[:up_to_date] &&
      repo.status_checker.branch_status != Repo::Status::GIT_BRANCH_STATUS[:no_remote]
        info.push(['分支', [repo.status_checker.branch_message]])

        mutex_branch.lock
        branch_notice.push(repo.name)
        mutex_branch.unlock
    end

    # 检查工作区状态
    if repo.status_checker.status != Repo::Status::GIT_REPO_STATUS[:clean]
      info += repo.status_checker.message
      mutex_modification.lock
      modification_notice.push(repo.name)
      mutex_modification.unlock
    end

    # 检查url是否一致
    if !repo.url_consist?
      info.push(['其他', ['仓库实际url与当前配置不一致']])
      mutex_other.lock
      other_notice.push(repo.name)
      mutex_other.unlock
    end

    # 生成表格
    status_msg = Output.generate_table_combination(info) + "\n\n" if info.length > 0

    # 压缩状态信息
    mutex.lock
    if status_msg.length > 0
      status_info[status_msg] = {'repo_names' => [], 'info' => info} if status_info[status_msg].nil?
      status_info[status_msg]['repo_names'].push(repo.name)
    end
    task_count += 1
    Output.update_progress(repo_combo.length, task_count)
    mutex.unlock
  }

  status_info.each_with_index { |(status_msg, item), index|
    info = item['info']
    repo_names = item['repo_names']
    Output.puts_remind_block(repo_names, "以上仓库状态:")
    MGit::Loger.info(info)
    status_msg += "\n" if index != status_info.length - 1
    puts status_msg
  }

  OperationProgressManager::PROGRESS_TYPE.each { |type, type_str|
    if OperationProgressManager.is_in_progress?(Workspace.root, type_str)
      in_progress_notice.push(type.to_s)
    end
  }

  summary = []
  if branch_notice.length > 0
    summary.push(["分支提醒(#{branch_notice.length})",branch_notice])
  end

  if modification_notice.length > 0
    summary.push(["改动提醒(#{modification_notice.length})",modification_notice])
  end

  if other_notice.length > 0
    summary.push(["其他警告(#{other_notice.length})", other_notice])
  end

  if in_progress_notice.length > 0
    summary.push(["处于中间态的操作",in_progress_notice])
  end

  if summary.length > 0
    puts "\n"
    puts Output.generate_table_combination(summary, title: "状态小结", separator: "|")
    MGit::Loger.info('状态小结')
    MGit::Loger.info(summary)
  end

  Output.puts_success_message("所查询仓库均无改动!") if status_info.keys.length == 0

  Output.puts_succeed_cmd(argv.absolute_cmd)
end

#validate(argv) ⇒ Object



114
115
116
# File 'lib/m-git/command/status.rb', line 114

def validate(argv)
  Foundation.help!("输入非法参数:#{argv.git_opts}。请通过\"mgit #{argv.cmd} --help\"查看用法。") if argv.git_opts.length > 0
end