2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-11-01 18:04:12 -04:00
|
|
|
//
|
|
|
|
// 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/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bep/gitmap"
|
2018-03-11 13:59:11 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
2022-12-30 03:20:58 -05:00
|
|
|
"github.com/gohugoio/hugo/source"
|
2016-11-01 18:04:12 -04:00
|
|
|
)
|
|
|
|
|
2018-03-11 13:59:11 -04:00
|
|
|
type gitInfo struct {
|
|
|
|
contentDir string
|
|
|
|
repo *gitmap.GitRepo
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:20:58 -05:00
|
|
|
func (g *gitInfo) forPage(p page.Page) source.GitInfo {
|
2019-01-02 06:33:26 -05:00
|
|
|
name := strings.TrimPrefix(filepath.ToSlash(p.File().Filename()), g.contentDir)
|
2018-08-11 10:37:00 -04:00
|
|
|
name = strings.TrimPrefix(name, "/")
|
2022-12-30 03:20:58 -05:00
|
|
|
gi, found := g.repo.Files[name]
|
|
|
|
if !found {
|
|
|
|
return source.GitInfo{}
|
|
|
|
}
|
|
|
|
return source.NewGitInfo(*gi)
|
2018-03-11 13:59:11 -04:00
|
|
|
}
|
2016-11-01 18:04:12 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func newGitInfo(conf config.AllProvider) (*gitInfo, error) {
|
|
|
|
workingDir := conf.BaseConfig().WorkingDir
|
2016-11-01 18:04:12 -04:00
|
|
|
|
|
|
|
gitRepo, err := gitmap.Map(workingDir, "")
|
|
|
|
if err != nil {
|
2018-03-11 13:59:11 -04:00
|
|
|
return nil, err
|
2016-11-01 18:04:12 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 10:37:00 -04:00
|
|
|
return &gitInfo{contentDir: gitRepo.TopLevelAbsPath, repo: gitRepo}, nil
|
2016-11-01 18:04:12 -04:00
|
|
|
}
|