1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
154
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::process::Command;
use camino::{Utf8Path, Utf8PathBuf};
use crate::config::SourceSubstrate;
use crate::error::{NewgitError, Result};
/// Shell-out driver for the source tracker. Everything goes through Git's
/// public interface — never into `.git` directly — so it works identically
/// against a clone or, later, a projection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitSource {
root: Utf8PathBuf,
}
impl GitSource {
pub fn open(root: &Utf8Path, substrate: SourceSubstrate) -> Result<Self> {
if root.join(".git").exists() {
return Ok(Self {
root: root.to_path_buf(),
});
}
match substrate {
SourceSubstrate::Jj => Err(NewgitError::Unsupported(format!(
"{root} is a jj repository without a colocated .git; v1 drives source through \
Git — recreate it with `jj git init --colocate`"
))),
SourceSubstrate::Git => Err(NewgitError::NotAGitRepo(root.to_path_buf())),
}
}
pub fn root(&self) -> &Utf8Path {
&self.root
}
/// Whether the store repo's gitignore rules cover `path` (which need not
/// exist yet) — used to enforce the tracker-path invariant.
pub fn is_ignored(&self, path: &str) -> Result<bool> {
let args = ["-C", self.root.as_str(), "check-ignore", "-q", "--", path];
let output = Command::new("git")
.args(args)
.output()
.map_err(|source| spawn_error(&args, &source))?;
Ok(output.status.success())
}
/// Whether `path` is tracked by the store repo — a tracker path that is
/// also in Git history is dual-tracked, deliberately or not.
pub fn is_tracked(&self, path: &str) -> Result<bool> {
self.git(&["ls-files", "--", path])
.map(|stdout| !stdout.is_empty())
}
pub fn branch_exists(&self, name: &str) -> Result<bool> {
let args = [
"-C",
self.root.as_str(),
"rev-parse",
"--verify",
"--quiet",
&format!("refs/heads/{name}"),
];
let output = Command::new("git")
.args(args)
.output()
.map_err(|source| spawn_error(&args, &source))?;
Ok(output.status.success())
}
pub fn create_branch(&self, name: &str, base: &str) -> Result<()> {
self.git(&["branch", "--", name, base]).map(|_| ())
}
pub fn rev_parse(&self, revision: &str) -> Result<String> {
self.git(&["rev-parse", "--verify", &format!("{revision}^{{commit}}")])
.map_err(|error| {
if revision == "HEAD" {
NewgitError::Unsupported(
"the store repository has no commits yet; make an initial commit before \
spawning"
.to_owned(),
)
} else {
error
}
})
}
/// Materialize `branch` as a full standalone clone at `destination`.
/// A local-path clone hardlinks objects; never `--shared`/`--reference`.
pub fn clone_to(&self, branch: &str, destination: &Utf8Path) -> Result<()> {
run_git(&[
"clone",
"--quiet",
"--branch",
branch,
"--",
self.root.as_str(),
destination.as_str(),
])
.map(|_| ())
}
/// Fetch a ref from another repository (typically a workspace clone)
/// into the store. Fetch, never push: the store pulls commits in when a
/// checkpoint blesses them, and workspaces stay passive.
pub fn fetch_ref(&self, from: &Utf8Path, remote_ref: &str, local_ref: &str) -> Result<()> {
self.git(&[
"fetch",
"--quiet",
"--no-write-fetch-head",
"--",
from.as_str(),
&format!("+{remote_ref}:{local_ref}"),
])
.map(|_| ())
}
pub fn update_ref(&self, name: &str, rev: &str) -> Result<()> {
self.git(&["update-ref", name, rev]).map(|_| ())
}
/// Every ref under a namespace, e.g. `refs/newgit/checkpoints/<slug>`.
pub fn refs_under(&self, prefix: &str) -> Result<Vec<String>> {
let listing = self.git(&["for-each-ref", "--format=%(refname)", prefix])?;
Ok(listing
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(ToOwned::to_owned)
.collect())
}
pub fn delete_ref(&self, name: &str) -> Result<()> {
self.git(&["update-ref", "-d", name]).map(|_| ())
}
/// Resolve a ref to a commit, when it exists.
pub fn ref_rev(&self, name: &str) -> Option<String> {
self.git(&[
"rev-parse",
"--verify",
"--quiet",
&format!("{name}^{{commit}}"),
])
.ok()
.filter(|rev| !rev.is_empty())
}
pub fn is_ancestor(&self, ancestor: &str, descendant: &str) -> Result<bool> {
let args = [
"-C",
self.root.as_str(),
"merge-base",
"--is-ancestor",
ancestor,
descendant,
];
let output = Command::new("git")
.args(args)
.output()
.map_err(|source| spawn_error(&args, &source))?;
Ok(output.status.success())
}
/// Live HEAD of a workspace clone, short form.
pub fn workspace_short_head(workspace: &Utf8Path) -> Result<String> {
run_git(&["-C", workspace.as_str(), "rev-parse", "--short", "HEAD"])
}
/// Live HEAD of a workspace clone, full form.
pub fn workspace_head(workspace: &Utf8Path) -> Result<String> {
run_git(&["-C", workspace.as_str(), "rev-parse", "HEAD"])
}
/// Committed content of one path in a workspace clone: the blob at HEAD,
/// not what is on disk. `None` when HEAD has no such path.
///
/// This is what a render substitutes into — see [`crate::render::apply`].
/// Read raw, never through [`run_git`]: that trims, which is right for a
/// rev and silently destructive for file content — it would drop the
/// file's trailing newline on every render.
pub fn workspace_show_head(workspace: &Utf8Path, path: &Utf8Path) -> Result<Option<String>> {
let args = ["-C", workspace.as_str(), "show", &format!("HEAD:{path}")];
let output = Command::new("git")
.args(args)
.output()
.map_err(|source| spawn_error(&args, &source))?;
// `git show` fails the same way for "no such path at HEAD" as for a
// broken repo; the caller has already established the latter is not
// the case, and treats absence as "nothing committed to render from".
if !output.status.success() {
return Ok(None);
}
// Strict rather than lossy: rendering into a file newgit cannot read
// as text would write back mojibake where the project's bytes were.
match String::from_utf8(output.stdout) {
Ok(contents) => Ok(Some(contents)),
Err(_) => Err(NewgitError::Unsupported(format!(
"`{path}` is not valid UTF-8 at HEAD; a render substitutes text"
))),
}
}
/// Mark paths `--skip-worktree` in a workspace clone, so this instance's
/// rendered values never show as a modification and cannot be committed
/// by an agent running `git add -A`.
///
/// The counterpart of `.git/info/exclude` for tracker paths: newgit does
/// not control the Git an agent runs, so what must not be committable has
/// to be made so by construction. v1's stand-in for the projection a v2
/// materializer does properly.
pub fn workspace_skip_worktree(workspace: &Utf8Path, paths: &[Utf8PathBuf]) -> Result<()> {
if paths.is_empty() {
return Ok(());
}
let mut args: Vec<&str> = vec![
"-C",
workspace.as_str(),
"update-index",
"--skip-worktree",
"--",
];
args.extend(paths.iter().map(|path| path.as_str()));
run_git(&args).map(|_| ())
}
/// Snapshot uncommitted and untracked (non-ignored) workspace state as a
/// dangling commit on top of HEAD, without touching HEAD, the real
/// index, or the worktree. Returns `None` when the worktree is clean.
///
/// Mechanics: a throwaway `GIT_INDEX_FILE` seeded from HEAD, `git add -A`
/// into it, `git write-tree`, and `git commit-tree` — all public
/// interface, nothing reaches into `.git` internals.
///
/// `rendered` paths are marked skip-worktree *in the throwaway index*.
/// The real index carries that bit already, but a fresh index seeded from
/// HEAD does not, so without this `git add -A` would sweep the instance's
/// rendered ports into the checkpoint — the one place skip-worktree does
/// not protect on its own.
pub fn workspace_dirty_commit(
workspace: &Utf8Path,
message: &str,
rendered: &[Utf8PathBuf],
) -> Result<Option<String>> {
let scratch = tempfile::tempdir().map_err(|source| NewgitError::io(workspace, source))?;
let index = scratch.path().join("index");
let Some(index) = index.to_str() else {
return Err(NewgitError::NonUtf8Path(index.display().to_string()));
};
let env = [("GIT_INDEX_FILE".to_owned(), index.to_owned())];
let ws = workspace.as_str();
run_git_env(&["-C", ws, "read-tree", "HEAD"], &env)?;
if !rendered.is_empty() {
let mut args: Vec<&str> = vec!["-C", ws, "update-index", "--skip-worktree", "--"];
args.extend(rendered.iter().map(|path| path.as_str()));
run_git_env(&args, &env)?;
}
run_git_env(&["-C", ws, "add", "-A"], &env)?;
let tree = run_git_env(&["-C", ws, "write-tree"], &env)?;
let head_tree = run_git(&["-C", ws, "rev-parse", "HEAD^{tree}"])?;
if tree == head_tree {
return Ok(None);
}
// A synthetic commit needs an identity even where none is configured.
let ident = [
("GIT_AUTHOR_NAME".to_owned(), "newgit".to_owned()),
("GIT_AUTHOR_EMAIL".to_owned(), "newgit@localhost".to_owned()),
("GIT_COMMITTER_NAME".to_owned(), "newgit".to_owned()),
(
"GIT_COMMITTER_EMAIL".to_owned(),
"newgit@localhost".to_owned(),
),
];
run_git_env(
&["-C", ws, "commit-tree", &tree, "-p", "HEAD", "-m", message],
&ident,
)
.map(Some)
}
pub fn workspace_update_ref(workspace: &Utf8Path, name: &str, rev: &str) -> Result<()> {
run_git(&["-C", workspace.as_str(), "update-ref", name, rev]).map(|_| ())
}
pub fn workspace_delete_ref(workspace: &Utf8Path, name: &str) -> Result<()> {
run_git(&["-C", workspace.as_str(), "update-ref", "-d", name]).map(|_| ())
}
/// Fetch a store ref into a workspace clone (undo may need objects the
/// workspace has since discarded).
pub fn workspace_fetch_ref(
workspace: &Utf8Path,
from: &Utf8Path,
remote_ref: &str,
) -> Result<()> {
run_git(&[
"-C",
workspace.as_str(),
"fetch",
"--quiet",
"--no-write-fetch-head",
"--",
from.as_str(),
remote_ref,
])
.map(|_| ())
}
/// Put a workspace back to a checkpointed source state: HEAD hard-reset
/// to `head_rev`, untracked (non-ignored) files removed, then — when the
/// checkpoint carried uncommitted state — that state reapplied to the
/// worktree as uncommitted changes again. Ignored files (tracker paths,
/// node_modules) are deliberately left alone; trackers and resources own
/// their restoration.
pub fn workspace_restore_to(
workspace: &Utf8Path,
head_rev: &str,
dirty_rev: Option<&str>,
) -> Result<()> {
let ws = workspace.as_str();
run_git(&["-C", ws, "reset", "--quiet", "--hard", head_rev])?;
run_git(&["-C", ws, "clean", "-fdq"])?;
if let Some(dirty) = dirty_rev {
run_git(&["-C", ws, "checkout", "--quiet", dirty, "--", "."])?;
// Mixed reset: index back to head_rev, so the dirty snapshot
// shows as uncommitted modifications/untracked files — exactly
// how it looked when the checkpoint was taken.
run_git(&["-C", ws, "reset", "--quiet", head_rev])?;
}
Ok(())
}
/// Every path the workspace's Git tracks, workspace-relative. This is
/// what "the source content of this branch" means for export: tracked
/// files as they stand on disk, so uncommitted edits are included and
/// ignored junk never is.
pub fn workspace_tracked_files(workspace: &Utf8Path) -> Result<Vec<Utf8PathBuf>> {
let listing = run_git(&["-C", workspace.as_str(), "ls-files", "-z"])?;
Ok(listing
.split('\0')
.filter(|entry| !entry.is_empty())
.map(Utf8PathBuf::from)
.collect())
}
/// Turn a directory of already-placed files into an ordinary Git
/// repository with one commit on `branch`. Returns the commit.
///
/// One commit, never a history rewrite: exporting the workspace's Git
/// history would carry every file any past commit contained, which is
/// exactly the content the audience filter just excluded.
pub fn init_export_repo(destination: &Utf8Path, branch: &str, message: &str) -> Result<String> {
let dest = destination.as_str();
run_git(&["init", "--quiet", "-b", branch, "--", dest])?;
run_git(&["-C", dest, "add", "-A"])?;
run_git_env(
&["-C", dest, "commit", "--quiet", "-m", message],
&export_identity(destination),
)?;
run_git(&["-C", dest, "rev-parse", "HEAD"])
}
fn git(&self, args: &[&str]) -> Result<String> {
let mut full: Vec<&str> = vec!["-C", self.root.as_str()];
full.extend_from_slice(args);
run_git(&full)
}
}
/// Walk upward looking for a source repo root. `.jj` wins over `.git` at the
/// same level (a colocated repo is still a jj repo).
pub fn find_repo_root(start: &Utf8Path) -> Option<(Utf8PathBuf, SourceSubstrate)> {
let mut dir = Some(start);
while let Some(current) = dir {
if current.join(".jj").is_dir() {
return Some((current.to_path_buf(), SourceSubstrate::Jj));
}
if current.join(".git").exists() {
return Some((current.to_path_buf(), SourceSubstrate::Git));
}
dir = current.parent();
}
None
}
fn run_git(args: &[&str]) -> Result<String> {
run_git_env(args, &[])
}
/// The user's own Git identity when they have one, so an exported repo looks
/// like their work; a newgit identity only where Git would otherwise refuse
/// to commit at all.
fn export_identity(destination: &Utf8Path) -> Vec<(String, String)> {
if run_git(&["-C", destination.as_str(), "var", "GIT_COMMITTER_IDENT"]).is_ok() {
return Vec::new();
}
["AUTHOR", "COMMITTER"]
.iter()
.flat_map(|role| {
[
(format!("GIT_{role}_NAME"), "newgit".to_owned()),
(format!("GIT_{role}_EMAIL"), "newgit@localhost".to_owned()),
]
})
.collect()
}
fn run_git_env(args: &[&str], env: &[(String, String)]) -> Result<String> {
let output = Command::new("git")
.args(args)
.envs(env.iter().map(|(key, value)| (key, value)))
.output()
.map_err(|source| spawn_error(args, &source))?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
} else {
Err(NewgitError::SourceCommand {
command: format!("git {}", args.join(" ")),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
})
}
}
fn spawn_error(args: &[&str], source: &std::io::Error) -> NewgitError {
NewgitError::SourceCommand {
command: format!("git {}", args.join(" ")),
stderr: source.to_string(),
}
}