hf-hub 1.0.0

Rust client for the Hugging Face Hub API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Commit history, refs, and revision comparison helpers for repositories.
//!
//! This module groups three related workflows:
//!
//! - Use [`HFRepository::list_commits`] to walk commit history and [`HFRepository::list_refs`] to inspect branches,
//!   tags, convert refs, and optional pull-request refs.
//! - Use [`HFRepository::get_commit_diff`] for the Hub's non-raw compare payload as text.
//! - Use [`HFRepository::get_raw_diff`] for the full raw diff text, or [`HFRepository::get_raw_diff_stream`] to parse
//!   that raw diff incrementally into [`HFFileDiff`] entries.
//!
//! The same module also hosts branch and tag creation/deletion helpers because
//! they operate on the same repository revision namespace.

use bon::bon;
use futures::TryStreamExt;
use futures::stream::{Stream, StreamExt};
use serde::Deserialize;
use url::Url;

use super::diff::{self, HFFileDiff};
use super::{HFRepository, RepoType};
use crate::client::encode_ref;
use crate::error::HFResult;
use crate::{constants, retry};

/// Author entry attached to a commit, as returned by the commit history endpoint.
///
/// All fields are optional because the Hub only surfaces the identifying fields it has
/// (a linked Hub user, or the raw git name/email).
#[derive(Debug, Clone, Deserialize)]
pub struct CommitAuthor {
    /// Hub username, when the commit author is linked to a Hub account.
    pub user: Option<String>,
    /// Git author name as recorded on the commit.
    pub name: Option<String>,
    /// Git author email as recorded on the commit.
    pub email: Option<String>,
}

/// A single commit entry returned by the commit history endpoint.
///
/// Returned by [`HFRepository::list_commits`].
#[derive(Debug, Clone, Deserialize)]
pub struct GitCommitInfo {
    /// Full commit SHA.
    pub id: String,
    /// Commit authors as returned by the Hub.
    pub authors: Vec<CommitAuthor>,
    /// Commit timestamp in ISO 8601 format, when available.
    pub date: Option<String>,
    /// Commit title/summary line.
    pub title: String,
    /// Full commit message.
    pub message: String,
    /// HTML-formatted commit title. Returned only when the request asks the Hub to format the
    /// message (e.g., `?formatted=true`).
    #[serde(default, rename = "formattedTitle")]
    pub formatted_title: Option<String>,
    /// HTML-formatted commit message. Returned only when the request asks the Hub to format the
    /// message (e.g., `?formatted=true`).
    #[serde(default, rename = "formattedMessage")]
    pub formatted_message: Option<String>,
    /// Parent commit SHAs.
    #[serde(default)]
    pub parents: Vec<String>,
}

/// A single git ref (branch, tag, convert, or pull-request ref) and the commit it points to.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitRefInfo {
    /// Short ref name such as `"main"` or `"v1.0.0"`.
    pub name: String,
    /// Full git ref name such as `"refs/heads/main"`.
    #[serde(rename = "ref")]
    pub git_ref: String,
    /// Commit SHA the ref currently points to.
    pub target_commit: String,
}

/// All git refs on a repository — branches, tags, converts, and pull-request refs.
///
/// Returned by [`HFRepository::list_refs`].
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitRefs {
    /// Branch refs on the repository.
    pub branches: Vec<GitRefInfo>,
    /// Tag refs on the repository.
    pub tags: Vec<GitRefInfo>,
    /// Convert refs exposed by the Hub, when present.
    #[serde(default)]
    pub converts: Vec<GitRefInfo>,
    /// Pull-request refs, only populated when requested.
    #[serde(default, rename = "pullRequests")]
    pub pull_requests: Vec<GitRefInfo>,
}

/// A single file entry in the Hub's non-raw compare payload.
///
/// This type is useful if you want to deserialize the response body returned by
/// [`HFRepository::get_commit_diff`] yourself.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiffEntry {
    /// Destination path for the changed file, when present.
    pub path: Option<String>,
    /// Previous path for renames or moves, when present.
    pub old_path: Option<String>,
    /// Hub-provided status string for the change.
    pub status: Option<String>,
}

