shithub: hugo

Download patch

ref: 2182ecfd34a24521bf0e3c939627a55327eb1e19
parent: e85833d868a902840c5ed1c90713256153b2548b
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Aug 11 12:37:00 EDT 2018

hugolib: Fix GitInfo when multiple content dirs

Fixes #5054

--- a/hugolib/gitinfo.go
+++ b/hugolib/gitinfo.go
@@ -14,13 +14,11 @@
 package hugolib
 
 import (
-	"path"
 	"path/filepath"
 	"strings"
 
 	"github.com/bep/gitmap"
 	"github.com/gohugoio/hugo/config"
-	"github.com/gohugoio/hugo/helpers"
 )
 
 type gitInfo struct {
@@ -32,15 +30,15 @@
 	if g == nil {
 		return nil, false
 	}
-	name := path.Join(g.contentDir, filepath.ToSlash(p.Path()))
+
+	name := strings.TrimPrefix(filepath.ToSlash(p.Filename()), g.contentDir)
+	name = strings.TrimPrefix(name, "/")
+
 	return g.repo.Files[name], true
 }
 
 func newGitInfo(cfg config.Provider) (*gitInfo, error) {
-	var (
-		workingDir = cfg.GetString("workingDir")
-		contentDir = cfg.GetString("contentDir")
-	)
+	workingDir := cfg.GetString("workingDir")
 
 	gitRepo, err := gitmap.Map(workingDir, "")
 	if err != nil {
@@ -47,13 +45,5 @@
 		return nil, err
 	}
 
-	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)
-	contentDir = path.Join(filepath.ToSlash(contentRoot), contentDir)
-
-	return &gitInfo{contentDir: contentDir, repo: gitRepo}, nil
+	return &gitInfo{contentDir: gitRepo.TopLevelAbsPath, repo: gitRepo}, nil
 }
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -921,21 +921,50 @@
 	cfg.Set("frontmatter", map[string]interface{}{
 		"lastmod": []string{":git", "lastmod"},
 	})
+	cfg.Set("defaultContentLanguage", "en")
 
+	langConfig := map[string]interface{}{
+		"en": map[string]interface{}{
+			"weight":       1,
+			"languageName": "English",
+			"contentDir":   "content",
+		},
+		"nn": map[string]interface{}{
+			"weight":       2,
+			"languageName": "Nynorsk",
+			"contentDir":   "content_nn",
+		},
+	}
+
+	cfg.Set("languages", langConfig)
 	cfg.Set("enableGitInfo", true)
 
 	assrt.NoError(loadDefaultSettingsFor(cfg))
+	assrt.NoError(loadLanguageSettings(cfg, nil))
 
 	wd, err := os.Getwd()
 	assrt.NoError(err)
 	cfg.Set("workingDir", filepath.Join(wd, "testsite"))
 
-	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
+	h, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
 
-	assrt.Len(s.RegularPages, 1)
+	assrt.NoError(err)
+	assrt.Len(h.Sites, 2)
 
+	require.NoError(t, h.Build(BuildCfg{SkipRender: true}))
+
+	enSite := h.Sites[0]
+	assrt.Len(enSite.RegularPages, 1)
+
 	// 2018-03-11 is the Git author date for testsite/content/first-post.md
-	assrt.Equal("2018-03-11", s.RegularPages[0].Lastmod.Format("2006-01-02"))
+	assrt.Equal("2018-03-11", enSite.RegularPages[0].Lastmod.Format("2006-01-02"))
+
+	nnSite := h.Sites[1]
+	assrt.Len(nnSite.RegularPages, 1)
+
+	// 2018-08-11 is the Git author date for testsite/content_nn/first-post.md
+	assrt.Equal("2018-08-11", nnSite.RegularPages[0].Lastmod.Format("2006-01-02"))
+
 }
 
 func TestPageWithFrontMatterConfig(t *testing.T) {