3 files changed, +108, -49
8b68144Add RepoInfo that contains CommitInfo and move repo related stuff outside of main
author
mogi <git@paramogi.world> on 01-10-2026 19:49 UTC+1
committer
mogi <git@paramogi.world> on 01-10-2026 19:49 UTC+1
parent69c109d
+8, -0
    @@ -4,6 +4,7 @@ use std::fmt;
 4 4 pub enum WitError {
 5 5     Git(git2::Error),
 6 6     InvalidDate,
   7+    Io(std::io::Error),
 7 8 }
 8 9 
 910 impl fmt::Display for WitError {
    @@ -11,6 +12,7 @@ impl fmt::Display for WitError {
1112         match self {
1213             WitError::Git(e) => write!(f, "{}", e.message()),
1314             WitError::InvalidDate => write!(f, "invalid commit date"),
  15+            WitError::Io(e) => write!(f, "{e}"),
1416         }
1517     }
1618 }
    @@ -22,3 +24,9 @@ impl From<git2::Error> for WitError {
2224         WitError::Git(e)
2325     }
2426 }
  27+
  28+impl From<std::io::Error> for WitError {
  29+    fn from(e: std::io::Error) -> Self {
  30+        WitError::Io(e)
  31+    }
  32+}
+5, -42
    @@ -1,59 +1,22 @@
 1  -mod commit_info;
 2 1 mod error;
   2+mod repo_info;
 3 3 
 4  -use chrono::{DateTime, FixedOffset};
 5  -use git2::Repository;
   4+use repo_info::RepoInfo;
 6 5 use std::{env, process};
 7 6 
 8  -use commit_info::CommitInfo;
 9  -use error::WitError;
