@@ -11,8 +11,8 @@ use anyhow::{Context, Result};
11
11
use bytes:: { Bytes , BytesMut } ;
12
12
use comrak:: { ComrakOptions , ComrakPlugins } ;
13
13
use git2:: {
14
- BranchType , DiffFormat , DiffLineType , DiffOptions , DiffStatsFormat , Email , EmailCreateOptions ,
15
- ObjectType , Oid , Signature ,
14
+ DiffFormat , DiffLineType , DiffOptions , DiffStatsFormat , Email , EmailCreateOptions , ObjectType ,
15
+ Oid , Signature ,
16
16
} ;
17
17
use moka:: future:: Cache ;
18
18
use parking_lot:: Mutex ;
@@ -51,7 +51,11 @@ impl Git {
51
51
52
52
impl Git {
53
53
#[ instrument( skip( self ) ) ]
54
- pub async fn repo ( self : Arc < Self > , repo_path : PathBuf ) -> Result < Arc < OpenRepository > > {
54
+ pub async fn repo (
55
+ self : Arc < Self > ,
56
+ repo_path : PathBuf ,
57
+ branch : Option < Arc < str > > ,
58
+ ) -> Result < Arc < OpenRepository > > {
55
59
let repo = tokio:: task:: spawn_blocking ( {
56
60
let repo_path = repo_path. clone ( ) ;
57
61
move || git2:: Repository :: open ( repo_path)
@@ -64,6 +68,7 @@ impl Git {
64
68
git : self ,
65
69
cache_key : repo_path,
66
70
repo : Mutex :: new ( repo) ,
71
+ branch,
67
72
} ) )
68
73
}
69
74
}
@@ -72,14 +77,14 @@ pub struct OpenRepository {
72
77
git : Arc < Git > ,
73
78
cache_key : PathBuf ,
74
79
repo : Mutex < git2:: Repository > ,
80
+ branch : Option < Arc < str > > ,
75
81
}
76
82
77
83
impl OpenRepository {
78
84
pub async fn path (
79
85
self : Arc < Self > ,
80
86
path : Option < PathBuf > ,
81
87
tree_id : Option < & str > ,
82
- branch : Option < String > ,
83
88
formatted : bool ,
84
89
) -> Result < PathDestination > {
85
90
let tree_id = tree_id
@@ -93,12 +98,11 @@ impl OpenRepository {
93
98
let mut tree = if let Some ( tree_id) = tree_id {
94
99
repo. find_tree ( tree_id)
95
100
. context ( "Couldn't find tree with given id" ) ?
96
- } else if let Some ( branch) = branch {
97
- let branch = repo. find_branch ( & branch, BranchType :: Local ) ?;
98
- branch
99
- . get ( )
101
+ } else if let Some ( branch) = & self . branch {
102
+ let reference = repo. resolve_reference_from_short_name ( branch) ?;
103
+ reference
100
104
. peel_to_tree ( )
101
- . context ( "Couldn't find tree for branch " ) ?
105
+ . context ( "Couldn't find tree for reference " ) ?
102
106
} else {
103
107
let head = repo. head ( ) ?;
104
108
head. peel_to_tree ( )
@@ -175,16 +179,14 @@ impl OpenRepository {
175
179
}
176
180
177
181
#[ instrument( skip( self ) ) ]
178
- pub async fn tag_info ( self : Arc < Self > , tag_name : & str ) -> Result < DetailedTag > {
179
- let reference = format ! ( "refs/tags/{tag_name}" ) ;
180
- let tag_name = tag_name. to_string ( ) ;
181
-
182
+ pub async fn tag_info ( self : Arc < Self > ) -> Result < DetailedTag > {
182
183
tokio:: task:: spawn_blocking ( move || {
184
+ let tag_name = self . branch . clone ( ) . context ( "no tag given" ) ?;
183
185
let repo = self . repo . lock ( ) ;
184
186
185
187
let tag = repo
186
- . find_reference ( & reference )
187
- . context ( "Given reference does not exist in repository" ) ?
188
+ . find_reference ( & format ! ( "refs/tags/{tag_name}" ) )
189
+ . context ( "Given tag does not exist in repository" ) ?
188
190
. peel_to_tag ( )
189
191
. context ( "Couldn't get to a tag from the given reference" ) ?;
190
192
let tag_target = tag. target ( ) . context ( "Couldn't find tagged object" ) ?;
@@ -222,7 +224,12 @@ impl OpenRepository {
222
224
tokio:: task:: spawn_blocking ( move || {
223
225
let repo = self . repo . lock ( ) ;
224
226
225
- let head = repo. head ( ) . context ( "Couldn't find HEAD of repository" ) ?;
227
+ let head = if let Some ( reference) = & self . branch {
228
+ repo. resolve_reference_from_short_name ( reference) ?
229
+ } else {
230
+ repo. head ( ) . context ( "Couldn't find HEAD of repository" ) ?
231
+ } ;
232
+
226
233
let commit = head. peel_to_commit ( ) . context (
227
234
"Couldn't find the commit that the HEAD of the repository refers to" ,
228
235
) ?;
@@ -268,7 +275,12 @@ impl OpenRepository {
268
275
tokio:: task:: spawn_blocking ( move || {
269
276
let repo = self . repo . lock ( ) ;
270
277
271
- let head = repo. head ( ) . context ( "Couldn't find HEAD of repository" ) ?;
278
+ let head = if let Some ( reference) = & self . branch {
279
+ repo. resolve_reference_from_short_name ( reference) ?
280
+ } else {
281
+ repo. head ( ) . context ( "Couldn't find HEAD of repository" ) ?
282
+ } ;
283
+
272
284
let commit = head
273
285
. peel_to_commit ( )
274
286
. context ( "Couldn't find commit HEAD of repository refers to" ) ?;
@@ -381,7 +393,7 @@ pub enum TaggedObject {
381
393
382
394
#[ derive( Debug ) ]
383
395
pub struct DetailedTag {
384
- pub name : String ,
396
+ pub name : Arc < str > ,
385
397
pub tagger : Option < CommitUser > ,
386
398
pub message : String ,
387
399
pub tagged_object : Option < TaggedObject > ,
0 commit comments