HUGO
Menu
GitHub 87548 stars Mastodon

GitInfo

返回與給定頁面最後一次提交相關的 Git 信息。

Syntax

PAGE.GitInfo

Returns

*gitmap.GitInfo

Page 對象上的 GitInfo 方法返回一個帶有附加方法的對象。

Hugo 的 Git 集成功能強大,但可能會增加大型站點的構建時間。

先決條件

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

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

enableGitInfo: true
enableGitInfo = true
{
   "enableGitInfo": true
}

或者,在構建站點時使用命令行標志:

hugo --enableGitInfo

當您將 enableGitInfo 設置為 true,或使用命令行標志啟用該功能時,每個內容頁面的最後修改日期將是該文件最後一次提交的作者日期。

這是可配置的。詳見 詳情

方法

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 }}

Ancestors

(gitmap.GitInfos) 文件過濾的祖先提交切片(如果有),從最近到最遠排序。

例如,列出最近 5 次提交:

{{ with .GitInfo }}
  {{ range .Ancestors | first 5 }} 
    {{ .CommitDate.Format "2006-01-02" }}: {{ .Subject }}
  {{ end }}
{{ end }}

反轉順序:

{{ with .GitInfo }}
  {{ range .Ancestors.Reverse | first 5 }} 
    {{ .CommitDate.Format "2006-01-02" }}: {{ .Subject }}
  {{ end }}
{{ end }}

Parent

(*gitmap.GitInfo) 第一個文件過濾的祖先提交(如果有)。

最後修改日期

默認情況下,當 enableGitInfotrue 時,Page 對象上的 Lastmod 方法返回包含該文件的最後一次提交的 Git 作者日期。

您可以在 站點配置 中更改此行為。

托管注意事項

CI/CD 平台上,克隆項目倉庫的步驟必須執行深度克隆。如果克隆是淺的,給定文件的 Git 信息可能不准確。它可能會錯誤地反映最近的倉庫提交,而不是實際修改文件的提交。

雖然某些提供程序默認執行深度克隆,但其他提供程序需要您自己配置深度。

托管服務 默認克隆深度 可配置
AWS Amplify 深度 N/A
Cloudflare 1
DigitalOcean App Platform 深度 N/A
GitHub Pages 2
GitLab Pages 3
Netlify 深度 N/A
Render 1
Vercel 1

  1. 在 Cloudflare、Render 或 Vercel 上托管時執行深度克隆,請在倉庫克隆後在構建腳本中包含此代碼:

    if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
      git fetch --unshallow
    fi
     ↩︎ ↩︎ ↩︎
  2. 在 GitHub Pages 上托管時執行深度克隆,請在 GitHub Action 的 checkout 步驟中設置 fetch-depth: 0。詳見 示例。 ↩︎

  3. 在 GitLab Pages 上托管時執行深度克隆,請在工作流文件中將 GIT_DEPTH 環境變量設置為 0。詳見 示例。 ↩︎


Last updated: January 1, 0001
Improve this page