Debian Codegraph @

docs/content/en/methods/page/GitInfo.md

raw
1 ---
2 title: GitInfo
3 description: Provides access to commit metadata for a given page.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: '*gitmap.GitInfo'
9 signatures: [PAGE.GitInfo]
10 ---
11
12 The `GitInfo` method on a `Page` object provides access to commit metadata from your Git history, such as the author's name, the commit hash, and the commit message.
13
14 > [!NOTE]
15 > Hugo's Git integration is performant, but may increase build times for large projects.
16
17 ## Prerequisites
18
19 Install Git, create a repository, and commit your project files.
20
21 You must also allow Hugo to access your repository by adding this to your project configuration:
22
23 {{< code-toggle file=hugo >}}
24 enableGitInfo = true
25 {{< /code-toggle >}}
26
27 > [!NOTE]
28 > When you set [`enableGitInfo`][] to `true`, the last modification date for each content page will automatically be the Author Date of the last commit for that file.
29 >
30 > This is configurable. See [details][].
31
32 ## Scope
33
34 Commit metadata is available for content stored in your local repository and for content provided by [modules](g).
35
36 ### Local content
37
38 Hugo retrieves commit metadata for files tracked within your project's local repository. This includes all content files managed by Git in your main project directory.
39
40 ### Module content
41
42 {{< new-in 0.157.0 />}}
43
44 Hugo also retrieves commit metadata for content provided by modules. This allows you to display commit data for remote repositories that are mounted as content directories, such as when aggregating documentation from multiple sources.
45
46 > [!NOTE]
47 > The `GitInfo` method returns `nil` for module content in these cases:
48 >
49 > - The module is vendored via `hugo mod vendor`
50 > - A [module replacement][] is configured via a `replace` directive in `go.mod` or the [`replacements`][] configuration parameter
51
52 ## Methods
53
54 Use these methods on the `GitInfo` object.
55
56 `AbbreviatedHash`
57 : (`string`) Returns the seven-character shortened version of the commit hash.
58
59 ```go-html-template
60 {{ with .GitInfo }}
61 {{ .AbbreviatedHash }} → aab9ec0
62 {{ end }}
63 ```
64
65 `AuthorDate`
66 : (`time.Time`) Returns the date the author originally created the commit.
67
68 ```go-html-template
69 {{ with .GitInfo }}
70 {{ .AuthorDate.Format "2006-01-02" }} → 2023-10-09
71 {{ end }}
72 ```
73
74 `AuthorEmail`
75 : (`string`) Returns the author's email address, respecting [gitmailmap][].
76
77 ```go-html-template
78 {{ with .GitInfo }}
79 {{ .AuthorEmail }} → [email protected]
80 {{ end }}
81 ```
82
83 `AuthorName`
84 : (`string`) Returns the author's name, respecting [gitmailmap][].
85
86 ```go-html-template
87 {{ with .GitInfo }}
88 {{ .AuthorName }} → John Smith
89 {{ end }}
90 ```
91
92 `CommitDate`
93 : (`time.Time`) Returns the date the commit was applied to the branch.
94
95 ```go-html-template
96 {{ with .GitInfo }}
97 {{ .CommitDate.Format "2006-01-02" }} → 2023-10-09
98 {{ end }}
99 ```
100
101 `Hash`
102 : (`string`) Returns the full SHA-1 commit hash.
103
104 ```go-html-template
105 {{ with .GitInfo }}
106 {{ .Hash }} → aab9ec0b31ebac916a1468c4c9c305f2bebf78d4
107 {{ end }}
108 ```
109
110 `Subject`
111 : (`string`) Returns the first line of the commit message (the summary).
112
113 ```go-html-template
114 {{ with .GitInfo }}
115 {{ .Subject }} → Add tutorials
116 {{ end }}
117 ```
118
119 `Body`
120 : (`string`) Returns the full content of the commit message, excluding the subject line.
121
122 ```go-html-template
123 {{ with .GitInfo }}
124 {{ .Body }} → Two new pages added.
125 {{ end }}
126 ```
127
128 `Ancestors`
129 : (`gitmap.GitInfos`) Returns a list of previous commits for this specific file, ordered from most recent to oldest.
130
131 For example, to list the last 5 commits:
132
133 ```go-html-template
134 {{ with .GitInfo }}
135 {{ range .Ancestors | first 5 }}
136 {{ .CommitDate.Format "2006-01-02" }}: {{ .Subject }}
137 {{ end }}
138 {{ end }}
139 ```
140
141 To reverse the order:
142
143 ```go-html-template
144 {{ with .GitInfo }}
145 {{ range .Ancestors.Reverse | first 5 }}
146 {{ .CommitDate.Format "2006-01-02" }}: {{ .Subject }}
147 {{ end }}
148 {{ end }}
149 ```
150
151 `Parent`
152 : (`*gitmap.GitInfo`) Returns the most recent ancestor commit for the file, if any.
153
154 ## Last modified date
155
156 By default, when `enableGitInfo` is `true`, the `Lastmod` method on a `Page` object returns the Git AuthorDate of the last commit that included the file.
157
158 You can change this behavior in your [project configuration][].
159
160 ## Hosting considerations
161
162 On a [CI/CD](g) platform, the step that clones your project repository must perform a deep clone. If the clone is shallow, the Git information for a given file may be inaccurate. It might incorrectly reflect the most recent repository commit, rather than the commit that actually modified the file.
163
164 While some providers perform a deep clone by default, others require you to configure the depth yourself.
165
166 Hosting service|Default clone depth|Configurable
167 :--|:--|:--
168 AWS Amplify|Deep|N/A
169 Cloudflare|Shallow|Yes [^1]
170 DigitalOcean App Platform|Deep|N/A
171 GitHub Pages|Shallow|Yes [^2]
172 GitLab Pages|Shallow|Yes [^3]
173 Netlify|Deep|N/A
174 Render|Shallow|Yes [^1]
175 Vercel|Shallow|Yes [^1]
176
177 [^1]: To perform a deep clone when hosting on Cloudflare, Render, or Vercel, include this code in the build script after the repository has been cloned:
178
179 ```sh
180 if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
181 git fetch --unshallow
182 fi
183 ```
184
185 [^2]: To perform a deep clone when hosting on GitHub Pages, set `fetch-depth: 0` in the `checkout` step of the GitHub Action.
186
187 [^3]: To perform a deep clone when hosting on GitLab Pages, set the `GIT_DEPTH` environment variable to `0` in the workflow file.
188
189 [`enableGitInfo`]: /configuration/all/#enablegitinfo
190 [`replacements`]: /configuration/module/#replacements
191 [details]: /configuration/front-matter/#dates
192 [gitmailmap]: https://git-scm.com/docs/gitmailmap
193 [module replacement]: /hugo-modules/use-modules/#replace
194 [project configuration]: /configuration/front-matter/