Class: MGit::Stash

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

Constant Summary collapse

OPT_LIST =
{
  :push     => '--push',
  :pop      => '--pop',
  :apply    => '--apply',
  :list     => '--list',
  :clear    => '--clear',
}.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



179
180
181
# File 'lib/m-git/command/stash.rb', line 179

def self.description
  "使用git stash将当前工作区改动暂时存放起来。"
end

.usageObject



183
184
185
# File 'lib/m-git/command/stash.rb', line 183

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

Instance Method Details

#do_clear(argv) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/m-git/command/stash.rb', line 53

def do_clear(argv)
  if Output.continue_with_user_remind?("该操作会丢失所有的stash,确定要执行吗?")
    Output.puts_start_cmd
    abs_cmd = "git stash clear"
    _, error_repos = Workspace.execute_common_cmd_with_repos(abs_cmd, all_repos)
    Output.puts_succeed_cmd(argv.absolute_cmd) if error_repos.length == 0
  else
    Output.puts_cancel_message
  end
end

#do_stash_list(argv) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/m-git/command/stash.rb', line 64

def do_stash_list(argv)
  Output.puts_start_cmd

  error_repos = {}
  all_repos.each { |repo|
    cmd = "git -C \"#{repo.path}\" stash list"
    success, output = repo.execute(cmd)
    if success
      puts Output.generate_title_block(repo.name) {
        output
      } + "\n" if output.length > 0
    else
      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

#do_stash_pop_apply(argv, stash_name, action) ⇒ Object



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
113
# File 'lib/m-git/command/stash.rb', line 86

def do_stash_pop_apply(argv, stash_name, action)
  do_repos = []
  mutex = Mutex.new
  Output.puts_processing_message("检查仓库状态...")
  all_repos.each { |repo|
    stash_list = repo_stash_list_msg(repo)
    next if stash_list.nil?

    stash_id = stash_include_name(stash_list, stash_name)
    next if stash_id.nil?
    mutex.lock
    do_repos.push(repo)
    mutex.unlock
  }
  Output.puts_success_message("检查完成!\n")

  Output.puts_start_cmd
  if do_repos.length == 0
    Output.puts_nothing_to_do_cmd
  else
    _, error_repos = Workspace.execute_common_cmd_with_repos('', do_repos) { |repo|
      stash_list = repo_stash_list_msg(repo)
      stash_id = stash_include_name(stash_list, stash_name)
      "git stash #{action} #{stash_id}"
    }
    Output.puts_succeed_cmd(argv.absolute_cmd) if error_repos.length == 0
  end
end

#do_stash_push(argv, stash_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/m-git/command/stash.rb', line 115

def do_stash_push(argv, stash_name)
  do_repos = []
  remind_repos = []
  mutex = Mutex.new
  Output.puts_processing_message("检查仓库状态...")
  all_repos.each { |repo|
    next if repo.status_checker.status == Repo::Status::GIT_REPO_STATUS[:clean]
    next if repo.status_checker.dirty_zone == Repo::Status::GIT_REPO_STATUS_DIRTY_ZONE[:special]

    stash_list = repo_stash_list_msg(repo)
    stash_id = stash_include_name(stash_list, stash_name)
    mutex.lock
    if stash_id.nil?
      do_repos.push(repo)
    else
      remind_repos.push(repo.name)
    end
    mutex.unlock
  }
  Output.puts_success_message("检查完成!\n")

  if remind_repos.length > 0
    Output.puts_remind_block(remind_repos, "以上仓库当前分支已经存在stash名称:#{stash_name},请换一个名称或者使用\"mgit stash --list\"查看详情。")
    Output.puts_fail_cmd(argv.absolute_cmd)
  elsif do_repos.empty?
    Output.puts_remind_message("所有仓库均是clean状态或者文件未跟踪,无需执行")
  else
    Output.puts_start_cmd
    abs_cmd = "git stash save -u #{stash_name}"
    _, error_repos = Workspace.execute_common_cmd_with_repos(abs_cmd, do_repos)
    Output.puts_succeed_cmd(argv.absolute_cmd) if error_repos.length == 0
  end
end

#enable_repo_selectionObject



175
176
177
# File 'lib/m-git/command/stash.rb', line 175

def enable_repo_selection
  true
end

#execute(argv) ⇒ Object

注意:git stash相关命令不支持指定“working tree”和“git dir” 如:git –git-dir=/path/to/.git –work-tree=/path/to/working-tree stash list,若当前不在working tree目录下,则将无法执行。



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

def execute(argv)
  argv.enumerate_valid_opts { |opt|
    if opt.key == OPT_LIST[:push]
      do_stash_push(argv, opt.value)
      break
    elsif opt.key == OPT_LIST[:pop] || opt.key == OPT_LIST[:apply]
      action = opt.key.gsub('--', '')
      do_stash_pop_apply(argv, opt.value, action)
      break
    elsif opt.key == OPT_LIST[:list]
      do_stash_list(argv)
    elsif opt.key == OPT_LIST[:clear]
      do_clear(argv)
    end
  }
end

#optionsObject



17
18
19
20
21
22
23
24
25
# File 'lib/m-git/command/stash.rb', line 17

def options
  return [
      ARGV::Opt.new(OPT_LIST[:push], info:'添加储藏:mgit stash --push "stash_name"。', type: :string),
      ARGV::Opt.new(OPT_LIST[:pop], info:'恢复储藏:mgit stash --pop "stash_name"。', type: :string),
      ARGV::Opt.new(OPT_LIST[:apply], info:'恢复储藏:mgit stash --apply "stash_name"。', type: :string),
      ARGV::Opt.new(OPT_LIST[:list], info:'显示储藏列表。', type: :boolean),
      ARGV::Opt.new(OPT_LIST[:clear], info:'清空所有储藏。', type: :boolean)
  ].concat(super)
end

#repo_stash_list_msg(repo) ⇒ Object

获取当前的 stash list 字符串,nil,标识当前没有stash



150
151
152
153
# File 'lib/m-git/command/stash.rb', line 150

def repo_stash_list_msg(repo)
  success, output = repo.execute("git -C \"#{repo.path}\" stash list")
  return output if success && output.length > 0
end

#stash_include_name(stash_list, stash_name) ⇒ Object

查询stash_list 是否包含某一个保存的stash 不做分支判断,因为在保存的stashlist中,分支只保留了/之后的内容



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/m-git/command/stash.rb', line 158

def stash_include_name(stash_list, stash_name)
  return if stash_list.nil?

  stash_list_array = stash_list.split("\n")

  find_stash_id = nil
  stash_list_array.each do |line|
    regex = /(stash@{\d+}):.*:\s(.*)$/
    next unless line.match(regex)
    match_stash_name = $2
    next unless match_stash_name == stash_name
    find_stash_id = $1
    break
  end
  find_stash_id
end

#validate(argv) ⇒ Object



27
28
29
30
31
32
# File 'lib/m-git/command/stash.rb', line 27

def validate(argv)
  missing_msg = "缺失必要参数"  if argv.raw_opts.length == 0
  illegal_msg = "输入非法参数:#{argv.git_opts}" if argv.git_opts.length > 0
  conjunction = ",同时" if !missing_msg.nil? && !illegal_msg.nil?
  Foundation.help!("#{missing_msg}#{conjunction}#{illegal_msg},请通过\"mgit #{argv.cmd} --help\"查看用法。") if !missing_msg.nil? || !illegal_msg.nil?
end