10  -
11 7 fn main() {
12 8     let Some(arg) = env::args().nth(1) else {
13 9         eprintln!("provide a path");
1410         process::exit(1);
1511     };
1612 
17  -    let commit_list = match handle_repo(&arg) {
18  -        Ok(list) => list,
  13+    let repo = match RepoInfo::open(&arg) {
  14+        Ok(repo) => repo,
1915         Err(e) => {
2016             eprintln!("{e}");
2117             process::exit(1);
2218         }
2319     };
24  -    for commit in commit_list {
25  -        println!("{}", commit);
26  -    }
27  -}
28  -
29  -fn handle_repo(repo_path: &str) -> Result<Vec<CommitInfo>, WitError> {
30  -    let repo = Repository::open_bare(repo_path)?;
31  -    let mut rev = repo.revwalk()?;
32  -    rev.push_head()?;
33  -
34  -    let mut commit_list = Vec::new();
35  -    for oid in rev {
36  -        let commit = repo.find_commit(oid?)?;
37  -
38  -        let id = commit.id().to_string();
39  -        let summary = commit.summary()?.unwrap_or_default().to_string();
40  -        let message = commit.message().unwrap_or_default().to_string();
41  -        let sig = commit.author();
42  -        let author_name = sig.name().unwrap_or_default().to_string();
43  -        let author_email = sig.email().unwrap_or_default().to_string();
44  -        let time = format_time(commit.time()).ok_or(WitError::InvalidDate)?;
45  -
46  -        commit_list.push(CommitInfo::new(id, summary, message, author_name, author_email, time));
47  -    }
48  -    Ok(commit_list)
49  -}
5020 
51  -fn format_time(time: git2::Time) -> Option<String> {
52  -    let offset = FixedOffset::east_opt(time.offset_minutes() * 60)?;
53  -    let date = DateTime::from_timestamp(time.seconds(), 0)?;
54  -    Some(
55  -        date.with_timezone(&offset)
56  -            .format("%Y-%m-%d %H:%M")
57  -            .to_string(),
58  -    )
  21+    println!("{repo}");
5922 }
      @@ -1,4 +1,8 @@
  1   -use std::fmt;
     1+use crate::error::WitError;
     2+
     3+use chrono::{DateTime, FixedOffset};
     4+use git2::Repository;
     5+use std::{fmt, fs};
  2  6 
  3  7 pub struct CommitInfo {
  4  8     pub id: String,
      @@ -9,9 +13,91 @@ pub struct CommitInfo {
  9 13     pub time: String,
 10 14 }
 11 15 
    16+pub struct RepoInfo {
    17+    pub description: String,
    18+    pub commit_list: Vec<CommitInfo>,
    19+}
    20+
    21+impl RepoInfo {
    22+    pub fn new(description: String, commit_list: Vec<CommitInfo>) -> Self {
    23+        RepoInfo {
    24+            description,
    25+            commit_list,
    26+        }
    27+    }
    28+
    29+    pub fn open(path: &str) -> Result<Self, WitError> {
    30+        let repo = Repository::open_bare(path)?;
    31+        let description_path = repo.path().join("description");
    32+        let description = fs::read_to_string(description_path)?.trim().to_string();
    33+        let commit_list = Self::handle_commits(&repo)?;
    34+        Ok(Self::new(description, commit_list))
    35+    }
    36+
    37+    fn handle_commits(repo: &Repository) -> Result<Vec<CommitInfo>, WitError> {
    38+        let mut rev = repo.revwalk()?;
    39+        rev.push_head()?;
    40+
    41+        let mut commit_list = Vec::new();
    42+        for oid in rev {
    43+            let commit = repo.find_commit(oid?)?;
    44+
    45+            let id = commit.id().to_string();
    46+            let summary = commit.summary()?.unwrap_or_default().to_string();
    47+            let message = commit.message().unwrap_or_default().to_string();
    48+            let sig = commit.author();
    49+            let author_name = sig.name().unwrap_or_default().to_string();
    50+            let author_email = sig.email().unwrap_or_default().to_string();
    51+            let time = Self::format_time(commit.time()).ok_or(WitError::InvalidDate)?;
    52+
    53+            commit_list.push(CommitInfo::new(
    54+                id,
    55+                summary,
    56+                message,
    57+                author_name,
    58+                author_email,
    59+                time,
    60+            ));
    61+        }
    62+        Ok(commit_list)
    63+    }
    64+
    65+    fn format_time(time: git2::Time) -> Option<String> {
    66+        let offset = FixedOffset::east_opt(time.offset_minutes() * 60)?;
    67+        let date = DateTime::from_timestamp(time.seconds(), 0)?;
    68+        Some(
    69+            date.with_timezone(&offset)
    70+                .format("%d-%m-%Y %H:%M")
    71+                .to_string(),
    72+        )
    73+    }
    74+}
    75+
    76+impl fmt::Display for RepoInfo {
    77+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    78+        writeln!(
    79+            f,
    80+            "\
    81+description: {}\n\
    82+last commit time: {}\n\
    83+",
    84+            self.description, self.commit_list[0].time
    85+        )?;
    86+        for commit in &self.commit_list {
    87+            write!(f, "{commit}")?;
    88+        }
    89+        Ok(())
    90+    }
    91+}
    92+
 12 93 impl CommitInfo {
 13 94     pub fn new(
 14   -        id: String, summary: String, message: String, author_name: String, author_email: String, time: String
    95+        id: String,
    96+        summary: String,
    97+        message: String,
    98+        author_name: String,
    99+        author_email: String,
   100+        time: String,
 15101     ) -> Self {
 16102         CommitInfo {
 17103             id,
      @@ -26,14 +112,16 @@ impl CommitInfo {
 26112 
 27113 impl fmt::Display for CommitInfo {
 28114     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 29   -        write!(f, "
 30   ----\n\
   115+        write!(
   116+            f,
   117+            "\
 31118 id: {}\n\
 32119 summary: {}\n\
 33120 message: {}\
 34121 author: {} <{}>\n\
 35   -time: {}\n\
 36   ----",
 37   -            self.id, self.summary, self.message, self.author_name, self.author_email, self.time)
   122+time: {}\
   123+",
   124+            self.id, self.summary, self.message, self.author_name, self.author_email, self.time
   125+        )
 38126     }
 39127 }