Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmux-tui/crates/cmux-tui/src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod dial;
mod dial_cli;
mod enrollment;
mod ensure;
// `cmux link seed plan`; `cmux link seed push|clone` is [`seed_cli`].
// `cmux link seed plan`; `cmux link seed push|clone|pull` is [`seed_cli`].
#[cfg_attr(not(test), allow(dead_code))]
mod seed;
mod seed_cli;
Expand Down Expand Up @@ -121,7 +121,7 @@ pub(crate) fn run(args: &[String]) -> i32 {
return sync_cli::run(&args[1..]);
}
if args.first().map(String::as_str) == Some("seed") {
if matches!(args.get(1).map(String::as_str), Some("push" | "clone")) {
if matches!(args.get(1).map(String::as_str), Some("push" | "clone" | "pull")) {
return seed_cli::run(args.get(1..).unwrap_or_default());
}
return seed::run(&args[1..]);
Expand Down
137 changes: 89 additions & 48 deletions cmux-tui/crates/cmux-tui/src/link/seed/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ pub struct SeedParams {
/// An untracked file left out of the work commit, and why in plain words.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Skipped {
/// For display: a name that is not UTF-8 is shown lossily.
pub path: String,
pub why: String,
/// git's raw name, which is what the plan leaves out (a lossy spelling
/// of a name that is not UTF-8 would match nothing).
#[serde(skip)]
pub raw: Vec<u8>,
}

/// A submodule and the commit checked out in it (the recorded one when it
Expand Down Expand Up @@ -421,6 +426,33 @@ const SEED_NAME: &str = "cmux seed";
const SEED_EMAIL: &str = "seed@cmux.invalid";
const SEED_DATE: &str = "946684800 +0000";

/// The working copy at `folder` as a seed would send it now: HEAD and its
/// branch, and the INDEX and WORK commits [`plan`] would make (written to
/// the repository's object store, unreferenced). `cmux link seed pull`
/// compares them with the seed's.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Current {
pub head_ref: Option<String>,
pub head: String,
pub index: String,
pub work: String,
}

pub(crate) fn current(folder: &Path) -> Result<Current, Fallback> {
let repo = Repo::open(folder)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: current unnecessarily requires the local repository to retain a GitHub remote, so a seeded folder whose remote was removed or changed cannot be pulled back. Build the current HEAD/index/work commits through a local-only repository opener instead of Repo::open.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At cmux-tui/crates/cmux-tui/src/link/seed/plan.rs, line 442:

<comment>`current` unnecessarily requires the local repository to retain a GitHub remote, so a seeded folder whose remote was removed or changed cannot be pulled back. Build the current HEAD/index/work commits through a local-only repository opener instead of `Repo::open`.</comment>

<file context>
@@ -421,6 +426,33 @@ const SEED_NAME: &str = "cmux seed";
+}
+
+pub(crate) fn current(folder: &Path) -> Result<Current, Fallback> {
+    let repo = Repo::open(folder)?;
+    let scratch = Scratch::new()?;
+    let made = commits(&repo, &scratch)?;
</file context>

let scratch = Scratch::new()?;
let made = commits(&repo, &scratch)?;
Ok(Current { head_ref: repo.head_ref, head: repo.head, index: made.index, work: made.work })
}

/// The INDEX and WORK commits of a seed, and what WORK left out.
struct Commits {
index: String,
work: String,
skipped: Vec<Skipped>,
sparse: Option<String>,
}

fn build(
repo: &Repo,
out_bundle: &Path,
Expand All @@ -429,6 +461,8 @@ fn build(
) -> Result<Planned, Fallback> {
let submodules = submodules(repo)?;
let scratch = Scratch::new()?;
let Commits { index: index_commit, work: work_commit, skipped, sparse } =
commits(repo, &scratch)?;
let store = scratch.0.join("store.git");
git::output(git::at(&scratch.0).args(["init", "-q", "--bare", "--template="]).arg(&store))
.map_err(|error| failed("making the scratch repository", error))?;
Expand All @@ -442,53 +476,6 @@ fn build(
std::fs::copy(shallow, store.join("shallow"))
.map_err(|error| failed("copying the shallow list", error.to_string()))?;
}
let index = scratch.0.join("index");
let real_index = repo.git_path("index")?;
if real_index.is_file() {
std::fs::copy(real_index, &index)
.map_err(|error| failed("copying the index", error.to_string()))?;
} else {
run(repo.scratch_git(&index).args(["read-tree", repo.head.as_str()]), "reading HEAD")?;
}

let index_tree = repo.write_tree(&index)?;
let index_commit = commit(repo, &index_tree, &repo.head, "cmux seed index")?;

let skipped = untracked_left_out(repo)?;
let sparse = sparse_patterns(repo)?;
// In a sparse checkout `--sparse` also takes untracked files outside
// the patterns; entries git keeps out of the working tree stay as they
// are either way.
let sparse_flag: &[&str] = if sparse.is_some() { &["--sparse"] } else { &[] };
run(repo.scratch_git(&index).args(["add", "-A"]).args(sparse_flag), "adding the working tree")?;
if !skipped.is_empty() {
let list = scratch.0.join("left-out");
let mut paths = Vec::new();
for item in &skipped {
paths.extend_from_slice(item.path.trim_end_matches('/').as_bytes());
paths.push(0);
}
write(&list, paths)?;
run(
repo.scratch_git(&index)
.env("GIT_LITERAL_PATHSPECS", "1")
.args([
"rm",
"--cached",
"-r",
"-f",
"-q",
"--ignore-unmatch",
"--pathspec-file-nul",
])
.args(sparse_flag)
.arg(format!("--pathspec-from-file={}", list.display())),
"leaving out secrets",
)?;
}
let work_tree = repo.write_tree(&index)?;
let work_commit = commit(repo, &work_tree, &index_commit, "cmux seed work")?;

let stash = repo.commit("refs/stash");
let lfs = uses_lfs(repo, &skipped);

Expand Down Expand Up @@ -561,6 +548,60 @@ fn build(
})
}

/// The INDEX commit (the staged tree on HEAD) and the WORK commit (the
/// working tree with untracked files that are not ignored, less
/// [`untracked_left_out`], on INDEX), built through a copy of the index in
/// `scratch`.
fn commits(repo: &Repo, scratch: &Scratch) -> Result<Commits, Fallback> {
let index = scratch.0.join("index");
let real_index = repo.git_path("index")?;
if real_index.is_file() {
std::fs::copy(real_index, &index)
.map_err(|error| failed("copying the index", error.to_string()))?;
} else {
run(repo.scratch_git(&index).args(["read-tree", repo.head.as_str()]), "reading HEAD")?;
}

let index_tree = repo.write_tree(&index)?;
let index_commit = commit(repo, &index_tree, &repo.head, "cmux seed index")?;

let skipped = untracked_left_out(repo)?;
let sparse = sparse_patterns(repo)?;
// In a sparse checkout `--sparse` also takes untracked files outside
// the patterns; entries git keeps out of the working tree stay as they
// are either way.
let sparse_flag: &[&str] = if sparse.is_some() { &["--sparse"] } else { &[] };
run(repo.scratch_git(&index).args(["add", "-A"]).args(sparse_flag), "adding the working tree")?;
if !skipped.is_empty() {
let list = scratch.0.join("left-out");
let mut paths = Vec::new();
for item in &skipped {
paths.extend_from_slice(item.raw.strip_suffix(b"/").unwrap_or(&item.raw));
paths.push(0);
}
write(&list, paths)?;
run(
repo.scratch_git(&index)
.env("GIT_LITERAL_PATHSPECS", "1")
.args([
"rm",
"--cached",
"-r",
"-f",
"-q",
"--ignore-unmatch",
"--pathspec-file-nul",
])
.args(sparse_flag)
.arg(format!("--pathspec-from-file={}", list.display())),
"leaving out secrets",
)?;
}
let work_tree = repo.write_tree(&index)?;
let work = commit(repo, &work_tree, &index_commit, "cmux seed work")?;
Ok(Commits { index: index_commit, work, skipped, sparse })
}

impl Repo {
/// git in the working copy with the scratch index.
fn scratch_git(&self, index: &Path) -> Command {
Expand Down Expand Up @@ -621,7 +662,7 @@ fn untracked_left_out(repo: &Repo) -> Result<Vec<Skipped>, Fallback> {
secrets::reason(&repo.top, raw)
};
if let Some(why) = why {
skipped.push(Skipped { path, why: why.to_string() });
skipped.push(Skipped { path, why: why.to_string(), raw: raw.to_vec() });
}
}
Ok(skipped)
Expand Down
14 changes: 14 additions & 0 deletions cmux-tui/crates/cmux-tui/src/link/seed/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ fn the_same_tree_gives_the_same_commits() {
assert_eq!(std::fs::read_to_string(machine.join("notes.txt")).unwrap(), "untracked\n");
}

/// A secret whose name is not UTF-8 is left out by its raw name: its
/// contents never reach WORK.
#[test]
fn a_secret_with_a_non_utf8_name_is_left_out() {
use std::os::unix::ffi::OsStrExt as _;
let f = Fixture::new();
std::fs::write(f.work.join(std::ffi::OsStr::from_bytes(b".env-\xff")), "SECRET=1\n").unwrap();
write(&f.work.join("README.md"), "changed\n");
let planned = plan(&f.work, &f.path("one.bundle")).unwrap();
assert_eq!(planned.skipped.len(), 1, "{:?}", planned.skipped);
let names = git(&f.work, &["ls-tree", "-r", "--name-only", &planned.params.work]);
assert!(!names.contains(".env"), "{names}");
}

#[test]
fn detached_head_has_no_head_ref() {
let f = Fixture::new();
Expand Down
Loading
Loading