Skip to content

Commit

Permalink
format-util: drop uninteresting extras when writing git commit
Browse files Browse the repository at this point in the history
Summary:
Drop redundant or noisy fields so we match the typical Git commit expectation
that in most cases there are no extras.

This avoids writing `author_date`, `rebase_source` out that changes some commit
hashes in tests so some broken tests get fixed.

Reviewed By: muirdm

Differential Revision: D64909107

fbshipit-source-id: b21d2631219c23bb4cf9126aa225abded4f09b91
  • Loading branch information
quark-zju authored and facebook-github-bot committed Oct 24, 2024
1 parent 9afa4b3 commit 2a6d0b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions eden/scm/lib/util/format-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ once_cell = "1.12"
serde = { version = "1.0.185", features = ["derive", "rc"] }
sha1 = "0.10.5"
storemodel = { version = "0.1.0", path = "../../storemodel" }
tracing = { version = "0.1.40", features = ["attributes", "valuable"] }
types = { version = "0.1.0", path = "../../types" }
1 change: 1 addition & 0 deletions eden/scm/lib/util/format-util/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust_library(
"fbsource//third-party/rust:once_cell",
"fbsource//third-party/rust:serde",
"fbsource//third-party/rust:sha1",
"fbsource//third-party/rust:tracing",
"//eden/scm/lib/minibytes:minibytes",
"//eden/scm/lib/storemodel:storemodel",
"//eden/scm/lib/types:types",
Expand Down
38 changes: 34 additions & 4 deletions eden/scm/lib/util/format-util/src/git_commit_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,38 @@ fn write_message(message: &str, out: &mut String) -> Result<()> {
Ok(())
}

// redundant with actual fields: author*, committer*
// not interesting for git: *source, mut*, branch
const SORTED_IGNORED_EXTRA_NAMES: &[&str] = &[
"amend_source",
"author",
"author_date",
"branch",
"committer",
"committer_date",
"histedit_source",
"intermediate-source",
"mutdate",
"mutop",
"mutpred",
"mutsplit",
"mutuser",
"rebase_source",
"source",
"transplant_source",
];

fn write_extra(name: &str, value: &str, out: &mut String) -> Result<()> {
if name == "committer" || name == "committer_date" || name == "branch" {
// "committer" was written before; "branch" is useless - just skip it.
if SORTED_IGNORED_EXTRA_NAMES.binary_search(&name).is_ok() {
tracing::trace!(name, value, "ignored commit extra field");
return Ok(());
}
let bad_extra_names = ["author", "parent", "tree"];

let bad_extra_names = ["parent", "tree"];
ensure!(
!name.contains("\n") && !name.contains(" ") && bad_extra_names.iter().all(|&n| n != name),
"invalid extra name"
"invalid extra: {:?}",
name
);
out.push_str(name);
let empty = write_multi_line(value, " ", out)?;
Expand Down Expand Up @@ -424,4 +447,11 @@ m
// Test root_tree() after "fields" being calculated.
assert!(fields.root_tree().unwrap().is_null());
}

#[test]
fn test_sorted_ignored_extras_are_actually_sorted() {
let mut v = SORTED_IGNORED_EXTRA_NAMES.to_vec();
v.sort_unstable();
assert_eq!(v, SORTED_IGNORED_EXTRA_NAMES);
}
}

0 comments on commit 2a6d0b6

Please sign in to comment.