Class: MGit::Branch

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

Overview

follow git branch eg: mgit branch –compact

Constant Summary collapse

OPT_LIST =
{
  :compact    =>  '--compact',
}.freeze

Constants inherited from BaseCommand

MGit::BaseCommand::HIGH_PRIORITY_OPT_LIST, MGit::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



81
82
83
# File 'lib/m-git/command/branch.rb', line 81

def self.description
  "创建、显示、删除分支。"
end

.usageObject



85
86
87
# File 'lib/m-git/command/branch.rb', line 85

def self.usage
  "mgit branch [<git-branch-option>|--compact] [(--mrepo|--el-mrepo) <repo>...] [--help]"
end

Instance Method Details

#enable_repo_selectionObject



77
78
79
# File 'lib/m-git/command/branch.rb', line 77

def enable_repo_selection
  true
end

#execute(argv) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/m-git/command/branch.rb', line 21

def execute(argv)
  Output.puts_start_cmd

  if argv.opt(OPT_LIST[:compact])
    show_compact_branches(argv)
    return
  end

  # 无自定义参数则透传
  extcute_as_common(argv)
end

#extcute_as_common(argv) ⇒ Object

常规执行



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/m-git/command/branch.rb', line 34

def extcute_as_common(argv)
  error_repos = {}
  all_repos.sort_by { |repo| repo.name }.each { |repo|
    success, output = repo.execute_git_cmd(argv.cmd, argv.git_opts)
    if success && output.length > 0
      puts Output.generate_title_block(repo.name) {
        output
      } + "\n"
    elsif !success
      error_repos[repo.name] = output
    end
  }
  if error_repos.length > 0
    Workspace.show_error(error_repos)
  else
    Output.puts_succeed_cmd(argv.absolute_cmd)
  end
end

#optionsObject



15
16
17
18
19
# File 'lib/m-git/command/branch.rb', line 15

def options
  [
      ARGV::Opt.new(OPT_LIST[:compact], info:"以归类的方式显示所有仓库的当前分支。", type: :boolean)
  ].concat(super)
end

#show_branches_for_repos(repos, locked) ⇒ Object

紧凑地显示一组仓库分支



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/m-git/command/branch.rb', line 61

def show_branches_for_repos(repos, locked)
  return if repos.nil?

  list = {}
  repos.sort_by { |repo| repo.name }.each { |repo|
    branch = repo.status_checker.current_branch(strict_mode:false)
    branch = 'HEAD游离,不在任何分支上!' if branch.nil?
    list[branch] = [] if list[branch].nil?
    list[branch].push(repo.name)
  }
  list.each { |branch, repo_names|
    Output.puts_remind_block(repo_names, "以上仓库的当前分支:#{branch}#{' [锁定]' if locked}")
    puts "\n"
  }
end

#show_compact_branches(argv) ⇒ Object

以紧凑模式执行



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

def show_compact_branches(argv)
  show_branches_for_repos(all_repos, false)
  show_branches_for_repos(locked_repos, true)
  Output.puts_succeed_cmd(argv.absolute_cmd)
end