@@ -177,6 +177,57 @@ pub async fn index_directory(path: &str, db_path: Option<&str>) -> Result<IndexS
177177 Ok ( stats)
178178}
179179
180+ /// Updates an existing index incrementally (using Git).
181+ /// If the index or graph does not exist, it falls back to a full index.
182+ pub async fn update_index ( path : & str , db_path : Option < & str > ) -> Result < ( ) > {
183+ use tracing:: { info, warn} ;
184+
185+ // Determine paths
186+ let default_db_path = std:: path:: Path :: new ( path) . join ( "data/ccm_db" ) ;
187+ let db_path_buf = db_path
188+ . map ( std:: path:: PathBuf :: from)
189+ . unwrap_or ( default_db_path) ;
190+ let db_path_str = db_path_buf. to_string_lossy ( ) . to_string ( ) ;
191+
192+ let parent_dir = db_path_buf. parent ( ) . ok_or_else ( || {
193+ anyhow:: anyhow!(
194+ "Invalid DB path '{}': cannot determine parent directory" ,
195+ db_path_str
196+ )
197+ } ) ?;
198+
199+ let graph_path = parent_dir. join ( "ccm_graph.json" ) ;
200+
201+ if !graph_path. exists ( ) {
202+ info ! (
203+ "Graph not found at {}, performing full index" ,
204+ graph_path. display( )
205+ ) ;
206+ index_directory ( path, db_path) . await ?;
207+ return Ok ( ( ) ) ;
208+ }
209+
210+ // Load graph
211+ let graph = CodeGraph :: from_file ( & graph_path. to_string_lossy ( ) ) ?;
212+ let store = LanceDbStore :: new ( & db_path_str, "code_vectors" ) . await ?;
213+ let graph_arc = std:: sync:: Arc :: new ( tokio:: sync:: RwLock :: new ( graph) ) ;
214+
215+ let engine = RetrievalEngine :: new ( graph_arc. clone ( ) , store) ;
216+
217+ // Run incremental index
218+ info ! ( "Starting incremental indexing for {}" , path) ;
219+ engine. incremental_index ( path) . await ?;
220+
221+ // Save graph back to disk
222+ let updated_graph = graph_arc. read ( ) . await ;
223+ match updated_graph. save_to_file ( & graph_path. to_string_lossy ( ) ) {
224+ Ok ( _) => info ! ( path = %graph_path. display( ) , "Graph updated on disk" ) ,
225+ Err ( e) => warn ! ( error = %e, "Failed to save updated graph" ) ,
226+ }
227+
228+ Ok ( ( ) )
229+ }
230+
180231/// Statistics from an indexing operation
181232#[ derive( Debug , Default ) ]
182233pub struct IndexStats {
0 commit comments