Class: DevContext::ShellEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_context/shell_emitter.rb

Constant Summary collapse

SCRIPT_MARKER =
"# DX_SHELL_EVAL".freeze

Instance Method Summary collapse

Constructor Details

#initialize(context:, remote_name:, auto_create_local_branch:) ⇒ ShellEmitter

Returns a new instance of ShellEmitter.



9
10
11
12
13
# File 'lib/dev_context/shell_emitter.rb', line 9

def initialize(context:, remote_name:, auto_create_local_branch:)
  @context = context
  @remote_name = remote_name
  @auto_create_local_branch = auto_create_local_branch
end

Instance Method Details

#activation_scriptObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dev_context/shell_emitter.rb', line 15

def activation_script
  repo = Shellwords.escape(context.fetch("repo_path"))
  branch = Shellwords.escape(context.fetch("branch"))
  forced_remote = Shellwords.escape(remote_name)
  auto_create_flag = auto_create_local_branch ? "true" : "false"

  <<~SH
    #{SCRIPT_MARKER}
    __dx_fail() { echo "$1" 1>&2; return "${2:-1}"; }
    cd #{repo} || __dx_fail "dx: could not cd into #{repo}" 11
    git rev-parse --is-inside-work-tree >/dev/null 2>&1 || __dx_fail "dx: not a git repository: #{repo}" 12

    if git show-ref --verify --quiet "refs/heads/#{branch}"; then
      git checkout #{branch} >/dev/null 2>&1 || __dx_fail "dx: checkout failed for #{branch}" 13
    elif git show-ref --verify --quiet "refs/remotes/origin/#{branch}" && [ "#{auto_create_flag}" = "true" ]; then
      git checkout -t "origin/#{branch}" >/dev/null 2>&1 || __dx_fail "dx: could not create local tracking branch #{branch}" 14
    else
      __dx_fail "dx: branch '#{branch}' was not found locally; set DX_AUTO_CREATE_LOCAL_BRANCH=true and ensure origin/#{branch} exists" 15
    fi

    if [ #{forced_remote.inspect} = "USE-REPO" ]; then
      upstream="$(git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' 2>/dev/null || true)"
      if [ -n "$upstream" ]; then
        remote="${upstream%%/*}"
        upstream_branch="${upstream#*/}"
        git pull --ff-only --autostash "$remote" "$upstream_branch" || __dx_fail "dx: pull failed for upstream $upstream (you are already in the target repo for investigation)" 16
      else
        remote_count="$(git remote | wc -l | tr -d ' ')"
        if [ "$remote_count" = "1" ]; then
          remote="$(git remote | head -n1)"
          git pull --ff-only --autostash "$remote" #{branch} || __dx_fail "dx: pull failed from $remote/#{branch} (you are already in the target repo for investigation)" 17
        else
          __dx_fail "dx: no upstream is configured and remote is ambiguous; configure upstream or set DX_GIT_REMOTE_NAME" 18
        fi
      fi
    else
      git remote get-url #{forced_remote} >/dev/null 2>&1 || __dx_fail "dx: remote '#{forced_remote}' does not exist in this repo" 19
      git pull --ff-only --autostash #{forced_remote} #{branch} || __dx_fail "dx: pull failed from #{forced_remote}/#{branch} (you are already in the target repo for investigation)" 20
    fi
  SH
end