@@ -11,9 +11,12 @@ use tracing::warn;
1111use super :: {
1212 create_code_builder,
1313 intelligence:: { CodeIntelligence , SourceCode } ,
14- CodeRepository ,
14+ repository, CodeRepository ,
15+ } ;
16+ use crate :: {
17+ code:: repository:: resolve_commits,
18+ indexer:: { Indexer , TantivyDocBuilder } ,
1519} ;
16- use crate :: indexer:: { Indexer , TantivyDocBuilder } ;
1720
1821// Magic numbers
1922static MAX_LINE_LENGTH_THRESHOLD : usize = 300 ;
@@ -22,13 +25,30 @@ static MIN_ALPHA_NUM_FRACTION: f32 = 0.25f32;
2225static MAX_NUMBER_OF_LINES : usize = 100000 ;
2326static MAX_NUMBER_FRACTION : f32 = 0.5f32 ;
2427
25- pub async fn index_repository (
26- embedding : Arc < dyn Embedding > ,
27- repository : & CodeRepository ,
28- commit : & str ,
29- ) {
30- let total_files = Walk :: new ( repository. dir ( ) ) . count ( ) ;
31- let file_stream = stream ! {
28+ pub async fn index_repository ( embedding : Arc < dyn Embedding > , repository : & CodeRepository ) {
29+ let refs = resolve_commits ( repository) ;
30+ // resolve_commits would return the current default branch,
31+ // so it should never be empty here.
32+ if refs. is_empty ( ) {
33+ logkit:: error!(
34+ "no branches found for repository {}" ,
35+ repository. canonical_git_url( )
36+ ) ;
37+ return ;
38+ }
39+
40+ let mut count_files = 0 ;
41+ let mut count_chunks = 0 ;
42+
43+ for ( ref_name, sha) in refs {
44+ if let Err ( e) = repository:: checkout ( repository, & ref_name) {
45+ warn ! ( "Failed to checkout ref {}: {}" , ref_name, e) ;
46+ continue ;
47+ }
48+
49+ logkit:: info!( "Indexing branch {} with commit {}" , ref_name, & sha) ;
50+
51+ let file_stream = stream ! {
3252 for file in Walk :: new( repository. dir( ) ) {
3353 let file = match file {
3454 Ok ( file) => file,
@@ -40,21 +60,22 @@ pub async fn index_repository(
4060
4161 yield file;
4262 }
43- }
44- // Commit every 100 files
45- . chunks ( 100 ) ;
63+ }
64+ // Commit every 100 files
65+ . chunks ( 100 ) ;
4666
47- let mut file_stream = pin ! ( file_stream) ;
67+ let mut file_stream = pin ! ( file_stream) ;
4868
49- let mut count_files = 0 ;
50- let mut count_chunks = 0 ;
51- while let Some ( files) = file_stream. next ( ) . await {
52- count_files += files. len ( ) ;
53- count_chunks += add_changed_documents ( repository, commit, embedding. clone ( ) , files) . await ;
54- logkit:: info!( "Processed {count_files}/{total_files} files, updated {count_chunks} chunks" , ) ;
69+ while let Some ( files) = file_stream. next ( ) . await {
70+ count_files += files. len ( ) ;
71+ count_chunks += add_changed_documents ( repository, & sha, embedding. clone ( ) , files) . await ;
72+ logkit:: info!( "Processed {count_files} files, updated {count_chunks} chunks" , ) ;
73+ }
5574 }
5675}
5776
77+ // garbage collection use blob id to check files,
78+ // does NOT have to checkout branch locally.
5879pub async fn garbage_collection ( ) {
5980 let index = Indexer :: new ( corpus:: CODE ) ;
6081 stream ! {
0 commit comments