github.com/olliephillips/hugo@v0.42.2/hugolib/gitinfo.go (about) 1 // Copyright 2016-present The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package hugolib 15 16 import ( 17 "path" 18 "path/filepath" 19 "strings" 20 21 "github.com/bep/gitmap" 22 "github.com/gohugoio/hugo/config" 23 "github.com/gohugoio/hugo/helpers" 24 ) 25 26 type gitInfo struct { 27 contentDir string 28 repo *gitmap.GitRepo 29 } 30 31 func (g *gitInfo) forPage(p *Page) (*gitmap.GitInfo, bool) { 32 if g == nil { 33 return nil, false 34 } 35 name := path.Join(g.contentDir, filepath.ToSlash(p.Path())) 36 return g.repo.Files[name], true 37 } 38 39 func newGitInfo(cfg config.Provider) (*gitInfo, error) { 40 var ( 41 workingDir = cfg.GetString("workingDir") 42 contentDir = cfg.GetString("contentDir") 43 ) 44 45 gitRepo, err := gitmap.Map(workingDir, "") 46 if err != nil { 47 return nil, err 48 } 49 50 repoPath := filepath.FromSlash(gitRepo.TopLevelAbsPath) 51 // The Hugo site may be placed in a sub folder in the Git repo, 52 // one example being the Hugo docs. 53 // We have to find the root folder to the Hugo site below the Git root. 54 contentRoot := strings.TrimPrefix(workingDir, repoPath) 55 contentRoot = strings.TrimPrefix(contentRoot, helpers.FilePathSeparator) 56 contentDir = path.Join(filepath.ToSlash(contentRoot), contentDir) 57 58 return &gitInfo{contentDir: contentDir, repo: gitRepo}, nil 59 }