@@ -91,6 +91,22 @@ func (r *Repository) GetObjectFormat() git.ObjectFormat {
91
91
return git .ObjectFormatFromName (r .Repository .ObjectFormatName )
92
92
}
93
93
94
+ func (r * Repository ) GetRefCommit (ref string , allowedTypes ... git.RefType ) (git.RefName , * git.Commit , error ) {
95
+ refName := git .RefNameFromUserInput (ref , allowedTypes ... )
96
+ if refName == "" {
97
+ return "" , nil , errors .New ("invalid ref" )
98
+ }
99
+ commitID , err := r .GitRepo .GetRefCommitID (refName .String ())
100
+ if err != nil {
101
+ return "" , nil , err
102
+ }
103
+ commit , err := r .GitRepo .GetCommit (commitID )
104
+ if err != nil {
105
+ return "" , nil , err
106
+ }
107
+ return refName , commit , nil
108
+ }
109
+
94
110
// RepoMustNotBeArchived checks if a repo is archived
95
111
func RepoMustNotBeArchived () func (ctx * Context ) {
96
112
return func (ctx * Context ) {
@@ -780,6 +796,20 @@ type RepoRefByTypeOptions struct {
780
796
IgnoreNotExistErr bool
781
797
}
782
798
799
+ func repoRefFullName (typ git.RefType , shortName string ) git.RefName {
800
+ switch typ {
801
+ case git .RefTypeBranch :
802
+ return git .RefNameFromBranch (shortName )
803
+ case git .RefTypeTag :
804
+ return git .RefNameFromTag (shortName )
805
+ case git .RefTypeCommit :
806
+ return git .RefNameFromCommit (shortName )
807
+ default :
808
+ setting .PanicInDevOrTesting ("Unknown RepoRefType: %v" , typ )
809
+ return git .RefNameFromBranch ("main" ) // just a dummy result, it shouldn't happen
810
+ }
811
+ }
812
+
783
813
// RepoRefByType handles repository reference name for a specific type
784
814
// of repository reference
785
815
func RepoRefByType (detectRefType git.RefType , opts ... RepoRefByTypeOptions ) func (* Context ) {
@@ -842,7 +872,7 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
842
872
} else {
843
873
refShortName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
844
874
}
845
- ctx .Repo .RefFullName = git . RefNameFromTypeAndShortName (refType , refShortName )
875
+ ctx .Repo .RefFullName = repoRefFullName (refType , refShortName )
846
876
isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
847
877
if isRenamedBranch && has {
848
878
renamedBranchName := ctx .Data ["RenamedBranchName" ].(string )
@@ -939,57 +969,6 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
939
969
}
940
970
}
941
971
942
- func RepoRefByQueries () func (ctx * Context ) {
943
- return func (ctx * Context ) {
944
- if ctx .Repo .Repository .IsEmpty {
945
- // assume the user is viewing the (non-existent) default branch
946
- ctx .Repo .IsViewBranch = true
947
- ctx .Repo .BranchName = ctx .Repo .Repository .DefaultBranch
948
- ctx .Repo .RefFullName = git .RefNameFromBranch (ctx .Repo .BranchName )
949
- return
950
- }
951
-
952
- var err error
953
- if ctx .Repo .GitRepo == nil {
954
- ctx .Repo .GitRepo , err = gitrepo .RepositoryFromRequestContextOrOpen (ctx , ctx .Repo .Repository )
955
- if err != nil {
956
- ctx .ServerError (fmt .Sprintf ("Open Repository %v failed" , ctx .Repo .Repository .FullName ()), err )
957
- return
958
- }
959
- }
960
-
961
- ctx .Repo .RefFullName = git .RefNameFromTypeAndShortName (git .RefType (ctx .FormTrim ("ref_type" )), ctx .FormTrim ("ref_name" ))
962
- if ctx .Repo .RefFullName == "" {
963
- ctx .Error (http .StatusBadRequest , "invalid ref type or name" )
964
- return
965
- }
966
-
967
- switch ctx .Repo .RefFullName .RefType () {
968
- case git .RefTypeBranch :
969
- ctx .Repo .IsViewBranch = true
970
- ctx .Repo .BranchName = ctx .Repo .RefFullName .ShortName ()
971
- case git .RefTypeTag :
972
- ctx .Repo .IsViewTag = true
973
- ctx .Repo .TagName = ctx .Repo .RefFullName .ShortName ()
974
- case git .RefTypeCommit :
975
- ctx .Repo .IsViewCommit = true
976
- ctx .Repo .CommitID = ctx .Repo .RefFullName .ShortName ()
977
- }
978
-
979
- ctx .Repo .Commit , err = ctx .Repo .GitRepo .GetCommit (ctx .Repo .RefFullName .String ())
980
- if err != nil {
981
- if git .IsErrNotExist (err ) {
982
- ctx .NotFound ("GetCommit" , err )
983
- } else {
984
- ctx .ServerError ("GetCommit" , err )
985
- }
986
- return
987
- }
988
- ctx .Repo .CommitID = ctx .Repo .Commit .ID .String ()
989
- ctx .Repo .TreePath = ctx .PathParam ("*" )
990
- }
991
- }
992
-
993
972
// GitHookService checks if repository Git hooks service has been enabled.
994
973
func GitHookService () func (ctx * Context ) {
995
974
return func (ctx * Context ) {
0 commit comments