#[bon]
impl<T: RepoType> HFRepository<T> {
    /// Stream commit history for the repository at a given revision.
    ///
    /// Returns `HFResult<impl Stream<Item = HFResult<GitCommitInfo>>>`. Pagination is automatic.
    ///
    /// # Parameters
    ///
    /// - `revision`: Git revision (branch, tag, or commit SHA). Defaults to the main branch.
    /// - `limit`: maximum number of commits yielded.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn list_commits(
        &self,
        /// Git revision (branch, tag, or commit SHA). Defaults to the main branch.
        #[builder(into)]
        revision: Option<String>,
        /// Maximum number of commits yielded.
        limit: Option<usize>,
    ) -> HFResult<impl Stream<Item = HFResult<GitCommitInfo>> + '_> {
        let revision = revision.as_deref().unwrap_or(constants::DEFAULT_REVISION);
        let url_str = format!(
            "{}/commits/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(revision)
        );
        let url = Url::parse(&url_str)?;
        Ok(self.hf_client.paginate(url, vec![], limit))
    }

    /// Fetch all branches, tags, and optionally pull-request refs for the repository.
    ///
    /// Endpoint: `GET /api/{repo_type}s/{repo_id}/refs`.
    ///
    /// # Parameters
    ///
    /// - `include_pull_requests` (default `false`): include pull-request refs in the listing.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn list_refs(
        &self,
        /// Include pull-request refs in the listing.
        #[builder(default)]
        include_pull_requests: bool,
    ) -> HFResult<GitRefs> {
        let url = format!("{}/refs", self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()));
        let mut query: Vec<(&str, String)> = Vec::new();
        if include_pull_requests {
            query.push(("include_prs", "1".into()));
        }

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client
                .http_client()
                .get(&url)
                .headers(headers.clone())
                .query(&query)
                .send()
        })
        .await?;

        let repo_path = self.repo_path();
        let response = self
            .hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(response.json().await?)
    }

    /// Fetch the Hub's non-raw compare payload as text.
    ///
    /// This returns the response body from the standard `/compare/{compare}` endpoint. Use
    /// [`HFRepository::get_raw_diff`] for raw git-style diff text or
    /// [`HFRepository::get_raw_diff_stream`] for parsed [`HFFileDiff`] entries.
    ///
    /// Endpoint: `GET /api/{repo_type}s/{repo_id}/compare/{compare}`.
    ///
    /// # Parameters
    ///
    /// - `compare` (required): revision spec describing what to compare. Either:
    ///   - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`, `"v1.0"`,
    ///     `"abc123…"`), or
    ///   - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
    ///     `"<sha1>..<sha2>"`).
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn get_commit_diff(
        &self,
        /// Revision spec describing what to compare. Either:
        /// - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`,
        ///   `"v1.0"`, `"abc123…"`), or
        /// - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
        ///   `"<sha1>..<sha2>"`).
        compare: &str,
    ) -> HFResult<String> {
        let url = format!(
            "{}/compare/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(compare)
        );

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client.http_client().get(&url).headers(headers.clone()).send()
        })
        .await?;

        let repo_path = self.repo_path();
        let response = self
            .hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(response.text().await?)
    }

    /// Fetch the raw diff payload between two revisions as a string.
    ///
    /// Prefer [`HFRepository::get_raw_diff_stream`] when you want file-level metadata without
    /// buffering the entire diff response in memory.
    ///
    /// Endpoint: `GET /api/{repo_type}s/{repo_id}/compare/{compare}?raw=true`.
    ///
    /// # Parameters
    ///
    /// - `compare` (required): revision spec describing what to compare. Either:
    ///   - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`, `"v1.0"`,
    ///     `"abc123…"`), or
    ///   - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
    ///     `"<sha1>..<sha2>"`).
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn get_raw_diff(
        &self,
        /// Revision spec describing what to compare. Either:
        /// - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`,
        ///   `"v1.0"`, `"abc123…"`), or
        /// - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
        ///   `"<sha1>..<sha2>"`).
        compare: &str,
    ) -> HFResult<String> {
        let url = format!(
            "{}/compare/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(compare)
        );

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client
                .http_client()
                .get(&url)
                .headers(headers.clone())
                .query(&[("raw", "true")])
                .send()
        })
        .await?;

        let repo_path = self.repo_path();
        let response = self
            .hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(response.text().await?)
    }

    /// Fetch the raw diff between two revisions as a parsed stream of [`HFFileDiff`] entries.
    ///
    /// Each `Ok` item is one parsed diff entry; malformed lines are `Err` items.
    ///
    /// Endpoint: `GET /api/{repo_type}s/{repo_id}/compare/{compare}?raw=true`.
    ///
    /// # Parameters
    ///
    /// - `compare` (required): revision spec describing what to compare. Either:
    ///   - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`, `"v1.0"`,
    ///     `"abc123…"`), or
    ///   - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
    ///     `"<sha1>..<sha2>"`).
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn get_raw_diff_stream(
        &self,
        /// Revision spec describing what to compare. Either:
        /// - A single revision (branch name, tag, or commit SHA), compared against its parent (e.g., `"main"`,
        ///   `"v1.0"`, `"abc123…"`), or
        /// - Two revisions in `<base>..<head>` form (two dots), comparing `base` to `head` (e.g., `"main..feature"`,
        ///   `"<sha1>..<sha2>"`).
        #[builder(into)]
        compare: String,
    ) -> HFResult<impl Stream<Item = HFResult<HFFileDiff>> + '_> {
        let url = format!(
            "{}/compare/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(&compare)
        );

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client
                .http_client()
                .get(&url)
                .headers(headers.clone())
                .query(&[("raw", "true")])
                .send()
        })
        .await?;

        let repo_path = self.repo_path();
        let response = self
            .hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        let byte_stream = response.bytes_stream().map(|r| r.map_err(std::io::Error::other));
        Ok(diff::stream_raw_diff(byte_stream).map_err(Into::into))
    }

    /// Create a new branch, optionally starting from a specific revision.
    ///
    /// Endpoint: `POST /api/{repo_type}s/{repo_id}/branch/{branch}`.
    ///
    /// # Parameters
    ///
    /// - `branch` (required): name of the branch to create.
    /// - `revision`: revision to branch from. Defaults to the current main branch head.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn create_branch(
        &self,
        /// Name of the branch to create.
        branch: &str,
        /// Revision to branch from. Defaults to the current main branch head.
        #[builder(into)]
        revision: Option<String>,
    ) -> HFResult<()> {
        let url = format!(
            "{}/branch/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(branch)
        );

        let mut body = serde_json::Map::new();
        if let Some(rev) = revision {
            body.insert("startingPoint".into(), serde_json::Value::String(rev));
        }

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client
                .http_client()
                .post(&url)
                .headers(headers.clone())
                .json(&body)
                .send()
        })
        .await?;

        let repo_path = self.repo_path();
        self.hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(())
    }

    /// Delete a branch from the repository.
    ///
    /// Endpoint: `DELETE /api/{repo_type}s/{repo_id}/branch/{branch}`.
    ///
    /// # Parameters
    ///
    /// - `branch` (required): name of the branch to delete.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn delete_branch(
        &self,
        /// Name of the branch to delete.
        branch: &str,
    ) -> HFResult<()> {
        let url = format!(
            "{}/branch/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(branch)
        );

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client.http_client().delete(&url).headers(headers.clone()).send()
        })
        .await?;

        let repo_path = self.repo_path();
        self.hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(())
    }

    /// Create a lightweight or annotated tag, optionally at a specific revision.
    ///
    /// Endpoint: `POST /api/{repo_type}s/{repo_id}/tag/{revision}`.
    ///
    /// # Parameters
    ///
    /// - `tag` (required): name of the tag to create.
    /// - `revision`: revision to tag. Defaults to the current main branch head.
    /// - `message`: annotation message for the tag.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn create_tag(
        &self,
        /// Name of the tag to create.
        tag: &str,
        /// Revision to tag. Defaults to the current main branch head.
        revision: Option<&str>,
        /// Annotation message for the tag.
        #[builder(into)]
        message: Option<String>,
    ) -> HFResult<()> {
        let revision = revision.unwrap_or(constants::DEFAULT_REVISION);
        let url = format!(
            "{}/tag/{}",
            self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()),
            encode_ref(revision)
        );

        let mut body = serde_json::json!({ "tag": tag });
        if let Some(m) = message {
            body["message"] = serde_json::Value::String(m);
        }

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client
                .http_client()
                .post(&url)
                .headers(headers.clone())
                .json(&body)
                .send()
        })
        .await?;

        let repo_path = self.repo_path();
        self.hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(())
    }

    /// Delete a tag from the repository.
    ///
    /// Endpoint: `DELETE /api/{repo_type}s/{repo_id}/tag/{tag}`.
    ///
    /// # Parameters
    ///
    /// - `tag` (required): name of the tag to delete.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub async fn delete_tag(
        &self,
        /// Name of the tag to delete.
        tag: &str,
    ) -> HFResult<()> {
        let url =
            format!("{}/tag/{}", self.hf_client.api_url(self.repo_type.plural(), &self.repo_path()), encode_ref(tag));

        let headers = self.hf_client.auth_headers();
        let response = retry::retry(self.hf_client.retry_config(), || {
            self.hf_client.http_client().delete(&url).headers(headers.clone()).send()
        })
        .await?;

        let repo_path = self.repo_path();
        self.hf_client
            .check_response(response, Some(&repo_path), crate::error::NotFoundContext::Repo)
            .await?;
        Ok(())
    }
}

