From 98a6e693799a58ebe6e36feeb780c407a9ebe478 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 9 May 2023 17:40:15 +0000 Subject: [PATCH] git-ext: Display/parse timezone offset in the format git2 expects. The previous implementation of Display for Time is shown as absolute minutes, whereas git expects HH:MM format. The FromStr implementation for Author also did not correctly convert +/-HHMM to absolute minutes. Signed-off-by: xphoniex Signed-off-by: Fintan Halpenny --- radicle-git-ext/src/author.rs | 16 ++++++++++++++-- radicle-git-ext/t/src/commit.rs | 23 ++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/radicle-git-ext/src/author.rs b/radicle-git-ext/src/author.rs index 34a5d62..eb34a64 100644 --- a/radicle-git-ext/src/author.rs +++ b/radicle-git-ext/src/author.rs @@ -60,7 +60,9 @@ impl From for Time { impl fmt::Display for Time { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let sign = if self.offset.is_negative() { '-' } else { '+' }; - write!(f, "{} {}{:0>4}", self.seconds, sign, self.offset.abs()) + let hours = self.offset.abs() / 60; + let minutes = self.offset.abs() % 60; + write!(f, "{} {}{:0>2}{:0>2}", self.seconds, sign, hours, minutes) } } @@ -110,7 +112,17 @@ impl FromStr for Author { let mut components = s.split(' '); let offset = match components.next_back() { None => return Err(ParseError::Missing("offset")), - Some(offset) => offset.parse::().map_err(ParseError::Offset)?, + Some(offset) => { + // The offset is in the form of timezone offset, + // e.g. +0200, -0100. This needs to be converted into + // minutes. The first two digits in the offset are the + // number of hours in the offset, while the latter two + // digits are the number of minutes in the offset. + let tz_offset = offset.parse::().map_err(ParseError::Offset)?; + let hours = tz_offset / 100; + let minutes = tz_offset % 100; + hours * 60 + minutes + }, }; let time = match components.next_back() { None => return Err(ParseError::Missing("time")), diff --git a/radicle-git-ext/t/src/commit.rs b/radicle-git-ext/t/src/commit.rs index f6719ca..6ea1427 100644 --- a/radicle-git-ext/t/src/commit.rs +++ b/radicle-git-ext/t/src/commit.rs @@ -1,4 +1,4 @@ -use std::{io, str::FromStr as _}; +use std::{io, str::FromStr as _, string::ToString as _}; use radicle_git_ext::{ author::{self, Author}, @@ -242,3 +242,24 @@ fn write_valid_commit() { .write(&repo); assert!(valid.is_ok()) } + +#[test] +fn author_roundtrip() { + let author = "author Fintan Halpenny 1669292989 +0000"; + assert_eq!( + author.parse::().unwrap().to_string(), + author.to_string() + ); + + let author = "author Alexis Sellier 1664467633 +0200"; + assert_eq!( + author.parse::().unwrap().to_string(), + author.to_string() + ); + + let author = "author Alexis Sellier 1664467633 -0200"; + assert_eq!( + author.parse::().unwrap().to_string(), + author.to_string() + ); +}