2016-11-01 22:04:12 +00:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *HugoSites) assembleGitInfo() {
|
2017-02-05 03:20:06 +00:00
|
|
|
if !h.Cfg.GetBool("enableGitInfo") {
|
2016-11-01 22:04:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2017-02-05 03:20:06 +00:00
|
|
|
workingDir = h.Cfg.GetString("workingDir")
|
|
|
|
contentDir = h.Cfg.GetString("contentDir")
|
2016-11-01 22:04:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
gitRepo, err := gitmap.Map(workingDir, "")
|
|
|
|
if err != nil {
|
2017-01-10 09:55:03 +00:00
|
|
|
h.Log.ERROR.Printf("Got error reading Git log: %s", err)
|
2016-11-01 22:04:12 +00:00
|
|
|
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 {
|
2016-11-13 13:27:10 +00:00
|
|
|
if p.Path() == "" {
|
|
|
|
// Home page etc. with no content file.
|
|
|
|
continue
|
|
|
|
}
|
2016-11-01 22:04:12 +00:00
|
|
|
// Git normalizes file paths on this form:
|
2017-01-01 22:16:58 +00:00
|
|
|
filename := path.Join(filepath.ToSlash(contentRoot), contentDir, filepath.ToSlash(p.Path()))
|
2016-11-01 22:04:12 +00:00
|
|
|
g, ok := gitMap[filename]
|
|
|
|
if !ok {
|
2017-04-26 09:03:12 +00:00
|
|
|
h.Log.WARN.Printf("Failed to find GitInfo for %q", filename)
|
2016-11-01 22:04:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GitInfo = g
|
|
|
|
p.Lastmod = p.GitInfo.AuthorDate
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|