diff --git a/src/forge_cli.rs b/src/forge_cli.rs index 8ec832c..21e32a8 100644 --- a/src/forge_cli.rs +++ b/src/forge_cli.rs @@ -223,6 +223,25 @@ fn n(v: &Value, key: &str) -> i64 { v.get(key).and_then(Value::as_i64).unwrap_or(0) } +/// Who wrote an issue / PR / comment. The forge returns `author_login` +/// (joined from its users table); `author_name`, an embedded `author` +/// object/string, or `user.login` are accepted too. Deleted accounts show as +/// `ghost`, matching the web UI. +fn author(v: &Value) -> &str { + let direct = ["author_login", "author_name", "author", "user_login"] + .iter() + .find_map(|key| v.get(*key).and_then(Value::as_str).filter(|a| !a.is_empty())); + let nested = || { + ["author", "user"].iter().find_map(|key| { + v.get(*key) + .and_then(|o| o.get("login").or_else(|| o.get("username"))) + .and_then(Value::as_str) + .filter(|a| !a.is_empty()) + }) + }; + direct.or_else(nested).unwrap_or("ghost") +} + /// `2026-09-24T10:11:12Z` -> `2026-09-24`. fn day(ts: &str) -> &str { ts.get(..10).unwrap_or(ts) @@ -249,7 +268,7 @@ fn print_comments(forge: &ForgeRepo, sub: &str) -> Result<()> { println!("{} comment(s):", list.len()); for c in &list { println!(); - println!(" -- {} --", day(s(c, "created_at"))); + println!(" -- {}, {} --", author(c), day(s(c, "created_at"))); for line in s(c, "body").lines() { println!(" {line}"); } @@ -268,10 +287,11 @@ pub fn cmd_issue(repo_path: &Path, args: &IssueArgs) -> Result<()> { } for issue in &list { println!( - "#{:<5} {:<7} {} {}", + "#{:<5} {:<7} {} {:<16} {}", n(issue, "number"), s(issue, "state"), day(s(issue, "created_at")), + author(issue), s(issue, "title") ); } @@ -280,9 +300,10 @@ pub fn cmd_issue(repo_path: &Path, args: &IssueArgs) -> Result<()> { let issue = forge.api("GET", &format!("/issues/{number}"), None)?; println!("#{} {}", n(&issue, "number"), s(&issue, "title")); println!( - "state: {} opened: {} comments: {}", + "state: {} opened: {} by {} comments: {}", s(&issue, "state"), day(s(&issue, "created_at")), + author(&issue), n(&issue, "comment_count") ); let join = |key: &str| { @@ -357,9 +378,10 @@ pub fn cmd_pr(repo_path: &Path, args: &PrArgs) -> Result<()> { } for pr in &list { println!( - "#{:<5} {:<7} {} -> {} {}", + "#{:<5} {:<7} {:<16} {} -> {} {}", n(pr, "number"), s(pr, "state"), + author(pr), s(pr, "head_branch"), s(pr, "base_branch"), s(pr, "title") @@ -370,11 +392,12 @@ pub fn cmd_pr(repo_path: &Path, args: &PrArgs) -> Result<()> { let pr = forge.api("GET", &format!("/pulls/{number}"), None)?; println!("#{} {}", n(&pr, "number"), s(&pr, "title")); println!( - "state: {} {} -> {} opened: {}", + "state: {} {} -> {} opened: {} by {}", s(&pr, "state"), s(&pr, "head_branch"), s(&pr, "base_branch"), - day(s(&pr, "created_at")) + day(s(&pr, "created_at")), + author(&pr) ); let mergeable = match pr.get("mergeable").and_then(Value::as_bool) { Some(true) => "yes", diff --git a/src/main.rs b/src/main.rs index e523571..dc22038 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1362,11 +1362,18 @@ struct PushArgs { /// Remote to push to. #[arg(default_value = "origin")] remote: String, - /// `[:]` (default: the current branch). `:` deletes ``. - refspec: Option, - /// Allow a non-fast-forward update. + /// `[:]` refspecs (default: the current branch). `:` + /// deletes ``; with `--delete` these are plain ref names to delete. + refspecs: Vec, + /// Allow a non-fast-forward update (or moving an existing remote tag). #[arg(short, long)] force: bool, + /// Delete the named refs on the remote (`rit push origin --delete feature`). + #[arg(short = 'd', long, conflicts_with = "tags")] + delete: bool, + /// Push every local tag (in addition to any refspecs given). + #[arg(long)] + tags: bool, } /// Derive the default clone directory from a URL (`.../repo.git` -> `repo`). @@ -1484,14 +1491,38 @@ fn cmd_pull(cli: &Cli, args: &PullArgs) -> Result<()> { fn cmd_push(cli: &Cli, args: &PushArgs) -> Result<()> { let repo = Repository::discover(&cli.repo) .with_context(|| format!("opening repo at {}", cli.repo.display()))?; - let refspec = match &args.refspec { - Some(r) => r.clone(), - None => current_branch(&repo)?, - }; - let report = rit::remote::push(&repo, &args.remote, &refspec, args.force)?; - if report.up_to_date { - println!("Everything up-to-date"); + let mut refspecs: Vec = if args.delete { + if args.refspecs.is_empty() { + anyhow::bail!("--delete needs at least one ref name (e.g. `rit push origin --delete feature`)"); + } + args.refspecs + .iter() + .map(|name| { + let name = name.trim(); + if name.is_empty() || name.contains(':') { + anyhow::bail!("--delete takes plain ref names, got '{name}'"); + } + Ok(format!(":{name}")) + }) + .collect::>()? } else { + args.refspecs.clone() + }; + if refspecs.is_empty() && !args.tags { + refspecs.push(current_branch(&repo)?); + } + let mut reports = Vec::new(); + for refspec in &refspecs { + reports.push(rit::remote::push(&repo, &args.remote, refspec, args.force)?); + } + if args.tags { + reports.extend(rit::remote::push_tags(&repo, &args.remote, args.force)?); + } + if reports.iter().all(|r| r.up_to_date) { + println!("Everything up-to-date"); + return Ok(()); + } + for report in reports.iter().filter(|r| !r.up_to_date) { match (report.old, report.new) { (_, None) => println!(" - [deleted] {}", report.remote_ref), (None, Some(new)) => println!(" * [new] {} -> {}", &new.to_string()[..8], report.remote_ref), diff --git a/src/remote.rs b/src/remote.rs index cf8815f..61c9ad5 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -742,7 +742,14 @@ pub struct PushReport { pub up_to_date: bool, } -/// `rit push [:]` (or `:` to delete a remote branch). +/// `rit push [:]` (or `:` to delete a remote ref). +/// +/// Unqualified names are qualified the way git does it: +/// - a destination for an existing local TAG (and no same-named branch) goes to +/// `refs/tags/`, never to a branch of the same name; +/// - a deletion (`:`) targets whichever of `refs/heads/` / +/// `refs/tags/` the remote advertises, and fails when neither exists; +/// - everything else goes to `refs/heads/`. pub fn push(repo: &Repository, remote: &str, refspec: &str, force: bool) -> Result { let remote_cfg = get_remote(repo, remote)?; let endpoint = resolve_endpoint(&remote_cfg.url)?; @@ -750,29 +757,80 @@ pub fn push(repo: &Repository, remote: &str, refspec: &str, force: bool) -> Resu Some((s, d)) => (s.to_string(), d.to_string()), None => (refspec.to_string(), refspec.to_string()), }; - let qualify = |name: &str| { - if name.starts_with("refs/") { name.to_string() } else { format!("refs/heads/{name}") } - }; - let dst = qualify(&dst); - let new = if src.is_empty() { - None - } else { - Some(repo.resolve(&src).with_context(|| format!("resolving '{src}'"))?) + let (src, force) = match src.strip_prefix('+') { + Some(stripped) => (stripped.to_string(), true), + None => (src, force), }; + if dst.is_empty() { + bail!("invalid refspec '{refspec}': missing destination"); + } let adv = discover(&endpoint, "git-receive-pack")?; + let advertised = |name: &str| adv.refs.iter().any(|(n, _)| n == name); + + let (dst, new) = if src.is_empty() { + // Deletion: the ref must exist on the remote. + let dst = if dst.starts_with("refs/") { + dst + } else if advertised(&format!("refs/heads/{dst}")) { + format!("refs/heads/{dst}") + } else if advertised(&format!("refs/tags/{dst}")) { + format!("refs/tags/{dst}") + } else { + format!("refs/heads/{dst}") + }; + if !advertised(&dst) { + bail!("unable to delete '{dst}': remote ref does not exist"); + } + (dst, None) + } else { + let local_tag = if src.starts_with("refs/tags/") { + Some(src.clone()) + } else if !src.starts_with("refs/") + && repo.refs().resolve(&format!("refs/tags/{src}")).is_ok() + && repo.refs().resolve(&format!("refs/heads/{src}")).is_err() + { + Some(format!("refs/tags/{src}")) + } else { + None + }; + // A tag is pushed as its own (possibly annotated) object, unpeeled. + let new = match &local_tag { + Some(tag_ref) => repo + .refs() + .resolve(tag_ref) + .with_context(|| format!("resolving tag '{tag_ref}'"))?, + None => repo.resolve(&src).with_context(|| format!("resolving '{src}'"))?, + }; + let dst = if dst.starts_with("refs/") { + dst + } else if local_tag.is_some() { + format!("refs/tags/{dst}") + } else { + format!("refs/heads/{dst}") + }; + (dst, Some(new)) + }; + let old = adv.refs.iter().find(|(n, _)| *n == dst).map(|(_, o)| *o); if old == new { return Ok(PushReport { remote_ref: dst, old, new, objects: 0, up_to_date: true }); } - if let (Some(old), Some(new)) = (old, new) { - if !repo.odb().contains(&old) { - bail!("the remote {dst} has commits you do not have; run `rit fetch {remote}` and merge first"); + if let (Some(_), Some(_)) = (old, new) { + if dst.starts_with("refs/tags/") && !force { + bail!("rejected: tag {dst} already exists on the remote (pass --force to move it)"); } - if !force - && !crate::fsck::is_ancestor(repo.odb(), old, new, crate::fsck::ConnectivityLimits::default())? - { - bail!("rejected: {dst} is not a fast-forward (fetch and merge, or pass --force)"); + } + if let (Some(old), Some(new)) = (old, new) { + if !dst.starts_with("refs/tags/") { + if !repo.odb().contains(&old) { + bail!("the remote {dst} has commits you do not have; run `rit fetch {remote}` and merge first"); + } + if !force + && !crate::fsck::is_ancestor(repo.odb(), old, new, crate::fsck::ConnectivityLimits::default())? + { + bail!("rejected: {dst} is not a fast-forward (fetch and merge, or pass --force)"); + } } } @@ -844,6 +902,24 @@ pub fn push(repo: &Repository, remote: &str, refspec: &str, force: bool) -> Resu Ok(PushReport { remote_ref: dst, old, new, objects, up_to_date: false }) } +/// `rit push --tags`: push every local tag the remote does not already have at +/// the same object. Existing remote tags pointing elsewhere are rejected +/// unless `force` (tags are not expected to move). +pub fn push_tags(repo: &Repository, remote: &str, force: bool) -> Result> { + let tags: Vec = repo + .refs() + .list()? + .into_iter() + .map(|(name, _)| name) + .filter(|name| name.starts_with("refs/tags/")) + .collect(); + let mut reports = Vec::with_capacity(tags.len()); + for tag in tags { + reports.push(push(repo, remote, &format!("{tag}:{tag}"), force)?); + } + Ok(reports) +} + fn check_report_status(response: &[u8], dst: &str) -> Result<()> { if response.is_empty() { bail!("server sent no push status");