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