Skip to content

Commit ff3fc89

Browse files
committed
fix(tags,stash): use epoch timestamp fallback to indicate data corruption
- Replace Utc::now() fallback with Unix epoch (1970-01-01) for invalid timestamps - Make data corruption obvious instead of silently masking with current time - Add test verification for epoch fallback behavior - Ensure consistent error handling pattern across both modules
1 parent ef84c86 commit ff3fc89

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/commands/stash.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,11 @@ fn parse_stash_line(index: usize, line: &str) -> Result<Stash> {
500500
let hash = Hash::from(parts[1]);
501501

502502
// Parse timestamp - if it fails, the stash metadata may be corrupted
503+
// Use Unix epoch as fallback to clearly indicate corrupted/invalid timestamp data
503504
let timestamp = parse_unix_timestamp(parts[2]).unwrap_or_else(|_| {
504505
// Timestamp parsing failed - this indicates malformed git stash metadata
505-
// Fall back to current time to avoid breaking the API, but this should be rare
506-
Utc::now()
506+
// Use Unix epoch (1970-01-01) as fallback to make data corruption obvious
507+
DateTime::from_timestamp(0, 0).unwrap_or_else(Utc::now)
507508
});
508509

509510
// Extract branch name and message from parts[3] (should be "On branch: message")
@@ -865,7 +866,9 @@ mod tests {
865866
assert_eq!(stash.branch, "master");
866867
assert_eq!(stash.message, "test message");
867868

868-
// The timestamp should be present (using fallback) but we can't test exact value
869-
// since it uses Utc::now() as fallback
869+
// The timestamp should use Unix epoch (1970-01-01) as fallback for invalid data
870+
// Verify fallback timestamp is Unix epoch (indicates data corruption)
871+
assert_eq!(stash.timestamp.timestamp(), 0); // Unix epoch
872+
assert_eq!(stash.timestamp.format("%Y-%m-%d").to_string(), "1970-01-01");
870873
}
871874
}

src/commands/tag.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ fn parse_for_each_ref_line(line: &str) -> Result<Tag> {
423423
let tagger =
424424
if tag_type == TagType::Annotated && !tagger_name.is_empty() && !tagger_email.is_empty() {
425425
// Parse the timestamp - if it fails, the tag metadata may be corrupted
426-
// We still create the Author but will handle timestamp separately below
426+
// Use Unix epoch as fallback to clearly indicate corrupted/invalid timestamp data
427427
let timestamp = parse_unix_timestamp(tagger_date).unwrap_or_else(|_| {
428428
// Timestamp parsing failed - this indicates malformed git metadata
429-
// Fall back to current time to avoid breaking the API, but this should be rare
430-
Utc::now()
429+
// Use Unix epoch (1970-01-01) as fallback to make data corruption obvious
430+
DateTime::from_timestamp(0, 0).unwrap_or_else(Utc::now)
431431
});
432432
Some(Author {
433433
name: tagger_name.to_string(),
@@ -791,10 +791,16 @@ mod tests {
791791
assert_eq!(tag.tag_type, TagType::Annotated);
792792
assert!(tag.tagger.is_some());
793793

794-
// The timestamp should be present (using fallback) but we can't test exact value
795-
// since it uses Utc::now() as fallback
794+
// The timestamp should use Unix epoch (1970-01-01) as fallback for invalid data
796795
let tagger = tag.tagger.unwrap();
797796
assert_eq!(tagger.name, "John Doe");
798797
assert_eq!(tagger.email, "[email protected]");
798+
799+
// Verify fallback timestamp is Unix epoch (indicates data corruption)
800+
assert_eq!(tagger.timestamp.timestamp(), 0); // Unix epoch
801+
assert_eq!(
802+
tagger.timestamp.format("%Y-%m-%d").to_string(),
803+
"1970-01-01"
804+
);
799805
}
800806
}

0 commit comments

Comments
 (0)