2020-12-17 17:00:47 +03:00
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2020-12-17 17:00:47 +03:00
2021-08-24 19:47:09 +03:00
//go:build gogit
2020-12-17 17:00:47 +03:00
package git
import (
"strings"
"github.com/go-git/go-git/v5/plumbing"
2023-12-14 00:02:00 +03:00
"github.com/go-git/go-git/v5/plumbing/hash"
2020-12-17 17:00:47 +03:00
"github.com/go-git/go-git/v5/plumbing/object"
)
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
func ( repo * Repository ) GetRefCommitID ( name string ) ( string , error ) {
ref , err := repo . gogitRepo . Reference ( plumbing . ReferenceName ( name ) , true )
if err != nil {
if err == plumbing . ErrReferenceNotFound {
return "" , ErrNotExist {
ID : name ,
}
}
return "" , err
}
return ref . Hash ( ) . String ( ) , nil
}
2021-12-23 16:44:00 +03:00
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
func ( repo * Repository ) SetReference ( name , commitID string ) error {
return repo . gogitRepo . Storer . SetReference ( plumbing . NewReferenceFromStrings ( name , commitID ) )
}
// RemoveReference removes the given reference (e.g. branch or tag).
func ( repo * Repository ) RemoveReference ( name string ) error {
return repo . gogitRepo . Storer . RemoveReference ( plumbing . ReferenceName ( name ) )
}
2023-12-14 00:02:00 +03:00
// ConvertToHash returns a Hash object from a potential ID string
func ( repo * Repository ) ConvertToGitID ( commitID string ) ( ObjectID , error ) {
2024-03-12 07:21:27 +03:00
objectFormat , err := repo . GetObjectFormat ( )
if err != nil {
return nil , err
}
2023-12-14 00:02:00 +03:00
if len ( commitID ) == hash . HexSize && objectFormat . IsValid ( commitID ) {
2023-12-19 10:20:47 +03:00
ID , err := NewIDFromString ( commitID )
2021-05-10 04:27:03 +03:00
if err == nil {
2023-12-14 00:02:00 +03:00
return ID , nil
2021-05-10 04:27:03 +03:00
}
}
2022-10-23 17:44:45 +03:00
actualCommitID , _ , err := NewCommand ( repo . Ctx , "rev-parse" , "--verify" ) . AddDynamicArguments ( commitID ) . RunStdString ( & RunOpts { Dir : repo . Path } )
2023-12-14 00:02:00 +03:00
actualCommitID = strings . TrimSpace ( actualCommitID )
2021-05-10 04:27:03 +03:00
if err != nil {
if strings . Contains ( err . Error ( ) , "unknown revision or path" ) ||
strings . Contains ( err . Error ( ) , "fatal: Needed a single revision" ) {
2023-12-17 14:56:08 +03:00
return objectFormat . EmptyObjectID ( ) , ErrNotExist { commitID , "" }
2021-05-10 04:27:03 +03:00
}
2023-12-17 14:56:08 +03:00
return objectFormat . EmptyObjectID ( ) , err
2021-05-10 04:27:03 +03:00
}
2023-12-19 10:20:47 +03:00
return NewIDFromString ( actualCommitID )
2021-05-10 04:27:03 +03:00
}
2020-12-17 17:00:47 +03:00
// IsCommitExist returns true if given commit exists in current repository.
func ( repo * Repository ) IsCommitExist ( name string ) bool {
2023-12-14 00:02:00 +03:00
hash , err := repo . ConvertToGitID ( name )
if err != nil {
return false
}
_ , err = repo . gogitRepo . CommitObject ( plumbing . Hash ( hash . RawValue ( ) ) )
2020-12-17 17:00:47 +03:00
return err == nil
}
2023-12-14 00:02:00 +03:00
func ( repo * Repository ) getCommit ( id ObjectID ) ( * Commit , error ) {
2020-12-17 17:00:47 +03:00
var tagObject * object . Tag
2023-12-14 00:02:00 +03:00
commitID := plumbing . Hash ( id . RawValue ( ) )
gogitCommit , err := repo . gogitRepo . CommitObject ( commitID )
2020-12-17 17:00:47 +03:00
if err == plumbing . ErrObjectNotFound {
2023-12-14 00:02:00 +03:00
tagObject , err = repo . gogitRepo . TagObject ( commitID )
2020-12-17 17:00:47 +03:00
if err == plumbing . ErrObjectNotFound {
return nil , ErrNotExist {
ID : id . String ( ) ,
}
}
if err == nil {
gogitCommit , err = repo . gogitRepo . CommitObject ( tagObject . Target )
}
// if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
}
if err != nil {
return nil , err
}
commit := convertCommit ( gogitCommit )
commit . repo = repo
tree , err := gogitCommit . Tree ( )
if err != nil {
return nil , err
}
2023-12-14 00:02:00 +03:00
commit . Tree . ID = ParseGogitHash ( tree . Hash )
2020-12-17 17:00:47 +03:00
commit . Tree . gogitTree = tree
return commit , nil
}