Class: MGit::Log

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

Constant Summary collapse

OPT_LIST =
{
  :number    =>  '--number',
  :number_s  =>  '-n'
}.freeze

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/log.rb', line 64

def self.description
  "输出指定(单个)仓库的提交历史。"
end

.usageObject



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

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

Instance Method Details

#enable_short_basic_optionObject



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

def enable_short_basic_option
  true
end

#execute(argv) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/m-git/command/log.rb', line 24

def execute(argv)
  repo_name = parse_repo_name(argv)
  repo = all_repos.find { |e| e.name.downcase == repo_name.downcase }
  number = argv.opt(OPT_LIST[:number]).value
  if repo.nil?
    Output.puts_fail_message("未找到与输入仓库名\"#{repo_name}\"匹配的仓库,请重试!") && return
    return
  end
  # print(Output.processing_message("正在提取#{repo.name}最新的#{number}条log信息..."))
  success, output = repo.execute_git_cmd(argv.cmd, "-n #{number}")
  if success
    if output.length > 0
      Output.puts_in_pager(output.gsub(/commit.*/) { |s| Output.yellow_message(s) })
      # print("\r")
    else
      Output.puts_remind_message("无提交记录")
    end
  else
    Output.puts_fail_message("执行失败:#{output}")
  end
end

#is_integer?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_integer?(string)
  true if Integer(string) rescue false
end

#optionsObject



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

def options
  return [
      ARGV::Opt.new(OPT_LIST[:number], short_key:OPT_LIST[:number_s], default:500, info:"指定需要显示的提交log个数,默认500。", type: :string)
  ].concat(super)
end

#parse_repo_name(argv) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/m-git/command/log.rb', line 46

def parse_repo_name(argv)
  return nil if argv.git_opts.nil?
  repo = argv.git_opts.split(' ')
  extra_opts = repo.select { |e| argv.is_option?(e) }
  Foundation.help!("输入非法参数:#{extra_opts.join('')}。请通过\"mgit #{argv.cmd} --help\"查看用法。") if extra_opts.length > 0
  Foundation.help!("未输入查询仓库名!请使用这种形式查询:mgit log some_repo") if repo.length == 0
  Foundation.help!("仅允许查询一个仓库!") if repo.length > 1
  repo.first
end

#revise_option_value(opt) ⇒ Object



20
21
22
# File 'lib/m-git/command/log.rb', line 20

def revise_option_value(opt)
  opt.value = Integer(opt.value) if opt.key == OPT_LIST[:number]
end