#[cfg(feature = "blocking")]
#[bon]
impl<T: RepoType> crate::blocking::HFRepositorySync<T> {
    /// Blocking counterpart of [`HFRepository::list_commits`]. Collects the stream into a
    /// `Vec<GitCommitInfo>`. See the async method for parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn list_commits(
        &self,
        #[builder(into)] revision: Option<String>,
        limit: Option<usize>,
    ) -> HFResult<Vec<GitCommitInfo>> {
        self.runtime.block_on(async move {
            let stream = self.inner.list_commits().maybe_revision(revision).maybe_limit(limit).send()?;
            futures::pin_mut!(stream);
            let mut items = Vec::new();
            while let Some(item) = stream.next().await {
                items.push(item?);
            }
            Ok(items)
        })
    }

    /// Blocking counterpart of [`HFRepository::list_refs`]. See the async method for parameters
    /// and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn list_refs(&self, #[builder(default)] include_pull_requests: bool) -> HFResult<GitRefs> {
        self.runtime
            .block_on(self.inner.list_refs().include_pull_requests(include_pull_requests).send())
    }

    /// Blocking counterpart of [`HFRepository::get_commit_diff`]. See the async method for
    /// parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn get_commit_diff(&self, compare: &str) -> HFResult<String> {
        self.runtime.block_on(self.inner.get_commit_diff().compare(compare).send())
    }

    /// Blocking counterpart of [`HFRepository::get_raw_diff`]. See the async method for
    /// parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn get_raw_diff(&self, compare: &str) -> HFResult<String> {
        self.runtime.block_on(self.inner.get_raw_diff().compare(compare).send())
    }

    /// Blocking counterpart of [`HFRepository::get_raw_diff_stream`]. Collects the parsed stream
    /// into a `Vec<HFFileDiff>`. See the async method for parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn get_raw_diff_stream(&self, #[builder(into)] compare: String) -> HFResult<Vec<HFFileDiff>> {
        self.runtime.block_on(async move {
            let stream = self.inner.get_raw_diff_stream().compare(compare).send().await?;
            futures::pin_mut!(stream);
            let mut items = Vec::new();
            while let Some(item) = stream.next().await {
                items.push(item?);
            }
            Ok(items)
        })
    }

    /// Blocking counterpart of [`HFRepository::create_branch`]. See the async method for
    /// parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn create_branch(&self, branch: &str, #[builder(into)] revision: Option<String>) -> HFResult<()> {
        self.runtime
            .block_on(self.inner.create_branch().branch(branch).maybe_revision(revision).send())
    }

    /// Blocking counterpart of [`HFRepository::delete_branch`]. See the async method for
    /// parameters and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn delete_branch(&self, branch: &str) -> HFResult<()> {
        self.runtime.block_on(self.inner.delete_branch().branch(branch).send())
    }

    /// Blocking counterpart of [`HFRepository::create_tag`]. See the async method for parameters
    /// and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn create_tag(
        &self,
        tag: &str,
        revision: Option<&str>,
        #[builder(into)] message: Option<String>,
    ) -> HFResult<()> {
        self.runtime.block_on(
            self.inner
                .create_tag()
                .tag(tag)
                .maybe_revision(revision)
                .maybe_message(message)
                .send(),
        )
    }

    /// Blocking counterpart of [`HFRepository::delete_tag`]. See the async method for parameters
    /// and behavior.
    #[builder(finish_fn = send, derive(Debug, Clone))]
    pub fn delete_tag(&self, tag: &str) -> HFResult<()> {
        self.runtime.block_on(self.inner.delete_tag().tag(tag).send())
    }
}

