Module: DevContext::Commands::GitOps
- Included in:
- DevContext::CLI
- Defined in:
- lib/dev_context/commands/git_ops.rb
Instance Method Summary collapse
- #cmd_active ⇒ Object
- #cmd_branches ⇒ Object
- #cmd_checkout ⇒ Object
- #cmd_deactivate ⇒ Object
- #cmd_diff ⇒ Object
- #cmd_import(emit_script: true) ⇒ Object
- #cmd_pop ⇒ Object
- #cmd_push ⇒ Object
Instance Method Details
#cmd_active ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dev_context/commands/git_ops.rb', line 17 def cmd_active active = config.active_contexts if active.empty? out.puts("No active contexts") return 0 end active.each do |ctx| status = Status.new(repo_path: ctx.fetch("repo_path")).one_line out.puts("#{ctx.fetch("name")} #{ctx.fetch("repo_path")} #{ctx.fetch("branch")} #{status}") end 0 end |
#cmd_branches ⇒ Object
114 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dev_context/commands/git_ops.rb', line 114 def cmd_branches target = argv.shift return usage_error("dx branches|br [CONTEXT|PATTERN]") unless argv.empty? if target context = matcher.resolve(target) if context contexts = [context] contexts.each do |ctx| path = ctx.fetch("repo_path") out.puts("# #{ctx.fetch('name')} (#{path})") out.print(`git -C "#{path}" branch --list`) end return 0 end rows = known_repo_rows.filter_map do |repo| next unless pattern_match?(repo[:branch], target) repo end.sort_by { |row| [row[:branch], row[:path]] } if rows.empty? out.puts("No matching branches") return 0 end display_rows = rows.map { |r| r.merge(path: display_path(r[:path])) } path_w = [display_rows.map { |r| r[:path].length }.max || 0, "Repo".length].max branch_w = [display_rows.map { |r| r[:branch].length }.max || 0, "Branch".length].max out.puts(format("%-#{path_w}s %-#{branch_w}s", "Repo", "Branch")) out.puts(format("%-#{path_w}s %-#{branch_w}s", "-" * path_w, "-" * branch_w)) display_rows.each { |row| out.puts(format("%-#{path_w}s %-#{branch_w}s", row[:path], row[:branch])) } return 0 end contexts = config.active_contexts if contexts.empty? out.puts("No active contexts") return 0 end contexts.each do |ctx| path = ctx.fetch("repo_path") out.puts("# #{ctx.fetch('name')} (#{path})") out.print(`git -C "#{path}" branch --list`) end 0 end |
#cmd_checkout ⇒ Object
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 |
# File 'lib/dev_context/commands/git_ops.rb', line 164 def cmd_checkout args = argv.dup create_flag = false if args.first == "-b" create_flag = true args.shift end feature = args.shift target = args.shift return usage_error("dx co|checkout [-b] FEATURE [CONTEXT]") if blank?(feature) context = if target matcher.resolve(target) else config.active_contexts.first end return not_found(target || "active context") unless context cmd = create_flag ? %(git -C "#{context.fetch('repo_path')}" checkout -b #{Shellwords.escape(feature)} 2>&1) : %(git -C "#{context.fetch('repo_path')}" checkout #{Shellwords.escape(feature)} 2>&1) output = `#{cmd}` unless $?.success? err.puts(output) return 28 end context_name = context.fetch("name") config.contexts[context_name]["branch"] = feature config.send(:save!) out.puts("Checked out #{feature} in #{context_name}") 0 end |
#cmd_deactivate ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dev_context/commands/git_ops.rb', line 31 def cmd_deactivate target = argv.shift return usage_error("dx deactivate CONTEXT") if blank?(target) context = matcher.resolve(target) return not_found(target) unless context config.deactivate_context!(context.fetch("name")) out.puts("Deactivated #{context.fetch("name")}") 0 end |
#cmd_diff ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dev_context/commands/git_ops.rb', line 97 def cmd_diff target = argv.shift contexts = target ? [matcher.resolve(target)].compact : config.active_contexts return not_found(target) if target && contexts.empty? if contexts.empty? out.puts("No active contexts") return 0 end contexts.each do |ctx| path = ctx.fetch("repo_path") out.puts("# #{ctx.fetch('name')} (#{path})") out.print(`git -C "#{path}" diff`) end 0 end |
#cmd_import(emit_script: true) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/dev_context/commands/git_ops.rb', line 6 def cmd_import(emit_script: true) imported_paths = parsed_shell_stack_paths imported_contexts = import_contexts_for_paths(imported_paths) config.active_stack.replace(imported_contexts.map { |ctx| ctx.fetch("name") }) config.send(:save!) return 0 unless emit_script out.write(stack_sync_script_for_active_contexts) 0 end |
#cmd_pop ⇒ Object
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 |
# File 'lib/dev_context/commands/git_ops.rb', line 67 def cmd_pop target = argv.shift return usage_error("dx popd [CONTEXT|+N|-N]") unless argv.empty? return pop_by_stack_index("+0") if target.nil? return pop_by_stack_index(target) if target.match?(/\A[+-]\d+\z/) if target context = matcher.resolve(target) return not_found(target) unless context removed = config.deactivate_context!(context.fetch("name")) return not_found(target) unless removed ("Popped #{context.fetch('name')}") else top_name = config.active_stack.first if top_name.nil? out.puts("No active contexts") return 0 end context = config.contexts.fetch(top_name) config.deactivate_context!(top_name) ("Popped #{context.fetch('name')}") end out.write(stack_sync_script_for_active_contexts) 0 end |
#cmd_push ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dev_context/commands/git_ops.rb', line 43 def cmd_push target = argv.shift return usage_error("dx pushd [CONTEXT|+N|-N]") unless argv.empty? if target&.match?(/\A[+-]\d+\z/) return rotate_active_stack_to(target) end return usage_error("dx pushd [CONTEXT|+N|-N]") if blank?(target) context, code = resolve_or_create_context(target) return code unless code.zero? config.activate_context!(context.fetch("name")) script = ShellEmitter.new( context: context, remote_name: env.fetch("DX_GIT_REMOTE_NAME", "USE-REPO"), auto_create_local_branch: truthy?(env.fetch("DX_AUTO_CREATE_LOCAL_BRANCH", "true")), stack_paths: config.active_contexts.map { |ctx| ctx.fetch("repo_path") } ).activation_script out.write(script) 0 end |