GitInfo

GitInfo 方法在 Page 物件上返回一個具有額外方法的物件。

前置條件

安裝 Git,創建一個倉庫並提交項目文件。

你還必須允許 Hugo 訪問你的倉庫。在你的網站配置中:

hugo.
     
enableGitInfo: true
enableGitInfo = true
{
   "enableGitInfo": true
}

或者,使用命令行參數在構建你的網站時啟用它:

hugo --enableGitInfo

方法

AbbreviatedHash

(string) 簡短的提交哈希。

{{ with .GitInfo }}
  {{ .AbbreviatedHash }} → aab9ec0b3
{{ end }}
AuthorDate

(time.Time) 作者日期。

{{ with .GitInfo }}
  {{ .AuthorDate.Format "2006-01-02" }} → 2023-10-09
{{ end }}
AuthorEmail

(string) 作者的電子郵件地址,遵循 gitmailmap

{{ with .GitInfo }}
  {{ .AuthorEmail }} → jsmith@example.org
{{ end }}
AuthorName

(string) 作者的名字,遵循 gitmailmap

{{ with .GitInfo }}
  {{ .AuthorName }} → John Smith
{{ end }}
CommitDate

(time.Time) 提交日期。

{{ with .GitInfo }}
  {{ .CommitDate.Format "2006-01-02" }} → 2023-10-09
{{ end }}
Hash

(string) 提交哈希。

{{ with .GitInfo }}
  {{ .Hash }} → aab9ec0b31ebac916a1468c4c9c305f2bebf78d4
{{ end }}
Subject

(string) 提交信息主題。

{{ with .GitInfo }}
  {{ .Subject }} → Add tutorials
{{ end }}
Body

(string) 提交信息正文。

{{ with .GitInfo }}
  {{ .Body }} → - Two new pages added.
{{ end }}

最後修改日期

默認情況下,當 enableGitInfo 設置為 true 時,Page 物件上的 Lastmod 方法返回的是包含該文件的最後提交的 Git AuthorDate。

你可以在 網站配置 中更改此行為。

託管考慮事項

當你在 CI/CD 環境中託管你的網站時,用於克隆項目倉庫的步驟必須進行深度克隆。如果克隆為淺克隆,則給定文件的 Git 資訊可能不準確——它可能反映的是最近的倉庫提交,而不是最後修改該文件的提交。

一些提供商默認進行深度克隆,其他提供商允許你配置克隆深度,還有一些提供商僅執行淺克隆。

託管服務 默認克隆深度 可配置
AWS Amplify 深度 不適用
Cloudflare Pages 淺克隆 1
DigitalOcean App Platform 深度 不適用
GitHub Pages 淺克隆 2
GitLab Pages 淺克隆 3
Netlify 深度 不適用
Render 淺克隆 不適用
Vercel 淺克隆 不適用

  1. 要為 Cloudflare Pages 站點配置深度克隆,將站點的常規 Hugo 構建命令前置為 git fetch --unshallow &&(例如,git fetch --unshallow && hugo)。 ↩︎

  2. 你可以通過在工作流文件的 “checkout” 步驟中指定 fetch-depth: 0 來配置 GitHub 操作進行深度克隆,如 Hugo 文檔中的 範例工作流文件 所示。 ↩︎

  3. 你可以根據 GitLab 文檔中所述 配置 GitLab Runner 的克隆深度;另見 Hugo 文檔中的 範例工作流文件。 ↩︎