Module: DevContext::Commands::GitOps

Included in:
DevContext::CLI
Defined in:
lib/dev_context/commands/git_ops.rb

Instance Method Summary collapse

Instance Method Details

#cmd_activeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dev_context/commands/git_ops.rb', line 6

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_branchesObject



105
106
107
108
109
110
111
112
113
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
# File 'lib/dev_context/commands/git_ops.rb', line 105

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_checkoutObject



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
# File 'lib/dev_context/commands/git_ops.rb', line 155

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_deactivateObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dev_context/commands/git_ops.rb', line 20

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_diffObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dev_context/commands/git_ops.rb', line 88

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_popObject



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
# File 'lib/dev_context/commands/git_ops.rb', line 55

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

    out.puts("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)
    out.puts("Popped #{context.fetch('name')}")
  end

  new_top = config.active_contexts.first
  return 0 unless new_top

  out.puts("cd #{Shellwords.escape(new_top.fetch('repo_path'))}")
  0
end

#cmd_pushObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dev_context/commands/git_ops.rb', line 32

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"))
  ).activation_script
  out.write(script)
  0
end