Skip to content

Commit dad94db

Browse files
committed
sync: tighten home branch fetches and zed launching
1 parent 0a1a9a6 commit dad94db

4 files changed

Lines changed: 79 additions & 42 deletions

File tree

docs/pr-edit-watcher.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ If the frontmatter is missing, Flow may fall back to `~/.flow/pr-edit/.index.jso
6868
- Finds the open PR for the current branch (fallback: queued commit PR)
6969
- Creates `~/.flow/pr-edit/<project>-<pr>.md` if missing
7070
- Ensures the file contains PR frontmatter
71-
- Opens the file in Zed Preview
71+
- Opens the file in Zed
7272
- Starts a foreground watcher that syncs on save (Ctrl-C to stop)
7373

7474
## Status JSON

src/commit.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9606,7 +9606,7 @@ pub fn run_pr(opts: PrOpts) -> Result<()> {
96069606
// Convenience: `f pr open` opens the PR for the current branch (or queued commit) without
96079607
// creating a new commit.
96089608
[a] if a == "open" => return run_pr_open(&repo_root, &opts),
9609-
// Convenience: `f pr open edit` opens a local markdown file in Zed Preview and syncs PR
9609+
// Convenience: `f pr open edit` opens a local markdown file in Zed and syncs PR
96109610
// title/body on save.
96119611
[a, b] if a == "open" && b == "edit" => return run_pr_open_edit(&repo_root, &opts),
96129612
_ => {}
@@ -11594,17 +11594,20 @@ fn flow_project_name(repo_root: &Path) -> String {
1159411594
}
1159511595

1159611596
fn open_in_zed_preview(path: &Path) -> Result<()> {
11597-
// Prefer Zed Preview if installed, otherwise fall back to Zed.
11597+
// Prefer the local Zed.app fork and fall back to Preview if that app is not installed.
1159811598
let try_open = |app: &str| -> Result<()> {
11599-
Command::new("open")
11599+
let status = Command::new("open")
1160011600
.args(["-a", app])
1160111601
.arg(path)
1160211602
.status()
1160311603
.with_context(|| format!("failed to open {app}"))?;
11604+
if !status.success() {
11605+
bail!("{app} exited with status {status}");
11606+
}
1160411607
Ok(())
1160511608
};
1160611609

11607-
try_open("/Applications/Zed Preview.app").or_else(|_| try_open("/Applications/Zed.app"))
11610+
try_open("/Applications/Zed.app").or_else(|_| try_open("/Applications/Zed Preview.app"))
1160811611
}
1160911612

1161011613
fn parse_pr_edit_markdown(text: &str) -> Result<(String, String)> {

src/repos.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ pub fn clone_git_like(opts: CloneOpts) -> Result<()> {
7070
}
7171

7272
fn open_in_zed(path: &std::path::Path) -> Result<()> {
73-
std::process::Command::new("open")
74-
.args(["-a", "/Applications/Zed Preview.app"])
75-
.arg(path)
76-
.status()
77-
.context("failed to open Zed")?;
78-
Ok(())
73+
let try_open = |app: &str| -> Result<()> {
74+
let status = std::process::Command::new("open")
75+
.args(["-a", app])
76+
.arg(path)
77+
.status()
78+
.with_context(|| format!("failed to open {app}"))?;
79+
if !status.success() {
80+
bail!("{app} exited with status {status}");
81+
}
82+
Ok(())
83+
};
84+
85+
try_open("/Applications/Zed.app").or_else(|_| try_open("/Applications/Zed Preview.app"))
7986
}
8087

8188
/// Fuzzy search through repos in ~/repos and print the selected path.

src/sync.rs

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ fn resolve_home_branch_sync_target(
248248
})
249249
}
250250

251+
fn origin_fetch_branches_for_jj_sync(
252+
current_branch: &str,
253+
origin_default_branch: Option<&str>,
254+
home_branch_sync_target: Option<&HomeBranchSyncTarget>,
255+
) -> Vec<String> {
256+
let mut branches = Vec::new();
257+
258+
if home_branch_sync_target.is_none() {
259+
let current_branch = current_branch.trim();
260+
if !current_branch.is_empty() {
261+
branches.push(current_branch.to_string());
262+
}
263+
}
264+
265+
if let Some(default_branch) = origin_default_branch
266+
.map(str::trim)
267+
.filter(|branch| !branch.is_empty())
268+
&& !branches.iter().any(|branch| branch == default_branch)
269+
{
270+
branches.push(default_branch.to_string());
271+
}
272+
273+
branches
274+
}
275+
251276
fn select_jj_sync_branch(
252277
git_head_ref: &str,
253278
current_bookmarks: &[String],
@@ -2629,46 +2654,23 @@ fn run_jj_sync(
26292654
let mut failures: Vec<String> = Vec::new();
26302655

26312656
if has_origin && origin_reachable {
2632-
track_remote_ref(&mut tracked_refs, repo_root, "origin", &current_branch);
2633-
recorder.record(
2634-
"jj",
2635-
format!("jj git fetch --remote origin --branch {}", current_branch),
2636-
);
2637-
if let Err(err) = jj_run_in(
2638-
repo_root,
2639-
&[
2640-
"--quiet",
2641-
"git",
2642-
"fetch",
2643-
"--remote",
2644-
"origin",
2645-
"--branch",
2646-
&current_branch,
2647-
],
2657+
for branch in origin_fetch_branches_for_jj_sync(
2658+
&current_branch,
2659+
origin_default_branch.as_deref(),
2660+
home_branch_sync_target.as_ref(),
26482661
) {
2649-
failures.push(format!("origin: {}", err));
2650-
} else {
2651-
fetched_any = true;
2652-
}
2653-
if let Some(default_branch) = origin_default_branch.as_deref() {
2654-
track_remote_ref(&mut tracked_refs, repo_root, "origin", default_branch);
2662+
track_remote_ref(&mut tracked_refs, repo_root, "origin", &branch);
26552663
recorder.record(
26562664
"jj",
2657-
format!("jj git fetch --remote origin --branch {}", default_branch),
2665+
format!("jj git fetch --remote origin --branch {}", branch),
26582666
);
26592667
if let Err(err) = jj_run_in(
26602668
repo_root,
26612669
&[
2662-
"--quiet",
2663-
"git",
2664-
"fetch",
2665-
"--remote",
2666-
"origin",
2667-
"--branch",
2668-
default_branch,
2670+
"--quiet", "git", "fetch", "--remote", "origin", "--branch", &branch,
26692671
],
26702672
) {
2671-
failures.push(format!("origin default {}: {}", default_branch, err));
2673+
failures.push(format!("origin {}: {}", branch, err));
26722674
} else {
26732675
fetched_any = true;
26742676
}
@@ -5034,6 +5036,31 @@ mod tests {
50345036
assert!(!home_branch_mode_active("nikiv", None));
50355037
}
50365038

5039+
#[test]
5040+
fn origin_fetch_branches_for_home_branch_mode_skips_local_home_branch() {
5041+
let home_target = HomeBranchSyncTarget {
5042+
home_branch: "nikiv".to_string(),
5043+
origin_default_branch: "main".to_string(),
5044+
};
5045+
5046+
assert_eq!(
5047+
origin_fetch_branches_for_jj_sync("nikiv", Some("main"), Some(&home_target)),
5048+
vec!["main".to_string()]
5049+
);
5050+
}
5051+
5052+
#[test]
5053+
fn origin_fetch_branches_for_regular_sync_dedupes_branches() {
5054+
assert_eq!(
5055+
origin_fetch_branches_for_jj_sync("feature/demo", Some("main"), None),
5056+
vec!["feature/demo".to_string(), "main".to_string()]
5057+
);
5058+
assert_eq!(
5059+
origin_fetch_branches_for_jj_sync("main", Some("main"), None),
5060+
vec!["main".to_string()]
5061+
);
5062+
}
5063+
50375064
#[test]
50385065
fn resolve_home_branch_sync_target_uses_origin_default_for_home_branch() {
50395066
let dir = tempdir().expect("tempdir");

0 commit comments

Comments
 (0)