Add GitInfo

This commit adds a `GitInfo` object to `Page` if `EnableGitInfo` is set.

It then also sets `Lastmod` for the given `Page` to the author date provided by Git.

The Git integrations should be fairly performant, but it adds "some time" to the build, somewhat depending on the Git history size.

If you want, you can run without during development and turn it on when deploying to the live server: `hugo --enableGitInfo`.

Fixes #2102
This commit is contained in:
Bjørn Erik Pedersen 2016-11-01 23:04:12 +01:00 committed by GitHub
parent 186db7cd7a
commit e8380e612f
8 changed files with 89 additions and 1 deletions

View file

@ -133,6 +133,7 @@ var (
canonifyURLs bool canonifyURLs bool
cleanDestination bool cleanDestination bool
enableRobotsTXT bool enableRobotsTXT bool
enableGitInfo bool
disable404 bool disable404 bool
disableRSS bool disableRSS bool
disableSitemap bool disableSitemap bool
@ -237,6 +238,7 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&uglyURLs, "uglyURLs", false, "if true, use /filename.html instead of /filename/") cmd.Flags().BoolVar(&uglyURLs, "uglyURLs", false, "if true, use /filename.html instead of /filename/")
cmd.Flags().BoolVar(&canonifyURLs, "canonifyURLs", false, "if true, all relative URLs will be canonicalized using baseURL") cmd.Flags().BoolVar(&canonifyURLs, "canonifyURLs", false, "if true, all relative URLs will be canonicalized using baseURL")
cmd.Flags().StringVarP(&baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/") cmd.Flags().StringVarP(&baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
cmd.Flags().BoolVar(&enableGitInfo, "enableGitInfo", false, "Add Git revision, date and author info to the pages")
cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program") cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
cmd.Flags().BoolVar(&pluralizeListTitles, "pluralizeListTitles", true, "Pluralize titles in lists using inflect") cmd.Flags().BoolVar(&pluralizeListTitles, "pluralizeListTitles", true, "Pluralize titles in lists using inflect")
@ -319,6 +321,9 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
if flagChanged(cmdV.Flags(), "enableRobotsTXT") { if flagChanged(cmdV.Flags(), "enableRobotsTXT") {
viper.Set("enableRobotsTXT", enableRobotsTXT) viper.Set("enableRobotsTXT", enableRobotsTXT)
} }
if flagChanged(cmdV.Flags(), "enableGitInfo") {
viper.Set("enableGitInfo", enableGitInfo)
}
if flagChanged(cmdV.Flags(), "pluralizeListTitles") { if flagChanged(cmdV.Flags(), "pluralizeListTitles") {
viper.Set("pluralizeListTitles", pluralizeListTitles) viper.Set("pluralizeListTitles", pluralizeListTitles)
} }

View file

@ -2,6 +2,7 @@ title = "Hugo: A Fast and Flexible Website Generator"
baseurl = "http://gohugo.io/" baseurl = "http://gohugo.io/"
MetaDataFormat = "yaml" MetaDataFormat = "yaml"
pluralizeListTitles = false pluralizeListTitles = false
enableGitInfo = true
[blackfriday] [blackfriday]
plainIDAnchors = true plainIDAnchors = true

View file

@ -1,6 +1,6 @@
<div class="row"> <div class="row">
<div class="footer-panel"> <div class="footer-panel">
<p>Last revision: {{ .Lastmod.Format "January 2, 2006" }} <p>Last revision: {{ .Lastmod.Format "January 2, 2006" }}{{ if .IsPage }}<em>{{ with .GitInfo }}| {{ .Subject }} | {{ .AbbreviatedHash }}{{end }}</em>{{ end }}
<span style="float: right;">Hugo v{{ .Hugo.Version }} documentation</span> <span style="float: right;">Hugo v{{ .Hugo.Version }} documentation</span>
</p> </p>
</div> </div>

View file

@ -106,4 +106,5 @@ func loadDefaultSettings() {
viper.SetDefault("defaultContentLanguage", "en") viper.SetDefault("defaultContentLanguage", "en")
viper.SetDefault("defaultContentLanguageInSubdir", false) viper.SetDefault("defaultContentLanguageInSubdir", false)
viper.SetDefault("enableMissingTranslationPlaceholders", false) viper.SetDefault("enableMissingTranslationPlaceholders", false)
viper.SetDefault("enableGitInfo", false)
} }

67
hugolib/gitinfo.go Normal file
View file

@ -0,0 +1,67 @@
// Copyright 2016-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"path"
"path/filepath"
"strings"
"github.com/bep/gitmap"
"github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
func (h *HugoSites) assembleGitInfo() {
if !viper.GetBool("enableGitInfo") {
return
}
var (
workingDir = viper.GetString("workingDir")
contentDir = viper.GetString("contentDir")
)
gitRepo, err := gitmap.Map(workingDir, "")
if err != nil {
jww.ERROR.Printf("Got error reading Git log: %s", err)
return
}
gitMap := gitRepo.Files
repoPath := filepath.FromSlash(gitRepo.TopLevelAbsPath)
// The Hugo site may be placed in a sub folder in the Git repo,
// one example being the Hugo docs.
// We have to find the root folder to the Hugo site below the Git root.
contentRoot := strings.TrimPrefix(workingDir, repoPath)
contentRoot = strings.TrimPrefix(contentRoot, helpers.FilePathSeparator)
s := h.Sites[0]
for _, p := range s.AllPages {
// Git normalizes file paths on this form:
filename := path.Join(contentRoot, contentDir, filepath.ToSlash(p.Path()))
g, ok := gitMap[filename]
if !ok {
jww.ERROR.Printf("Failed to find GitInfo for %q", filename)
return
}
p.GitInfo = g
p.Lastmod = p.GitInfo.AuthorDate
}
}

View file

@ -216,6 +216,9 @@ func (h *HugoSites) Build(config BuildCfg) error {
} }
} }
// TODO(bep) make a more logical grouping of these.
h.assembleGitInfo()
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.postProcess(); err != nil { if err := s.postProcess(); err != nil {
return err return err
@ -288,6 +291,7 @@ func (h *HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
h.setupTranslations(firstSite) h.setupTranslations(firstSite)
if changed.source { if changed.source {
h.assembleGitInfo()
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.postProcess(); err != nil { if err := s.postProcess(); err != nil {
return err return err

View file

@ -19,6 +19,8 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"github.com/bep/gitmap"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
@ -94,6 +96,8 @@ type Page struct {
Source Source
Position `json:"-"` Position `json:"-"`
Node Node
GitInfo *gitmap.GitInfo
} }
type Source struct { type Source struct {

6
vendor/vendor.json vendored
View file

@ -20,6 +20,12 @@
"revision": "5bd2802263f21d8788851d5305584c82a5c75d7e", "revision": "5bd2802263f21d8788851d5305584c82a5c75d7e",
"revisionTime": "2016-07-26T15:08:25Z" "revisionTime": "2016-07-26T15:08:25Z"
}, },
{
"checksumSHA1": "dIidE/pUkqulIvTkuMBTpH3/lnU=",
"path": "github.com/bep/gitmap",
"revision": "a1a71abe12823e27ae7507189fe2e914ba9626ac",
"revisionTime": "2016-10-29T14:53:16Z"
},
{ {
"checksumSHA1": "K8wTIgrK5sl+LmQs8CD/orvKsAM=", "checksumSHA1": "K8wTIgrK5sl+LmQs8CD/orvKsAM=",
"path": "github.com/bep/inflect", "path": "github.com/bep/inflect",