#[cfg(test)]
mod tests {
    use super::GitCommitInfo;

    #[test]
    fn test_git_commit_info_with_formatted_fields() {
        let json = r#"{
            "id":"abc123",
            "authors":[{"user":"u","name":"User","email":"u@x"}],
            "date":"2025-01-01T00:00:00Z",
            "title":"feat: add thing",
            "message":"feat: add thing\n\nLong body.",
            "formattedTitle":"<p>feat: add thing</p>",
            "formattedMessage":"<p>feat: add thing</p><p>Long body.</p>",
            "parents":["p1"]
        }"#;
        let info: GitCommitInfo = serde_json::from_str(json).unwrap();
        assert_eq!(info.formatted_title.as_deref(), Some("<p>feat: add thing</p>"));
        assert_eq!(info.formatted_message.as_deref(), Some("<p>feat: add thing</p><p>Long body.</p>"));
        assert_eq!(info.parents, vec!["p1"]);
    }

    #[test]
    fn test_git_commit_info_without_formatted_fields() {
        let json = r#"{"id":"abc","authors":[],"date":null,"title":"t","message":"m"}"#;
        let info: GitCommitInfo = serde_json::from_str(json).unwrap();
        assert!(info.formatted_title.is_none());
        assert!(info.formatted_message.is_none());
    }
}