@giamme/forge 0.12.0 → 0.13.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.
- package/CHANGELOG.md +77 -0
- package/README.md +1 -1
- package/SKILL.md +31 -12
- package/VERSION +1 -1
- package/docs/add-ons.md +11 -6
- package/docs/artifacts.md +18 -6
- package/docs/commands.md +189 -17
- package/docs/decomposed-runs.md +13 -5
- package/docs/forge-ui.md +34 -4
- package/docs/memory.md +2 -1
- package/docs/models.md +4 -2
- package/docs/reviews.md +81 -19
- package/docs/troubleshooting.md +3 -1
- package/forge +17 -1
- package/package.json +1 -1
- package/references/dataset.md +40 -14
- package/references/decision-models.md +759 -0
- package/references/decompose.md +197 -28
- package/references/harnesses.md +10 -3
- package/references/planning.md +8 -0
- package/references/solo.md +13 -3
- package/references/ui.md +11 -0
- package/scripts/forge-artifact.sh +30 -4
- package/scripts/forge-config.py +238 -29
- package/scripts/forge-contract.py +127 -58
- package/scripts/forge-control.py +40 -9
- package/scripts/forge-decide.py +6 -0
- package/scripts/forge-dispatch.sh +47 -15
- package/scripts/forge-fail-first.py +2 -2
- package/scripts/forge-gate-addressed.sh +40 -0
- package/scripts/forge-gate-message.sh +64 -0
- package/scripts/forge-issues.py +368 -36
- package/scripts/forge-jev-options.sh +269 -42
- package/scripts/forge-lib-attribute.sh +447 -0
- package/scripts/forge-lib-gates.sh +32 -0
- package/scripts/forge-lib-plans.sh +17 -6
- package/scripts/forge-lib-review.sh +24 -19
- package/scripts/forge-lib-task.sh +219 -39
- package/scripts/forge-lib-verify.sh +45 -8
- package/scripts/forge-memory.sh +3 -3
- package/scripts/forge-parallel-options.sh +44 -5
- package/scripts/forge-parallel.sh +503 -304
- package/scripts/forge-plan-check.py +35 -1
- package/scripts/forge-pre-review.py +437 -0
- package/scripts/forge-prompt.py +25 -5
- package/scripts/forge-route.py +2 -2
- package/scripts/forge-runtime.py +55 -1
- package/scripts/forge-schedule.py +70 -9
- package/scripts/forge-shadow.py +2 -2
- package/scripts/forge-solo.sh +41 -10
- package/scripts/forge-submodules.py +389 -0
- package/scripts/forge-sweep.py +542 -0
- package/scripts/forge_dataset/cli.py +12 -6
- package/scripts/forge_dataset/export.py +55 -45
- package/scripts/forge_dataset/forecast.py +4 -1
- package/scripts/forge_dataset/harvest.py +33 -5
- package/scripts/{forge_jev/outcome.py → forge_dataset/states.py} +21 -7
- package/scripts/forge_decide/__init__.py +141 -0
- package/scripts/forge_decide/budget.py +149 -0
- package/scripts/forge_decide/cache.py +66 -0
- package/scripts/forge_decide/cli.py +953 -0
- package/scripts/forge_decide/doctor.py +221 -0
- package/scripts/forge_decide/label_addressed.py +75 -0
- package/scripts/forge_decide/label_attribution.py +62 -0
- package/scripts/forge_decide/label_coverage.py +81 -0
- package/scripts/forge_decide/label_message.py +38 -0
- package/scripts/forge_decide/label_prompt.py +139 -0
- package/scripts/forge_decide/labels.py +328 -0
- package/scripts/forge_decide/metrics.py +125 -0
- package/scripts/forge_decide/migrate.py +226 -0
- package/scripts/forge_decide/providers.py +292 -0
- package/scripts/forge_decide/replay.py +350 -0
- package/scripts/forge_decide/report.py +382 -0
- package/scripts/forge_decide/resolve.py +397 -0
- package/scripts/forge_decide/sites.py +377 -0
- package/scripts/forge_jev/__init__.py +202 -104
- package/scripts/forge_jev/addressed.py +171 -0
- package/scripts/forge_jev/attribution.py +198 -0
- package/scripts/forge_jev/cli.py +460 -382
- package/scripts/forge_jev/client.py +155 -77
- package/scripts/forge_jev/coupling.py +19 -87
- package/scripts/forge_jev/coverage.py +303 -0
- package/scripts/forge_jev/findings.py +219 -0
- package/scripts/forge_jev/gates.py +225 -27
- package/scripts/forge_jev/memory.py +65 -4
- package/scripts/forge_jev/message.py +134 -0
- package/scripts/forge_jev/plan_gates.py +294 -0
- package/scripts/forge_jev/questions.py +173 -313
- package/scripts/forge_jev/verify.py +130 -104
- package/ui/README.md +41 -10
- package/ui/docs/api.md +212 -44
- package/ui/forge_ui/checks.py +331 -0
- package/ui/forge_ui/control.py +60 -11
- package/ui/forge_ui/export.py +8 -3
- package/ui/forge_ui/models.py +1 -0
- package/ui/forge_ui/reader.py +436 -26
- package/ui/forge_ui/server.py +13 -0
- package/ui/forge_ui/static/app.js +15 -8
- package/ui/forge_ui/static/checksview.js +449 -0
- package/ui/forge_ui/static/controls.js +20 -13
- package/ui/forge_ui/static/convo.js +11 -2
- package/ui/forge_ui/static/findingsview.js +6 -4
- package/ui/forge_ui/static/index.html +1 -0
- package/ui/forge_ui/static/panel.js +105 -17
- package/ui/forge_ui/static/runmodel.js +40 -2
- package/ui/forge_ui/static/scene3d.js +2 -15
- package/ui/forge_ui/static/settings.js +336 -41
- package/ui/forge_ui/static/style.css +72 -7
- package/ui/forge_ui/transcripts.py +225 -54
- package/references/jev.md +0 -865
- package/scripts/forge_jev/backtest.py +0 -322
- package/scripts/forge_jev/corpus.py +0 -156
- package/scripts/forge_jev/features.py +0 -24
- package/scripts/forge_jev/predict.py +0 -204
- package/scripts/forge_jev/review.py +0 -73
- package/scripts/forge_jev/routing.py +0 -271
- package/scripts/forge_jev/subset.py +0 -123
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""forge-submodules.py populate --source <repo> --worktree <wt> --out <file>
|
|
3
|
+
|
|
4
|
+
Give a worktree its submodules' files. `git worktree add` leaves every submodule path (a gitlink,
|
|
5
|
+
mode 160000) an empty directory; this fills each one, offline, from the checkout the worktree was
|
|
6
|
+
made from (<source>), and writes one TSV line per gitlink to <file>:
|
|
7
|
+
|
|
8
|
+
state<TAB>path<TAB>sha<TAB>detail
|
|
9
|
+
|
|
10
|
+
in index order, `sha` being the commit the worktree's index records. The states:
|
|
11
|
+
|
|
12
|
+
exported the source has the submodule's repository (.git/modules/<name>, or an old-style
|
|
13
|
+
<path>/.git) and it holds `sha`: `git archive <sha>` is extracted into the path
|
|
14
|
+
copied otherwise, the source's <path> has files: they are copied from its working tree
|
|
15
|
+
(real copies, never hardlinks; symlinks stay symlinks; any .git is skipped)
|
|
16
|
+
missing neither, or a failure (`detail` says which): the directory stays as it was
|
|
17
|
+
kept the path already holds files (a resumed run): left untouched
|
|
18
|
+
|
|
19
|
+
No `.git` is ever created under a populated path. Files there with no `.git` are invisible to the
|
|
20
|
+
superproject's git: `status` stays clean, `add -A` skips them, `worktree move` and `remove` work,
|
|
21
|
+
so every diff, fingerprint and merge gate forge takes is the same as with the empty directory. A
|
|
22
|
+
real submodule checkout would change all of that.
|
|
23
|
+
|
|
24
|
+
Never uses the network (every git call has protocol.allow=never and no lazy fetch), never runs
|
|
25
|
+
`git submodule`, fetch or clone, never writes any git config. With no gitlink, <file> is not
|
|
26
|
+
written. A failure on one submodule makes it `missing` and the others go on. Exit 0, or 2 on bad
|
|
27
|
+
arguments. Standard library only.
|
|
28
|
+
"""
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import argparse
|
|
32
|
+
import os
|
|
33
|
+
import shutil
|
|
34
|
+
import stat
|
|
35
|
+
import subprocess
|
|
36
|
+
import sys
|
|
37
|
+
import tarfile
|
|
38
|
+
import tempfile
|
|
39
|
+
|
|
40
|
+
GIT_TIMEOUT_S = 120
|
|
41
|
+
ARCHIVE_TIMEOUT_S = 600
|
|
42
|
+
GITLINK = '160000'
|
|
43
|
+
# Variables a calling git (a hook, `git rebase --exec`) may have set: they would point every
|
|
44
|
+
# call here at the caller's repository.
|
|
45
|
+
GIT_ENV_DROP = ('GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR', 'GIT_OBJECT_DIRECTORY',
|
|
46
|
+
'GIT_ALTERNATE_OBJECT_DIRECTORIES', 'GIT_NAMESPACE', 'GIT_PREFIX')
|
|
47
|
+
NO_METADATA = 'source checkout has no submodule metadata; files copied from its working tree'
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Refused(Exception):
|
|
51
|
+
"""This submodule cannot be populated; the message is the `detail`."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class BadArguments(Exception):
|
|
55
|
+
"""Exit 2."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def git_env() -> dict:
|
|
59
|
+
env = {k: v for k, v in os.environ.items() if k not in GIT_ENV_DROP}
|
|
60
|
+
# A partial clone fetches a missing object on demand; nothing here may reach a remote.
|
|
61
|
+
env.update(GIT_NO_LAZY_FETCH='1', GIT_TERMINAL_PROMPT='0')
|
|
62
|
+
return env
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def git_cmd(*args: str) -> list:
|
|
66
|
+
return ['git', '-c', 'protocol.allow=never', *args]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def git(*args: str, timeout: int = GIT_TIMEOUT_S) -> subprocess.CompletedProcess:
|
|
70
|
+
return subprocess.run(git_cmd(*args), capture_output=True, env=git_env(), timeout=timeout)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def first_line(data: bytes) -> str:
|
|
74
|
+
lines = data.decode('utf-8', 'replace').strip().splitlines()
|
|
75
|
+
return lines[0] if lines else ''
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def is_dot_git(name: str) -> bool:
|
|
79
|
+
# case-folded: on a case-insensitive filesystem `.GIT` is a `.git`
|
|
80
|
+
return name.casefold() == '.git'
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# ---- what the worktree has
|
|
84
|
+
|
|
85
|
+
def gitlinks(wt: str) -> list:
|
|
86
|
+
"""[(path, sha)] in index order. An unmerged gitlink counts once, at stage 0 or else 2 (ours)."""
|
|
87
|
+
done = git('-C', wt, 'ls-files', '-s', '-z')
|
|
88
|
+
if done.returncode != 0:
|
|
89
|
+
raise BadArguments('--worktree %s is not a git worktree: %s' % (wt, first_line(done.stderr)))
|
|
90
|
+
order, chosen = [], {}
|
|
91
|
+
for entry in done.stdout.split(b'\0'):
|
|
92
|
+
if not entry:
|
|
93
|
+
continue
|
|
94
|
+
meta, _, path = entry.partition(b'\t')
|
|
95
|
+
mode, sha, stage = meta.decode().split(' ')
|
|
96
|
+
if mode != GITLINK:
|
|
97
|
+
continue
|
|
98
|
+
path = os.fsdecode(path)
|
|
99
|
+
if path not in chosen:
|
|
100
|
+
order.append(path)
|
|
101
|
+
chosen[path] = (stage, sha)
|
|
102
|
+
elif stage in ('0', '2') and chosen[path][0] not in ('0', '2'):
|
|
103
|
+
chosen[path] = (stage, sha)
|
|
104
|
+
return [(path, chosen[path][1]) for path in order]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def names(wt: str) -> dict:
|
|
108
|
+
"""{path: name} from the worktree's .gitmodules; empty without one."""
|
|
109
|
+
gitmodules = os.path.join(wt, '.gitmodules')
|
|
110
|
+
if not os.path.isfile(gitmodules):
|
|
111
|
+
return {}
|
|
112
|
+
done = git('config', '-f', gitmodules, '-z', '--get-regexp', r'^submodule\..*\.path$')
|
|
113
|
+
found = {}
|
|
114
|
+
for entry in done.stdout.split(b'\0') if done.returncode == 0 else ():
|
|
115
|
+
key, _, value = os.fsdecode(entry).partition('\n')
|
|
116
|
+
if key.startswith('submodule.') and key.endswith('.path') and value:
|
|
117
|
+
found.setdefault(value, key[len('submodule.'):-len('.path')])
|
|
118
|
+
return found
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# ---- where the source keeps the submodule's repository
|
|
122
|
+
|
|
123
|
+
def safe_name(name: str) -> bool:
|
|
124
|
+
# git refuses these too: a name is a path under .git/modules and must stay there
|
|
125
|
+
parts = name.replace('\\', '/').split('/')
|
|
126
|
+
return bool(name) and not os.path.isabs(name) and '..' not in parts
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def rev_parse(source: str, *args: str) -> str | None:
|
|
130
|
+
done = git('-C', source, 'rev-parse', '--path-format=absolute', *args)
|
|
131
|
+
if done.returncode != 0:
|
|
132
|
+
return None
|
|
133
|
+
return os.fsdecode(done.stdout).strip() or None
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def source_gitdir(source: str, path: str, name: str) -> str | None:
|
|
137
|
+
if safe_name(name):
|
|
138
|
+
# A linked worktree keeps the modules it initialized itself under .git/worktrees/<id>/
|
|
139
|
+
# (what --git-path gives); the ones its main worktree initialized are in the common dir.
|
|
140
|
+
candidates = [rev_parse(source, '--git-path', 'modules/' + name)]
|
|
141
|
+
common = rev_parse(source, '--git-common-dir')
|
|
142
|
+
candidates.append(common and os.path.join(common, 'modules', *name.split('/')))
|
|
143
|
+
for candidate in candidates:
|
|
144
|
+
if candidate and os.path.isdir(candidate):
|
|
145
|
+
return candidate
|
|
146
|
+
# old style: the submodule's repository, or a `gitdir:` file, at <source>/<path>/.git
|
|
147
|
+
sub = os.path.join(source, path)
|
|
148
|
+
dotgit = os.path.join(sub, '.git')
|
|
149
|
+
if os.path.isdir(dotgit):
|
|
150
|
+
return dotgit
|
|
151
|
+
if os.path.isfile(dotgit):
|
|
152
|
+
try:
|
|
153
|
+
with open(dotgit, encoding='utf-8', errors='replace') as fh:
|
|
154
|
+
line = fh.readline().strip()
|
|
155
|
+
except OSError:
|
|
156
|
+
return None
|
|
157
|
+
if line.startswith('gitdir:'):
|
|
158
|
+
target = os.path.join(sub, line[len('gitdir:'):].strip())
|
|
159
|
+
if os.path.isdir(target):
|
|
160
|
+
return os.path.normpath(target)
|
|
161
|
+
return None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# ---- filling a destination
|
|
165
|
+
|
|
166
|
+
def inside(path: str, root: str) -> bool:
|
|
167
|
+
return path == root or path.startswith(root.rstrip(os.sep) + os.sep)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def make_writable(path: str) -> None:
|
|
171
|
+
"""Give `path` and the directories under it their owner's rwx: copy_tree keeps a source
|
|
172
|
+
directory's mode, and a read-only directory cannot have its entries removed."""
|
|
173
|
+
os.chmod(path, os.stat(path).st_mode | stat.S_IRWXU)
|
|
174
|
+
with os.scandir(path) as it:
|
|
175
|
+
for entry in it:
|
|
176
|
+
if entry.is_dir(follow_symlinks=False):
|
|
177
|
+
make_writable(entry.path)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def clear(dest: str) -> None:
|
|
181
|
+
"""Empty `dest` after a failed attempt, so it is left as it was found."""
|
|
182
|
+
for entry in os.scandir(dest):
|
|
183
|
+
if entry.is_dir(follow_symlinks=False):
|
|
184
|
+
make_writable(entry.path)
|
|
185
|
+
shutil.rmtree(entry.path, ignore_errors=True)
|
|
186
|
+
else:
|
|
187
|
+
try:
|
|
188
|
+
os.unlink(entry.path)
|
|
189
|
+
except OSError:
|
|
190
|
+
pass
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def member_parts(name: str) -> list:
|
|
194
|
+
if name.startswith('/') or os.path.isabs(name):
|
|
195
|
+
raise Refused('archive member refused: %s (absolute path)' % name)
|
|
196
|
+
parts = [p for p in name.split('/') if p not in ('', '.')]
|
|
197
|
+
if '..' in parts:
|
|
198
|
+
raise Refused('archive member refused: %s (.. component)' % name)
|
|
199
|
+
return parts
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def no_link_on_the_way(dest: str, parts: list, name: str) -> None:
|
|
203
|
+
"""Refuse to write through a symlink: no tree git makes needs it, and it is how a crafted
|
|
204
|
+
archive (`d -> elsewhere` then `d/x`) writes outside the destination."""
|
|
205
|
+
current = dest
|
|
206
|
+
for part in parts:
|
|
207
|
+
current = os.path.join(current, part)
|
|
208
|
+
if os.path.islink(current):
|
|
209
|
+
raise Refused('archive member refused: %s (path goes through a symlink)' % name)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def extract(archive, dest: str) -> None:
|
|
213
|
+
"""Extract an uncompressed tar stream into `dest`, vetting every member."""
|
|
214
|
+
links = []
|
|
215
|
+
with tarfile.open(fileobj=archive, mode='r|') as tar:
|
|
216
|
+
for member in tar:
|
|
217
|
+
parts = member_parts(member.name)
|
|
218
|
+
if not parts or any(is_dot_git(p) for p in parts):
|
|
219
|
+
continue
|
|
220
|
+
target = os.path.join(dest, *parts)
|
|
221
|
+
if member.isdir():
|
|
222
|
+
no_link_on_the_way(dest, parts, member.name)
|
|
223
|
+
os.makedirs(target, exist_ok=True)
|
|
224
|
+
os.chmod(target, (member.mode & 0o777) | 0o700)
|
|
225
|
+
continue
|
|
226
|
+
no_link_on_the_way(dest, parts[:-1], member.name)
|
|
227
|
+
os.makedirs(os.path.dirname(target), exist_ok=True)
|
|
228
|
+
if member.issym():
|
|
229
|
+
linked = member.linkname
|
|
230
|
+
resolved = os.path.normpath(os.path.join(dest, *parts[:-1], linked))
|
|
231
|
+
if not linked or os.path.isabs(linked) or not inside(resolved, dest):
|
|
232
|
+
raise Refused('archive member refused: %s (link to %s, outside the submodule)'
|
|
233
|
+
% (member.name, linked))
|
|
234
|
+
os.symlink(linked, target)
|
|
235
|
+
links.append((member.name, target))
|
|
236
|
+
elif member.isreg():
|
|
237
|
+
fd = os.open(target, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_NOFOLLOW', 0), 0o600)
|
|
238
|
+
with os.fdopen(fd, 'wb') as out:
|
|
239
|
+
shutil.copyfileobj(tar.extractfile(member), out)
|
|
240
|
+
os.fchmod(out.fileno(), member.mode & 0o777)
|
|
241
|
+
else:
|
|
242
|
+
raise Refused('archive member refused: %s (not a file, directory or symlink)' % member.name)
|
|
243
|
+
# Each link was checked on its own text; a chain (`b -> .`, `a -> b/..`) can still lead out.
|
|
244
|
+
real = os.path.realpath(dest)
|
|
245
|
+
for name, link in links:
|
|
246
|
+
if not inside(os.path.realpath(link), real):
|
|
247
|
+
raise Refused('archive member refused: %s (link resolves outside the submodule)' % name)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def export(gitdir: str, sha: str, dest: str) -> None:
|
|
251
|
+
with tempfile.TemporaryFile() as err:
|
|
252
|
+
proc = subprocess.Popen(git_cmd('--git-dir=' + gitdir, 'archive', '--format=tar', sha),
|
|
253
|
+
stdout=subprocess.PIPE, stderr=err, env=git_env())
|
|
254
|
+
try:
|
|
255
|
+
extract(proc.stdout, dest)
|
|
256
|
+
except (tarfile.TarError, OSError) as exc:
|
|
257
|
+
raise Refused('extracting git archive failed: %s' % exc) from exc
|
|
258
|
+
finally:
|
|
259
|
+
proc.stdout.close()
|
|
260
|
+
try:
|
|
261
|
+
code = proc.wait(timeout=ARCHIVE_TIMEOUT_S)
|
|
262
|
+
except subprocess.TimeoutExpired:
|
|
263
|
+
proc.kill()
|
|
264
|
+
code = proc.wait()
|
|
265
|
+
if code != 0:
|
|
266
|
+
err.seek(0)
|
|
267
|
+
raise Refused('git archive failed: %s' % (first_line(err.read()) or 'exit %d' % code))
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def copy_tree(src: str, dest: str) -> None:
|
|
271
|
+
"""Copy src's contents into dest: symlinks as symlinks, files as new files, .git skipped."""
|
|
272
|
+
for entry in os.scandir(src):
|
|
273
|
+
if is_dot_git(entry.name):
|
|
274
|
+
continue
|
|
275
|
+
target = os.path.join(dest, entry.name)
|
|
276
|
+
if entry.is_symlink():
|
|
277
|
+
os.symlink(os.readlink(entry.path), target)
|
|
278
|
+
elif entry.is_dir(follow_symlinks=False):
|
|
279
|
+
os.mkdir(target)
|
|
280
|
+
copy_tree(entry.path, target)
|
|
281
|
+
shutil.copymode(entry.path, target)
|
|
282
|
+
elif entry.is_file(follow_symlinks=False):
|
|
283
|
+
# copyfile writes a new file: never os.link, so the copy can be rewritten in place
|
|
284
|
+
shutil.copyfile(entry.path, target, follow_symlinks=False)
|
|
285
|
+
shutil.copymode(entry.path, target, follow_symlinks=False)
|
|
286
|
+
# sockets, fifos, devices: nothing to build with
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def has_files(directory: str) -> bool:
|
|
290
|
+
try:
|
|
291
|
+
with os.scandir(directory) as it:
|
|
292
|
+
return any(not is_dot_git(e.name) for e in it)
|
|
293
|
+
except OSError:
|
|
294
|
+
return False
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def populate_one(source: str, wt: str, path: str, sha: str, name: str) -> tuple:
|
|
298
|
+
"""(state, detail) for one gitlink."""
|
|
299
|
+
dest = os.path.join(wt, path)
|
|
300
|
+
if os.path.islink(dest) or (os.path.lexists(dest) and not os.path.isdir(dest)):
|
|
301
|
+
return 'missing', 'the worktree has something other than a directory at this path'
|
|
302
|
+
if not inside(os.path.realpath(dest), os.path.realpath(wt)):
|
|
303
|
+
return 'missing', 'the path leads outside the worktree'
|
|
304
|
+
if os.path.isdir(dest) and any(True for _ in os.scandir(dest)):
|
|
305
|
+
return 'kept', ''
|
|
306
|
+
gitdir = source_gitdir(source, path, name)
|
|
307
|
+
fallback = NO_METADATA
|
|
308
|
+
if gitdir:
|
|
309
|
+
if git('--git-dir=' + gitdir, 'cat-file', '-e', sha + '^{commit}').returncode == 0:
|
|
310
|
+
os.makedirs(dest, exist_ok=True)
|
|
311
|
+
try:
|
|
312
|
+
export(gitdir, sha, dest)
|
|
313
|
+
except Exception as exc: # noqa: BLE001 whatever it was, this one is missing and the rest go on
|
|
314
|
+
clear(dest)
|
|
315
|
+
return 'missing', str(exc) if isinstance(exc, Refused) else 'exporting failed: %s' % exc
|
|
316
|
+
return 'exported', ''
|
|
317
|
+
fallback = ("the source's submodule repository does not have the recorded commit; "
|
|
318
|
+
'files copied from its working tree')
|
|
319
|
+
src = os.path.join(source, path)
|
|
320
|
+
if os.path.isdir(src) and has_files(src):
|
|
321
|
+
os.makedirs(dest, exist_ok=True)
|
|
322
|
+
try:
|
|
323
|
+
copy_tree(src, dest)
|
|
324
|
+
except Exception as exc: # noqa: BLE001
|
|
325
|
+
clear(dest)
|
|
326
|
+
return 'missing', 'copying from the source checkout failed: %s' % exc
|
|
327
|
+
return 'copied', fallback
|
|
328
|
+
if gitdir:
|
|
329
|
+
return 'missing', "the source's submodule repository does not have the recorded commit"
|
|
330
|
+
return 'missing', 'the source checkout has neither the submodule repository nor its files'
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def field(text: str) -> str:
|
|
334
|
+
return text.replace('\t', ' ').replace('\n', ' ').replace('\r', ' ')
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def write_atomically(out: str, text: str) -> None:
|
|
338
|
+
directory = os.path.dirname(os.path.abspath(out))
|
|
339
|
+
os.makedirs(directory, exist_ok=True)
|
|
340
|
+
fd, tmp = tempfile.mkstemp(dir=directory, prefix='.' + os.path.basename(out) + '.')
|
|
341
|
+
try:
|
|
342
|
+
with os.fdopen(fd, 'w', encoding='utf-8') as fh:
|
|
343
|
+
fh.write(text)
|
|
344
|
+
os.replace(tmp, out)
|
|
345
|
+
except BaseException:
|
|
346
|
+
try:
|
|
347
|
+
os.unlink(tmp)
|
|
348
|
+
except OSError:
|
|
349
|
+
pass
|
|
350
|
+
raise
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def populate(source: str, wt: str, out: str) -> int:
|
|
354
|
+
links = gitlinks(wt)
|
|
355
|
+
if not links:
|
|
356
|
+
return 0
|
|
357
|
+
by_path = names(wt)
|
|
358
|
+
lines = []
|
|
359
|
+
for path, sha in links:
|
|
360
|
+
try:
|
|
361
|
+
state, detail = populate_one(source, wt, path, sha, by_path.get(path, path))
|
|
362
|
+
except Exception as exc: # noqa: BLE001
|
|
363
|
+
state, detail = 'missing', 'populating failed: %s' % exc
|
|
364
|
+
lines.append('\t'.join((state, field(path), sha, field(detail))) + '\n')
|
|
365
|
+
write_atomically(out, ''.join(lines))
|
|
366
|
+
return 0
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def main(argv: list) -> int:
|
|
370
|
+
parser = argparse.ArgumentParser(prog='forge-submodules.py', description=__doc__.split('\n\n')[1])
|
|
371
|
+
sub = parser.add_subparsers(dest='command', required=True)
|
|
372
|
+
pop = sub.add_parser('populate', help="fill a worktree's submodule paths from the source checkout")
|
|
373
|
+
pop.add_argument('--source', required=True, help='the checkout the worktree was made from')
|
|
374
|
+
pop.add_argument('--worktree', required=True, help='the worktree to populate')
|
|
375
|
+
pop.add_argument('--out', required=True, help='where the TSV outcome is written')
|
|
376
|
+
args = parser.parse_args(argv)
|
|
377
|
+
source, wt = os.path.abspath(args.source), os.path.abspath(args.worktree)
|
|
378
|
+
for label, directory in (('--source', source), ('--worktree', wt)):
|
|
379
|
+
if not os.path.isdir(directory):
|
|
380
|
+
parser.error('%s %s is not a directory' % (label, directory))
|
|
381
|
+
try:
|
|
382
|
+
return populate(source, wt, args.out)
|
|
383
|
+
except BadArguments as exc:
|
|
384
|
+
print('forge-submodules: %s' % exc, file=sys.stderr)
|
|
385
|
+
return 2
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
if __name__ == '__main__':
|
|
389
|
+
sys.exit(main(sys.argv[1:]))
|