space-cadet 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +279 -0
- data/exe/space +13 -0
- data/lib/space_core/atomic_write.rb +21 -0
- data/lib/space_core/cli/base_command.rb +19 -0
- data/lib/space_core/cli/build.rb +27 -0
- data/lib/space_core/cli/config.rb +49 -0
- data/lib/space_core/cli/current.rb +16 -0
- data/lib/space_core/cli/help.rb +195 -0
- data/lib/space_core/cli/helpers.rb +115 -0
- data/lib/space_core/cli/init.rb +29 -0
- data/lib/space_core/cli/list.rb +24 -0
- data/lib/space_core/cli/loop_status.rb +47 -0
- data/lib/space_core/cli/new.rb +38 -0
- data/lib/space_core/cli/pack.rb +29 -0
- data/lib/space_core/cli/path.rb +16 -0
- data/lib/space_core/cli/repeatable_options.rb +84 -0
- data/lib/space_core/cli/repo.rb +76 -0
- data/lib/space_core/cli/run.rb +44 -0
- data/lib/space_core/cli/shell.rb +125 -0
- data/lib/space_core/cli/show.rb +21 -0
- data/lib/space_core/cli/status.rb +74 -0
- data/lib/space_core/cli/use.rb +17 -0
- data/lib/space_core/cli.rb +177 -0
- data/lib/space_core/cloner.rb +77 -0
- data/lib/space_core/commands.rb +51 -0
- data/lib/space_core/config.rb +152 -0
- data/lib/space_core/errors.rb +14 -0
- data/lib/space_core/git_client.rb +49 -0
- data/lib/space_core/mise_client.rb +37 -0
- data/lib/space_core/oci_builder.rb +56 -0
- data/lib/space_core/oci_packer.rb +103 -0
- data/lib/space_core/oci_runner.rb +89 -0
- data/lib/space_core/paths.rb +58 -0
- data/lib/space_core/repo_reference.rb +19 -0
- data/lib/space_core/repo_resolver.rb +167 -0
- data/lib/space_core/scm/client.rb +87 -0
- data/lib/space_core/scm/git.rb +232 -0
- data/lib/space_core/scm/status.rb +24 -0
- data/lib/space_core/shell.rb +90 -0
- data/lib/space_core/shell_integration.rb +438 -0
- data/lib/space_core/slugger.rb +16 -0
- data/lib/space_core/space.rb +174 -0
- data/lib/space_core/space_store.rb +319 -0
- data/lib/space_core/state.rb +86 -0
- data/lib/space_core/templates/oci/dockerfile.erb +73 -0
- data/lib/space_core/templates/oci/dockerignore.erb +17 -0
- data/lib/space_core/templates/oci/entrypoint.sh.erb +16 -0
- data/lib/space_core/terminal.rb +161 -0
- data/lib/space_core/version.rb +7 -0
- data/lib/space_core/warnings.rb +13 -0
- data/lib/space_core/xdg.rb +33 -0
- data/lib/space_core.rb +26 -0
- metadata +199 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Space::Core
|
|
6
|
+
class RepoResolver
|
|
7
|
+
SCP_LIKE_PATTERN = /\A(?:[^@\/]+@)?(?<provider>[^:\/]+):(?<path>.+)\z/
|
|
8
|
+
URL_PATTERN = %r{\A[A-Za-z][A-Za-z0-9+\-.]*://}
|
|
9
|
+
|
|
10
|
+
attr_reader :config
|
|
11
|
+
|
|
12
|
+
def initialize(config)
|
|
13
|
+
@config = config
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def resolve(spec)
|
|
17
|
+
value = spec.to_s.strip
|
|
18
|
+
raise RepoResolutionError, "Repo cannot be blank" if value.empty?
|
|
19
|
+
|
|
20
|
+
if url_like?(value)
|
|
21
|
+
resolve_url(value)
|
|
22
|
+
elsif (match = value.match(SCP_LIKE_PATTERN))
|
|
23
|
+
reference_from_parts(
|
|
24
|
+
provider: match[:provider],
|
|
25
|
+
path_parts: split_repo_path(match[:path]),
|
|
26
|
+
clone_url: value,
|
|
27
|
+
source: value
|
|
28
|
+
)
|
|
29
|
+
else
|
|
30
|
+
resolve_shorthand(value)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def resolve_url(value)
|
|
37
|
+
uri = URI.parse(value)
|
|
38
|
+
provider = uri.host
|
|
39
|
+
path = uri.path.to_s.delete_prefix("/")
|
|
40
|
+
raise RepoResolutionError, "Could not determine provider from '#{value}'" if provider.to_s.empty?
|
|
41
|
+
|
|
42
|
+
reference_from_parts(
|
|
43
|
+
provider: provider,
|
|
44
|
+
path_parts: split_repo_path(path),
|
|
45
|
+
clone_url: value,
|
|
46
|
+
source: value
|
|
47
|
+
)
|
|
48
|
+
rescue URI::InvalidURIError
|
|
49
|
+
raise RepoResolutionError, "Could not parse repo URL '#{value}'"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def resolve_shorthand(value)
|
|
53
|
+
parts = split_repo_path(value)
|
|
54
|
+
|
|
55
|
+
if parts.length == 1
|
|
56
|
+
resolve_default_organization_repo(parts.first, value)
|
|
57
|
+
elsif provider_like?(parts.first) && parts.length >= 3
|
|
58
|
+
reference_from_parts(
|
|
59
|
+
provider: parts.first,
|
|
60
|
+
path_parts: parts[1..],
|
|
61
|
+
clone_url: nil,
|
|
62
|
+
source: value
|
|
63
|
+
)
|
|
64
|
+
else
|
|
65
|
+
resolve_default_provider_repo(parts, value)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def resolve_default_organization_repo(name, source)
|
|
70
|
+
provider = require_default_provider(source)
|
|
71
|
+
owner = config.default_organization
|
|
72
|
+
unless owner
|
|
73
|
+
raise RepoResolutionError,
|
|
74
|
+
"Repo '#{source}' needs an organization. Set one with: space config set default_organization ORG"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
reference(provider:, owner:, name:, clone_url: nil, source:)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def resolve_default_provider_repo(parts, source)
|
|
81
|
+
provider = require_default_provider(source)
|
|
82
|
+
reference_from_parts(provider:, path_parts: parts, clone_url: nil, source:)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def reference_from_parts(provider:, path_parts:, clone_url:, source:)
|
|
86
|
+
if path_parts.length < 2
|
|
87
|
+
raise RepoResolutionError, "Repo '#{source}' must include an organization and repo name"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
name = path_parts.last
|
|
91
|
+
owner = path_parts[0...-1].join("/")
|
|
92
|
+
reference(provider:, owner:, name:, clone_url:, source:)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def reference(provider:, owner:, name:, clone_url:, source:)
|
|
96
|
+
normalized_provider = normalize_provider(provider)
|
|
97
|
+
normalized_owner = normalize_path_part(owner)
|
|
98
|
+
normalized_name = normalize_repo_name(name)
|
|
99
|
+
|
|
100
|
+
RepoReference.new(
|
|
101
|
+
provider: normalized_provider,
|
|
102
|
+
owner: normalized_owner,
|
|
103
|
+
name: normalized_name,
|
|
104
|
+
clone_url: clone_url || clone_url_for(normalized_provider, normalized_owner, normalized_name),
|
|
105
|
+
source: source
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def clone_url_for(provider, owner, name)
|
|
110
|
+
case config.git_clone_protocol
|
|
111
|
+
when "ssh"
|
|
112
|
+
"git@#{provider}:#{owner}/#{name}.git"
|
|
113
|
+
when "https"
|
|
114
|
+
"https://#{provider}/#{owner}/#{name}.git"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def split_repo_path(value)
|
|
119
|
+
normalized = value.to_s.strip.delete_prefix("/").delete_suffix("/")
|
|
120
|
+
normalized = normalized.delete_suffix(".git")
|
|
121
|
+
parts = normalized.split("/").reject(&:empty?)
|
|
122
|
+
raise RepoResolutionError, "Repo '#{value}' must include a repo name" if parts.empty?
|
|
123
|
+
|
|
124
|
+
parts
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def require_default_provider(source)
|
|
128
|
+
config.default_provider || raise(
|
|
129
|
+
RepoResolutionError,
|
|
130
|
+
"Repo '#{source}' needs a provider. Set one with: space config set default_provider PROVIDER"
|
|
131
|
+
)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def normalize_provider(value)
|
|
135
|
+
normalized = value.to_s.strip
|
|
136
|
+
normalized = normalized.delete_prefix("https://")
|
|
137
|
+
normalized = normalized.delete_prefix("http://")
|
|
138
|
+
normalized = normalized.delete_prefix("ssh://")
|
|
139
|
+
normalized = normalized.delete_suffix("/")
|
|
140
|
+
raise RepoResolutionError, "Provider cannot be blank" if normalized.empty?
|
|
141
|
+
|
|
142
|
+
normalized
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def normalize_path_part(value)
|
|
146
|
+
normalized = value.to_s.strip.delete_prefix("/").delete_suffix("/")
|
|
147
|
+
raise RepoResolutionError, "Organization cannot be blank" if normalized.empty?
|
|
148
|
+
|
|
149
|
+
normalized
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def normalize_repo_name(value)
|
|
153
|
+
normalized = value.to_s.strip.delete_suffix(".git")
|
|
154
|
+
raise RepoResolutionError, "Repo name cannot be blank" if normalized.empty?
|
|
155
|
+
|
|
156
|
+
normalized
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def url_like?(value)
|
|
160
|
+
value.match?(URL_PATTERN)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def provider_like?(value)
|
|
164
|
+
value.include?(".") || value.include?(":") || value == "localhost"
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/monads"
|
|
4
|
+
|
|
5
|
+
module Space::Core
|
|
6
|
+
module SCM
|
|
7
|
+
# Abstract SCM interface. The git CLI is the only implementation for
|
|
8
|
+
# now (per AGENTS.md / PRD §1), but the sync engine + tests must
|
|
9
|
+
# code to this interface so a future SCM is a drop-in.
|
|
10
|
+
#
|
|
11
|
+
# Every method returns a Dry::Monads::Result. Programmer error
|
|
12
|
+
# (e.g. nil path) is a raise; expected failures (no .git, non-fast-
|
|
13
|
+
# forward, network down) are Failure.
|
|
14
|
+
class Client
|
|
15
|
+
extend Dry::Monads[:result]
|
|
16
|
+
|
|
17
|
+
# Parse the working-tree status of a repo on disk. Implementation
|
|
18
|
+
# must read porcelain v2 (per AGENTS.md gotcha) and report a
|
|
19
|
+
# `SCM::Status` value object.
|
|
20
|
+
def status(path)
|
|
21
|
+
raise NotImplementedError
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Returns the current branch name, or nil if HEAD is detached.
|
|
25
|
+
def current_branch(path)
|
|
26
|
+
raise NotImplementedError
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Returns the bare remote's HEAD (e.g. "main" or "trunk"). Must
|
|
30
|
+
# work even when the default branch is not "main". May do a
|
|
31
|
+
# one-shot network call (`git remote set-head origin -a`) to
|
|
32
|
+
# refresh a stale `origin/HEAD`.
|
|
33
|
+
def default_branch(path)
|
|
34
|
+
raise NotImplementedError
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns the mtime of .git/FETCH_HEAD, or nil if absent. Treated
|
|
38
|
+
# as a freshness hint (PRD §3.3 step 4 + gate G5).
|
|
39
|
+
def last_fetch_at(path)
|
|
40
|
+
raise NotImplementedError
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# `git fetch --prune --no-tags origin`.
|
|
44
|
+
def fetch(path)
|
|
45
|
+
raise NotImplementedError
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# `git merge --ff-only origin/<default>`. Returns Failure if the
|
|
49
|
+
# local branch has diverged (left count > 0) — never resets.
|
|
50
|
+
# On Success: returns Integer commit count pulled.
|
|
51
|
+
# 0 → already up to date (no merge performed)
|
|
52
|
+
# N → fast-forwarded N commits
|
|
53
|
+
def fast_forward(path, default_branch)
|
|
54
|
+
raise NotImplementedError
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# `git clone <url> <path>`.
|
|
58
|
+
def clone(url, path)
|
|
59
|
+
raise NotImplementedError
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# `git switch <branch>`. Switches the local repo to the given
|
|
63
|
+
# branch. By default `git switch` refuses to clobber a dirty
|
|
64
|
+
# working tree (the operation is aborted on local-change loss
|
|
65
|
+
# per `man git-switch`); the engine treats that as a Failure.
|
|
66
|
+
# The engine is responsible for the upstream dirty-tree guard
|
|
67
|
+
# (per Slice 2 gate G5 / PHASE-0 ruling): the plan returns
|
|
68
|
+
# `:report_wrong_branch` / `:report_detached` for a dirty tree,
|
|
69
|
+
# so this method is only called on clean trees.
|
|
70
|
+
def switch(path, branch)
|
|
71
|
+
raise NotImplementedError
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Sync an unborn (empty) local clone. Called when the repo has no
|
|
75
|
+
# commits (`status.unborn? == true`) and the working tree is clean.
|
|
76
|
+
# Returns Success(:empty) when the remote has no branches (valid
|
|
77
|
+
# empty clone; no mutation). Returns Success(:fast_forwarded) when
|
|
78
|
+
# the remote has gained commits and the local clone was advanced to
|
|
79
|
+
# them. Returns Failure on a real network/probe error (the
|
|
80
|
+
# empty-vs-error distinction is made via `git ls-remote --heads
|
|
81
|
+
# origin`: exit 0 = definitive answer; non-zero = real error).
|
|
82
|
+
def sync_empty(path)
|
|
83
|
+
raise NotImplementedError
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "time"
|
|
5
|
+
require "space_core/scm/client"
|
|
6
|
+
require "space_core/scm/status"
|
|
7
|
+
require "space_core/shell"
|
|
8
|
+
|
|
9
|
+
module Space::Core
|
|
10
|
+
module SCM
|
|
11
|
+
# Git CLI implementation of SCM::Client. All subprocess work is
|
|
12
|
+
# delegated to Shell.run (which requires an ambient Async::Task).
|
|
13
|
+
class Git < Client
|
|
14
|
+
# Resolve the bare remote's HEAD. First try the local
|
|
15
|
+
# `origin/HEAD` symbolic ref; if missing/stale, do a one-shot
|
|
16
|
+
# `git remote set-head origin -a` (network) to refresh it, then
|
|
17
|
+
# re-read. This is the gotcha path from AGENTS.md: a plain
|
|
18
|
+
# `git fetch` does NOT update `origin/HEAD`.
|
|
19
|
+
def default_branch(path)
|
|
20
|
+
symbolic = read_origin_head(path)
|
|
21
|
+
return Dry::Monads::Success(symbolic) if symbolic
|
|
22
|
+
|
|
23
|
+
refresh = Shell.run("git", "remote", "set-head", "origin", "-a", chdir: path)
|
|
24
|
+
return refresh if refresh.failure?
|
|
25
|
+
|
|
26
|
+
resolved = read_origin_head(path)
|
|
27
|
+
if resolved
|
|
28
|
+
Dry::Monads::Success(resolved)
|
|
29
|
+
else
|
|
30
|
+
Dry::Monads::Failure({path: path, reason: "could not resolve origin/HEAD after set-head -a"})
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def current_branch(path)
|
|
35
|
+
# `git symbolic-ref --short HEAD` exits non-zero on detached HEAD.
|
|
36
|
+
result = Shell.run("git", "symbolic-ref", "--short", "HEAD", chdir: path)
|
|
37
|
+
if result.success?
|
|
38
|
+
Dry::Monads::Success(result.success.strip)
|
|
39
|
+
else
|
|
40
|
+
# Detached HEAD is not a hard failure — report nil.
|
|
41
|
+
head = Shell.run("git", "rev-parse", "--verify", "HEAD", chdir: path)
|
|
42
|
+
if head.success?
|
|
43
|
+
Dry::Monads::Success(nil)
|
|
44
|
+
else
|
|
45
|
+
Dry::Monads::Failure({path: path, reason: "no HEAD", stderr: result.failure[:stderr]})
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def last_fetch_at(path)
|
|
51
|
+
fetch_head = File.join(path, ".git", "FETCH_HEAD")
|
|
52
|
+
return Dry::Monads::Success(nil) unless File.exist?(fetch_head)
|
|
53
|
+
Dry::Monads::Success(Time.at(File.mtime(fetch_head).to_i))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def fetch(path)
|
|
57
|
+
Shell.run("git", "fetch", "--prune", "--no-tags", "origin", chdir: path)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# `merge --ff-only` refuses on divergence. We additionally check
|
|
61
|
+
# the rev-list left/right count first so we can surface a clean
|
|
62
|
+
# "diverged" failure with diagnostic info, not just a git error
|
|
63
|
+
# string.
|
|
64
|
+
def fast_forward(path, default_branch)
|
|
65
|
+
upstream = "origin/#{default_branch}"
|
|
66
|
+
counts = Shell.run("git", "rev-list", "--left-right", "--count", "HEAD...#{upstream}", chdir: path)
|
|
67
|
+
return counts if counts.failure?
|
|
68
|
+
|
|
69
|
+
left, right = counts.success.strip.split("\t").map(&:to_i)
|
|
70
|
+
if left > 0
|
|
71
|
+
return Dry::Monads::Failure({
|
|
72
|
+
path: path,
|
|
73
|
+
reason: "diverged: local is #{left} commit(s) ahead of #{upstream}; not auto-resolving",
|
|
74
|
+
local_ahead: left,
|
|
75
|
+
remote_ahead: right
|
|
76
|
+
})
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
if right == 0
|
|
80
|
+
return Dry::Monads::Success(0)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Do the fetch (cheap; FETCH_HEAD mtime hint logic can skip this
|
|
84
|
+
# later, but Slice 1 always fetches when asked to fast-forward).
|
|
85
|
+
fetch_result = fetch(path)
|
|
86
|
+
return fetch_result if fetch_result.failure?
|
|
87
|
+
|
|
88
|
+
merge = Shell.run("git", "merge", "--ff-only", upstream, chdir: path)
|
|
89
|
+
if merge.success?
|
|
90
|
+
Dry::Monads::Success(right)
|
|
91
|
+
else
|
|
92
|
+
# On --ff-only failure, git leaves the working tree and local
|
|
93
|
+
# commits intact — the test asserts this.
|
|
94
|
+
Dry::Monads::Failure({
|
|
95
|
+
path: path,
|
|
96
|
+
reason: "fast-forward failed (likely raced divergence)",
|
|
97
|
+
stderr: merge.failure[:stderr]
|
|
98
|
+
})
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def status(path)
|
|
103
|
+
result = Shell.run("git", "status", "--porcelain=v2", "--branch", "--untracked-files=normal", chdir: path)
|
|
104
|
+
return result if result.failure?
|
|
105
|
+
|
|
106
|
+
parsed = parse_porcelain_v2(result.success)
|
|
107
|
+
Dry::Monads::Success(parsed)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def clone(url, dest)
|
|
111
|
+
parent = File.dirname(dest)
|
|
112
|
+
FileUtils.mkdir_p(parent)
|
|
113
|
+
result = Shell.run("git", "clone", url, dest)
|
|
114
|
+
if result.success?
|
|
115
|
+
Dry::Monads::Success(dest)
|
|
116
|
+
else
|
|
117
|
+
Dry::Monads::Failure({url: url, dest: dest, stderr: result.failure[:stderr]})
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# `git switch <branch>`. `git switch` aborts on a dirty tree by
|
|
122
|
+
# default (man git-switch: "The operation is aborted however if
|
|
123
|
+
# the operation leads to loss of local changes"), so a nonzero
|
|
124
|
+
# exit here most likely means the caller violated the engine's
|
|
125
|
+
# dirty-tree guard. We surface that as a Failure with the
|
|
126
|
+
# captured stderr so the engine / log can diagnose it.
|
|
127
|
+
def switch(path, branch)
|
|
128
|
+
result = Shell.run("git", "switch", branch, chdir: path)
|
|
129
|
+
if result.success?
|
|
130
|
+
Dry::Monads::Success(branch)
|
|
131
|
+
else
|
|
132
|
+
Dry::Monads::Failure({path: path, branch: branch, reason: "git switch refused", stderr: result.failure[:stderr]})
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Handle an unborn (empty) local clone. If the remote has no
|
|
137
|
+
# branches, the repo is already a valid empty clone — return
|
|
138
|
+
# Success(:empty) with no mutation. If the remote has gained
|
|
139
|
+
# commits, fetch and fast-forward the unborn branch into them.
|
|
140
|
+
#
|
|
141
|
+
# `git ls-remote --heads origin` is the authoritative
|
|
142
|
+
# empty-vs-error discriminator: exit 0 + empty stdout means the
|
|
143
|
+
# remote truly has no branches; exit 0 + output means it has
|
|
144
|
+
# commits; non-zero exit means a real network/probe error.
|
|
145
|
+
def sync_empty(path)
|
|
146
|
+
ls = Shell.run("git", "ls-remote", "--heads", "origin", chdir: path)
|
|
147
|
+
return ls if ls.failure?
|
|
148
|
+
|
|
149
|
+
return Dry::Monads::Success(:empty) if ls.success.strip.empty?
|
|
150
|
+
|
|
151
|
+
fetch_result = fetch(path)
|
|
152
|
+
return fetch_result if fetch_result.failure?
|
|
153
|
+
|
|
154
|
+
branch_result = default_branch(path)
|
|
155
|
+
return branch_result if branch_result.failure?
|
|
156
|
+
|
|
157
|
+
upstream = "origin/#{branch_result.success}"
|
|
158
|
+
merge = Shell.run("git", "merge", "--ff-only", upstream, chdir: path)
|
|
159
|
+
if merge.success?
|
|
160
|
+
Dry::Monads::Success(:fast_forwarded)
|
|
161
|
+
else
|
|
162
|
+
Dry::Monads::Failure({path: path, reason: "ff merge into unborn branch failed", stderr: merge.failure[:stderr]})
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
private
|
|
167
|
+
|
|
168
|
+
# `git symbolic-ref --short refs/remotes/origin/HEAD` returns
|
|
169
|
+
# `origin/<branch>` (the short form of `refs/remotes/origin/<branch>`);
|
|
170
|
+
# callers want the bare branch name. Returns nil when origin/HEAD
|
|
171
|
+
# is unset.
|
|
172
|
+
def read_origin_head(path)
|
|
173
|
+
result = Shell.run("git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD", chdir: path)
|
|
174
|
+
return nil unless result.success?
|
|
175
|
+
line = result.success.strip
|
|
176
|
+
return nil if line.empty?
|
|
177
|
+
line.sub(%r{\Aorigin/}, "")
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Parse `git status --porcelain=v2 --branch --untracked-files=normal`.
|
|
181
|
+
# v2 grammar (from the git-status man page):
|
|
182
|
+
# # branch.oid <commit-ish> | (initial)
|
|
183
|
+
# # branch.head <name> | (detached)
|
|
184
|
+
# # branch.upstream <upstream-branch>
|
|
185
|
+
# # branch.ab +<ahead> -<behind>
|
|
186
|
+
# 1 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <path>
|
|
187
|
+
# 2 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <X><score> <path><TAB><origPath>
|
|
188
|
+
# u <XY> <sub> <m1> <m2> <m3> <mW> <h1> <h2> <h3> <path>
|
|
189
|
+
# ? <path>
|
|
190
|
+
# ! <path>
|
|
191
|
+
def parse_porcelain_v2(output)
|
|
192
|
+
branch = nil
|
|
193
|
+
upstream = nil
|
|
194
|
+
ahead = 0
|
|
195
|
+
behind = 0
|
|
196
|
+
detached = false
|
|
197
|
+
unborn = false
|
|
198
|
+
entries = []
|
|
199
|
+
|
|
200
|
+
output.each_line do |raw|
|
|
201
|
+
line = raw.chomp
|
|
202
|
+
next if line.empty?
|
|
203
|
+
case line
|
|
204
|
+
when /\A# branch\.oid (.+)/
|
|
205
|
+
unborn = (Regexp.last_match(1) == "(initial)")
|
|
206
|
+
when /\A# branch\.head (.+)/
|
|
207
|
+
branch = Regexp.last_match(1)
|
|
208
|
+
detached = (branch == "(detached)")
|
|
209
|
+
when /\A# branch\.upstream (.+)/
|
|
210
|
+
upstream = Regexp.last_match(1)
|
|
211
|
+
when /\A# branch\.ab \+(\d+) -(\d+)/
|
|
212
|
+
ahead = Regexp.last_match(1).to_i
|
|
213
|
+
behind = Regexp.last_match(2).to_i
|
|
214
|
+
when /\A[12u?!]/
|
|
215
|
+
entries << line
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
Status.new(
|
|
220
|
+
clean: entries.empty?,
|
|
221
|
+
branch: branch,
|
|
222
|
+
upstream: upstream,
|
|
223
|
+
ahead: ahead,
|
|
224
|
+
behind: behind,
|
|
225
|
+
detached: detached,
|
|
226
|
+
entries: entries,
|
|
227
|
+
unborn: unborn
|
|
228
|
+
)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Space::Core
|
|
4
|
+
module SCM
|
|
5
|
+
# Value object produced by parsing `git status --porcelain=v2
|
|
6
|
+
# --branch --untracked-files=normal`. v2 is mandatory (per
|
|
7
|
+
# AGENTS.md gotcha) so submodule state and rename detection are
|
|
8
|
+
# stable.
|
|
9
|
+
#
|
|
10
|
+
# A working tree is "clean" iff the only porcelain-v2 lines are the
|
|
11
|
+
# `# branch.*` header lines. Any `1`/`2`/`u`/`?`/`!` line is dirty.
|
|
12
|
+
Status = Data.define(:clean, :branch, :upstream, :ahead, :behind, :detached, :entries, :unborn) do
|
|
13
|
+
def initialize(clean:, branch: nil, upstream: nil, ahead: 0, behind: 0, detached: false, entries: [], unborn: false)
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def clean? = clean
|
|
18
|
+
|
|
19
|
+
def detached? = detached
|
|
20
|
+
|
|
21
|
+
def unborn? = unborn
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "async"
|
|
5
|
+
require "dry/monads"
|
|
6
|
+
|
|
7
|
+
module Space::Core
|
|
8
|
+
# Thin Open3.capture3 wrapper that:
|
|
9
|
+
# * requires an ambient Async::Task (so subprocess I/O flows through
|
|
10
|
+
# Ruby's Fiber scheduler → kqueue on macOS and is non-blocking);
|
|
11
|
+
# * returns a Dry::Monads::Result — Success(stdout) on zero exit,
|
|
12
|
+
# Failure({argv:, stderr:, status:}) otherwise.
|
|
13
|
+
#
|
|
14
|
+
# Per AGENTS.md: no `async-process`. Per PRD §2: boundaries return
|
|
15
|
+
# Result, exceptions are for programmer error only.
|
|
16
|
+
class Shell
|
|
17
|
+
extend Dry::Monads[:result]
|
|
18
|
+
|
|
19
|
+
@run_count = 0
|
|
20
|
+
@saved_roe = nil
|
|
21
|
+
|
|
22
|
+
def self.run(*argv, chdir: nil, env: nil)
|
|
23
|
+
raise ArgumentError, "Shell.run requires at least argv" if argv.empty?
|
|
24
|
+
raise "Shell.run must be called inside an ambient Async::Task" unless Async::Task.current?
|
|
25
|
+
|
|
26
|
+
full_env = env ? ENV.to_h.merge(env.transform_keys(&:to_s)) : nil
|
|
27
|
+
opts = {}
|
|
28
|
+
opts[:chdir] = chdir if chdir
|
|
29
|
+
# Open3.capture3: env is a leading hash positional arg, not a kwarg.
|
|
30
|
+
#
|
|
31
|
+
# Open3.capture3 spawns the child with two internal reader
|
|
32
|
+
# threads (one for stdout, one for stderr; see
|
|
33
|
+
# `rubylibdir/open3.rb` ~L644: `out_reader = Thread.new { o.read }`
|
|
34
|
+
# / `err_reader = Thread.new { e.read }`). When the `popen3`
|
|
35
|
+
# block exits via exception (e.g. the user ^C'd mid-Shell.run
|
|
36
|
+
# via SIGINT), `popen_run`'s ensure closes the read pipes from
|
|
37
|
+
# the main thread while those reader threads are still inside
|
|
38
|
+
# `o.read` / `e.read`. The mid-read close races with the reader
|
|
39
|
+
# and raises `IOError: stream closed in another thread` in the
|
|
40
|
+
# reader thread. With the default `Thread.report_on_exception
|
|
41
|
+
# = true` (since Ruby 2.5), Ruby prints a multi-line backtrace
|
|
42
|
+
# to stderr for that orphaned thread — exactly the noise
|
|
43
|
+
# Slice 6 G3 silences.
|
|
44
|
+
#
|
|
45
|
+
# We bracket the `Open3.capture3` call with a save/restore of
|
|
46
|
+
# `Thread.report_on_exception = false`. This is targeted
|
|
47
|
+
# because, at this code site, the ONLY threads in flight are:
|
|
48
|
+
# * the main thread (this method's caller);
|
|
49
|
+
# * Async's internal `io_select` thread
|
|
50
|
+
# (`async/lib/async/scheduler.rb` L425) — which silences
|
|
51
|
+
# its own report (`Thread.current.report_on_exception =
|
|
52
|
+
# false` on that thread, not globally);
|
|
53
|
+
# * the Open3 reader threads (the source of the noise).
|
|
54
|
+
# `lib/` has zero `Thread.new` calls; `dry-cli`, `dry-monads`,
|
|
55
|
+
# `dry-validation`, `dry-struct`, `dry-types`, `dry-schema`,
|
|
56
|
+
# `xdg` have none either (verified Slice 6 PHASE 0). So we are
|
|
57
|
+
# NOT hiding any app-owned worker-thread crashes — the only
|
|
58
|
+
# thread that can raise here is the Open3 reader thread, and
|
|
59
|
+
# the only thing it can raise is the IOError we explicitly
|
|
60
|
+
# want to silence. The original value is restored in `ensure`
|
|
61
|
+
# so we never leak the suppression past this call.
|
|
62
|
+
# Refcount the active Shell.run calls so the global flag is suppressed
|
|
63
|
+
# for the entire overlapping window, not just per-fiber. On 0→1: capture
|
|
64
|
+
# original and set false. On 1→0 (in ensure): restore the original.
|
|
65
|
+
# Safe without a Mutex: the reactor is single-threaded; fibers only yield
|
|
66
|
+
# at Open3.capture3's thread-join, never between these plain assignments.
|
|
67
|
+
if @run_count == 0
|
|
68
|
+
@saved_roe = Thread.report_on_exception
|
|
69
|
+
Thread.report_on_exception = false
|
|
70
|
+
end
|
|
71
|
+
@run_count += 1
|
|
72
|
+
begin
|
|
73
|
+
stdout, stderr, status = if full_env
|
|
74
|
+
Open3.capture3(full_env, *argv, **opts)
|
|
75
|
+
else
|
|
76
|
+
Open3.capture3(*argv, **opts)
|
|
77
|
+
end
|
|
78
|
+
ensure
|
|
79
|
+
@run_count -= 1
|
|
80
|
+
Thread.report_on_exception = @saved_roe if @run_count == 0
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
if status.success?
|
|
84
|
+
Success(stdout)
|
|
85
|
+
else
|
|
86
|
+
Failure({argv: argv, stderr: stderr, status: status.exitstatus})
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|