Skip to content

Commit

Permalink
format-util: remove Option from commit extras
Browse files Browse the repository at this point in the history
Summary: Now commit extras are supported by both Git and Hg format. Remove `Option`.

Reviewed By: MichaelCuevas

Differential Revision: D64943398

fbshipit-source-id: af8baac309024dda0c3f16f0a10d7913a7f2b003
  • Loading branch information
quark-zju authored and facebook-github-bot committed Oct 25, 2024
1 parent eb40652 commit c5b3d2e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
7 changes: 4 additions & 3 deletions eden/scm/lib/util/format-util/src/commit_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ pub trait CommitFields: Send + 'static {
Ok(None)
}

/// Returns `None` if the commit does not have extras.
fn extras(&self) -> Result<Option<&BTreeMap<Text, Text>>> {
Ok(None)
/// Extra metadata for this commit.
fn extras(&self) -> Result<&BTreeMap<Text, Text>> {
static EMPTY_TREE: BTreeMap<Text, Text> = BTreeMap::new();
Ok(&EMPTY_TREE)
}

/// Commit message encoded in UTF-8.
Expand Down
12 changes: 4 additions & 8 deletions eden/scm/lib/util/format-util/src/git_commit_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,9 @@ impl CommitFields for GitCommitLazyFields {
Ok(Some(self.fields()?.committer_date))
}

fn extras(&self) -> Result<Option<&BTreeMap<Text, Text>>> {
fn extras(&self) -> Result<&BTreeMap<Text, Text>> {
let extras = &self.fields()?.extras;
if extras.is_empty() {
Ok(None)
} else {
Ok(Some(extras))
}
Ok(extras)
}

fn parents(&self) -> Result<Option<&[Id20]>> {
Expand Down Expand Up @@ -383,7 +379,7 @@ Signed-off-by: Alice <a@example.com>
fields.root_tree().unwrap().to_hex(),
"98edb6a9c7a48cae7a1ed9a39600952547daaebb"
);
assert_eq!(format!("{:?}", fields.extras().unwrap()), "None");
assert_eq!(format!("{:?}", fields.extras().unwrap()), "{}");

let text2 = fields.fields().unwrap().to_text().unwrap();
assert_eq!(text2, text);
Expand Down Expand Up @@ -413,7 +409,7 @@ This is the commit message.
);

assert_eq!(
format!("{:?}", fields.extras().unwrap().unwrap()),
format!("{:?}", fields.extras().unwrap()),
r#"{"data1": "foo\nbar", "data2": "foo bar", "gpgsig": "-- BEGIN --\n\nsignature foo bar\n\n-- END --"}"#
);
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions eden/scm/lib/util/format-util/src/hg_commit_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ impl CommitFields for HgCommitLazyFields {
Ok(Some(&self.fields()?.files))
}

fn extras(&self) -> Result<Option<&BTreeMap<Text, Text>>> {
Ok(Some(&self.fields()?.extras))
fn extras(&self) -> Result<&BTreeMap<Text, Text>> {
Ok(&self.fields()?.extras)
}

fn description(&self) -> Result<&str> {
Expand Down Expand Up @@ -358,7 +358,7 @@ mod tests {
(1714200000, -28800)
);
assert_eq!(
format!("{:?}", fields.extras().unwrap().unwrap()),
format!("{:?}", fields.extras().unwrap()),
"{\"committer\": \"Bob \\\\ 2 <b@example.com>\", \"committer_date\": \"1714200000 -28800\"}"
);
assert_eq!(
Expand Down Expand Up @@ -400,7 +400,7 @@ mod tests {
(1714100000, 25200)
);
assert_eq!(fields.committer_date().unwrap(), None);
assert_eq!(format!("{:?}", fields.extras().unwrap().unwrap()), "{}");
assert_eq!(format!("{:?}", fields.extras().unwrap()), "{}");
assert_eq!(format!("{:?}", fields.files().unwrap().unwrap()), "[]");
assert_eq!(fields.description().unwrap(), "This is the commit message.");
assert_eq!(fields.raw_text(), text.as_bytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ py_class!(pub class CommitFields |py| {
Ok(files.map(|v| Serde(v.to_vec())))
}

/// Only provided by hg format. Parsed extras.
/// Returns `None` if not tracked in the commit text (i.e. git format).
def extras(&self) -> PyResult<Option<Serde<BTreeMap<Text, Text>>>> {
/// extras() -> Dict[str, str]
def extras(&self) -> PyResult<Serde<BTreeMap<Text, Text>>> {
let inner = self.inner(py);
let extras = inner.extras().map_pyerr(py)?;
Ok(extras.map(|v| Serde(v.clone())))
inner.extras().map_pyerr(py).map(|v| Serde(v.clone()))
}

/// Commit message encoded in UTF-8.
Expand Down

0 comments on commit c5b3d2e

Please sign in to comment.