github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_commitgraph_gogit.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Copyright 2019 The GitBundle Authors.
     7  // All rights reserved.
     8  // Use of this source code is governed by a MIT-style
     9  // license that can be found in the LICENSE file.
    10  
    11  //go:build gogit
    12  
    13  package git
    14  
    15  import (
    16  	"os"
    17  	"path"
    18  
    19  	gitealog "github.com/gitbundle/modules/log"
    20  
    21  	"github.com/go-git/go-git/v5/plumbing/format/commitgraph"
    22  	cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
    23  )
    24  
    25  // CommitNodeIndex returns the index for walking commit graph
    26  func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) {
    27  	indexPath := path.Join(r.Path, "objects", "info", "commit-graph")
    28  
    29  	file, err := os.Open(indexPath)
    30  	if err == nil {
    31  		var index commitgraph.Index
    32  		index, err = commitgraph.OpenFileIndex(file)
    33  		if err == nil {
    34  			return cgobject.NewGraphCommitNodeIndex(index, r.gogitRepo.Storer), file
    35  		}
    36  	}
    37  
    38  	if !os.IsNotExist(err) {
    39  		gitealog.Warn("Unable to read commit-graph for %s: %v", r.Path, err)
    40  	}
    41  
    42  	return cgobject.NewObjectCommitNodeIndex(r.gogitRepo.Storer), nil
    43  }