From 853bf0894d4f3606e09283278c42bf9ca981f92a Mon Sep 17 00:00:00 2001 From: Dean Jackson Date: Mon, 17 Dec 2018 20:36:16 +0100 Subject: [PATCH] Retrieve GitInfo from contentDir Get metadata for `GitInfo` from `contentDir` instead of `workingDir`. This decouples the `GitInfo` feature from the working directory, making it dependent on configuration, not the directory `hugo` is run in. Rationale --------- This patch aligns the `GitInfo` feature more closely with the way the rest of Hugo works. The standard `cd /my/site && hugo` build idiom also works when content isn't in the same repo as site, and it allows the `GitInfo` feature to be used regardless of which directory `hugo` is run from (when configured correctly), as is the case for other features. Currently, Hugo searches the working directory for the git repo to populate `GitInfo` content metadata. This means that, in order to retrieve the correct metadata, `hugo` must be run from within the repo containing the content. If `hugo` isn't run from within a repo, `--enableGitInfo` causes a repo-not-found build error, even if the content *is* in a repo. And if `hugo` is run in a different repo, say if content is a submodule of site, it will find no/bad data, as it's looking in the wrong repo. This restriction is unique to `GitInfo` (AFAIK). If you aren't using that feature, you can run Hugo in any directory you want, as long as you pass the appropriate flags. This patch makes it possible to use the standard `cd /my/site && hugo` build process also when the content is in a different repo. More generally, it makes it possible to run `hugo` from any directory when using `--enableGitInfo`, as you can when not using it. Effects ------- `GitInfo` currently only works correctly if `workingDir` and `contentDir` are in the same repo, so it will continue to work everywhere it already works, particularly in the normal case of having site and content in a single repo. This patch will also make `GitInfo` Just Work ^(TM) when content is in a separate repo to the site. Currently, using `GitInfo` requires `cd`-ing to the content repo and running `hugo` from there with a bunch of flags. If the content is unversioned, this patch will cause Hugo to *always* fail to build the site when run with `--enableGitInfo`. Currently, because Hugo looks in the working directory, not the content directory, a build with unversioned content can succeed when Hugo is called with `--enableGitInfo` as long as Hugo is *run* within a git repo. Hugo won't find any (correct) file metadata, but missing file metadata don't cause a build error, whereas a missing git repo does. Fixes #5533 --- hugolib/gitinfo.go | 4 ++-- hugolib/page_test.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/hugolib/gitinfo.go b/hugolib/gitinfo.go index d356fcf075e..bbcdbda181e 100644 --- a/hugolib/gitinfo.go +++ b/hugolib/gitinfo.go @@ -38,9 +38,9 @@ func (g *gitInfo) forPage(p *Page) (*gitmap.GitInfo, bool) { } func newGitInfo(cfg config.Provider) (*gitInfo, error) { - workingDir := cfg.GetString("workingDir") + contentDir := cfg.GetString("contentDir") - gitRepo, err := gitmap.Map(workingDir, "") + gitRepo, err := gitmap.Map(contentDir, "") if err != nil { return nil, err } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 9723b1426cf..cc8272492b8 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -896,6 +896,10 @@ func TestPageWithLastmodFromGitInfo(t *testing.T) { wd, err := os.Getwd() assrt.NoError(err) cfg.Set("workingDir", filepath.Join(wd, "testsite")) + // Content is actually read from the contentDirs specified in langConfig + // above. Set global contentDir to a common parent, as this is + // where Hugo will look for GitInfo. + cfg.Set("contentDir", filepath.Join(wd, "testsite")) h, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})