Class: Zomgit::Commands::StatusCommand
- Inherits:
-
BasicCommand
- Object
- BasicCommand
- Zomgit::Commands::StatusCommand
- Includes:
- Helpers::FileHelper, Helpers::RainbowHelper
- Defined in:
- lib/zomgit/commands/status.rb
Constant Summary collapse
- DESCRIPTION =
"Show the status of the git repo"- FLAGS =
{ i(filter f) => { arg_name: "filter", desc: "Status filter", must_match: %w(staged unmerged unstaged untracked) } }
- MAX_CHANGES =
150- COLORS =
{ reset: [:white, { bold: false }], deleted: [:red, { bold: false }], modified: [:green, { bold: false }], added: [:yellow, { bold: false }], renamed: [:blue, { bold: false }], copied: [:yellow, { bold: false }], retyped: [:purple, { bold: false }], untracked: [:cyan, { bold: false }], dark: [:black, { bold: true }], branch: [:gray, { bold: true }], header: [:white, { bold: false }] }
- GROUPS =
{ staged: { color: :yellow, message: "Changes to be committed" }, unmerged: { color: :red, message: "Unmerged paths" }, unstaged: { color: :green, message: "Changes not staged for commit" }, untracked: { color: :cyan, message: "Untracked files" } }
Constants included from Helpers::RainbowHelper
Helpers::RainbowHelper::COLOR_CODES, Helpers::RainbowHelper::COLOR_CODE_PREFIX, Helpers::RainbowHelper::COLOR_CODE_SUFFIX
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
Attributes inherited from BasicCommand
Instance Method Summary collapse
- #changes_for(status) ⇒ Object
- #has_dirty_module! ⇒ Object
- #has_dirty_module? ⇒ Boolean
- #has_filter? ⇒ Boolean
- #has_modules? ⇒ Boolean
- #index!(changes, persist: true) ⇒ Object
-
#initialize(arguments = [], options = {}) ⇒ StatusCommand
constructor
A new instance of StatusCommand.
- #long_status ⇒ Object
- #max_changes ⇒ Object
- #output_for(group, changes) ⇒ Object
- #show ⇒ Object
- #stats_for(status) ⇒ Object
Methods included from Helpers::RainbowHelper
Methods included from Helpers::FileHelper
Constructor Details
#initialize(arguments = [], options = {}) ⇒ StatusCommand
Returns a new instance of StatusCommand.
41 42 43 44 45 46 |
# File 'lib/zomgit/commands/status.rb', line 41 def initialize(arguments = [], = {}) super arguments, @filter = [:filter].to_sym if [:filter] @index = 0 end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
7 8 9 |
# File 'lib/zomgit/commands/status.rb', line 7 def files @files end |
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
7 8 9 |
# File 'lib/zomgit/commands/status.rb', line 7 def filter @filter end |
Instance Method Details
#changes_for(status) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/zomgit/commands/status.rb', line 154 def changes_for(status) changes = { staged: [], unmerged: [], unstaged: [], untracked: [] } modules = self.has_modules? ? File.read(File.join(Zomgit::project_root, ".gitmodules")) : "" status.each do |raw_change| change = { left: raw_change[0], right: raw_change[1], file: raw_change[3..-1] } if !self.has_dirty_module? && modules.include?(change[:file]) self.has_dirty_module! end case raw_change[0..1] when "DD"; changes[:unmerged] << { message: " both deleted", color: :deleted, file: change[:file] } when "AU"; changes[:unmerged] << { message: " added by us", color: :added, file: change[:file] } when "UD"; changes[:unmerged] << { message: "deleted by them", color: :deleted, file: change[:file] } when "UA"; changes[:unmerged] << { message: " added by them", color: :added, file: change[:file] } when "DU"; changes[:unmerged] << { message: " deleted by us", color: :deleted, file: change[:file] } when "AA"; changes[:unmerged] << { message: " both added", color: :added, file: change[:file] } when "UU"; changes[:unmerged] << { message: " both modified", color: :modified, file: change[:file] } when /M./; changes[:staged] << { message: " modified", color: :modified, file: change[:file] } when /A./; changes[:staged] << { message: " new file", color: :added, file: change[:file] } when /D./; changes[:staged] << { message: " deleted", color: :deleted, file: change[:file] } when /R./; changes[:staged] << { message: " renamed", color: :renamed, file: change[:file] } when /C./; changes[:staged] << { message: " copied", color: :copied, file: change[:file] } when /T./; changes[:staged] << { message: "typechange", color: :retyped, file: change[:file] } when "??"; changes[:untracked] << { message: " untracked", color: :untracked, file: change[:file] } end if change[:right] == "M" changes[:unstaged] << { message: " modified", color: :modified, file: change[:file] } elsif change[:right] == "D" && change[:left] != "D" && change[:left] != "U" changes[:unstaged] << { message: " deleted", color: :deleted, file: change[:file] } elsif change[:right] == "T" changes[:unstaged] << { message: "typechange", color: :retyped, file: change[:file] } end end changes end |
#has_dirty_module! ⇒ Object
134 135 136 |
# File 'lib/zomgit/commands/status.rb', line 134 def has_dirty_module! @has_dirty_module = true end |
#has_dirty_module? ⇒ Boolean
130 131 132 |
# File 'lib/zomgit/commands/status.rb', line 130 def has_dirty_module? !!@has_dirty_module end |
#has_filter? ⇒ Boolean
138 139 140 |
# File 'lib/zomgit/commands/status.rb', line 138 def has_filter? !self.filter.nil? end |
#has_modules? ⇒ Boolean
126 127 128 |
# File 'lib/zomgit/commands/status.rb', line 126 def has_modules? @has_modules ||= File.exists?(File.join(Zomgit::project_root, ".gitmodules")) end |
#index!(changes, persist: true) ⇒ Object
241 242 243 244 245 |
# File 'lib/zomgit/commands/status.rb', line 241 def index!(changes, persist: true) @files = changes.values.flatten.map { |c| c[:file] } Zomgit::Persistor.instance.cache_index(@files) if persist end |
#long_status ⇒ Object
142 143 144 |
# File 'lib/zomgit/commands/status.rb', line 142 def long_status @long_status ||= `command git status` end |
#max_changes ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/zomgit/commands/status.rb', line 117 def max_changes unless @max_changes max = ENV["ZOMGIT_STATUS_MAX_CHANGES"].to_i @max_changes = max > 0 ? max : MAX_CHANGES end @max_changes end |
#output_for(group, changes) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/zomgit/commands/status.rb', line 200 def output_for(group, changes) output = "" output << send("#{group}_group_color", "\u27A4".encode("utf-8"), bold: true) output << header_color(" #{GROUPS[group][:message]}\n") output << send("#{group}_group_color", "#") output << "\n" changes.each do |change| relative_file = relative_path(Dir.pwd, File.join(Zomgit::project_root, change[:file])) submodule_change = nil if self.has_dirty_module? submodule_change = self.long_status[/#{change[:file]} \((.*)\)/, 1] unless submodule_change.nil? submodule_change = "(#{submodule_change})" end end output << send("#{group}_group_color", "# ") output << send("#{change[:color]}_color", change[:message]) output << ": " index = self.files.index(change[:file]) + 1 padding = "" padding << " " if changes.count >= 10 && index < 10 padding << " " if changes.count >= 100 && index < 100 output << "[#{padding}#{index}] " output << send("#{group}_group_color", relative_file) output << " #{submodule_change}\n" end output << send("#{group}_group_color", "#") output << "\n" output end |
#show ⇒ Object
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 |
# File 'lib/zomgit/commands/status.rb', line 48 def show status = `command git status --porcelain`.split("\n") if status.count > self.max_changes raise Zomgit::Exceptions::TooManyChangesError.new("Too many changes") end git_branch_output = `command git branch -v 2> /dev/null` branch = git_branch_output[/^\* (\(no branch\)|[^ ]*)/, 1] ahead = git_branch_output[/^\* [^ ]* *[^ ]* *\[ahead ?(\d+).*\]/, 1] behind = git_branch_output[/^\* [^ ]* *[^ ]* *\[.*behind ?(\d+)\]/, 1] difference = ["-#{behind}", "+#{ahead}"].select{ |diff| diff.length > 1 }.join("/") if difference.length > 0 diff = "" diff << dark_color(" | ") diff << added_color(difference) difference = diff else difference = "" end output = "" output << dark_color("#") output << " On branch: " output << branch_color(branch) output << difference output << dark_color(" | ") if status.empty? output << modified_color("No changes (working directory clean)") else output << self.stats_for(status).gsub(/(\d+)/, modified_color('\1')) output << dark_color("\n#\n") changes = self.changes_for status if self.has_filter? && GROUPS.has_key?(self.filter) if changes[self.filter].empty? raise Zomgit::Exceptions::NoChangesError.new("No changes matching this filter") else self.index! changes[self.filter] output << self.output_for(self.filter, changes[self.filter]) end else self.index! changes GROUPS.keys.each { |g| output << self.output_for(g, changes[g]) unless changes[g].empty? } end end output end |
#stats_for(status) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/zomgit/commands/status.rb', line 146 def stats_for(status) stats = "#{status.count} changes (" staged = status.grep(/\A[^ ?]/).count unstaged = status.grep(/\A[ ?]/).count stats << "#{staged} staged, #{unstaged} unstaged" stats << ")" end |