@@ -87,13 +87,12 @@ def index_directory(self, root_dir: str | Path) -> int:
8787 total_chunks += 1
8888
8989 # Store current commit hash for future incremental indexing
90- import subprocess
9190 try :
92- res = subprocess .run (["git" , "rev-parse" , "HEAD" ], cwd = str (root_path ), capture_output = True , text = True , check = True )
93- self .manifest ["last_indexed_commit" ] = res .stdout .strip ()
91+ import git
92+ repo = git .Repo (str (root_path ), search_parent_directories = True )
93+ self .manifest ["last_indexed_commit" ] = repo .head .commit .hexsha
9494 except Exception as e :
95- import logging
96- logging .getLogger (__name__ ).warning ("Ignored exception: %s" , e )
95+ logger .warning ("Failed to get git commit for manifest: %s" , e )
9796
9897 return total_chunks
9998
@@ -106,16 +105,16 @@ def index_incremental(self, root_dir: str | Path) -> int:
106105 Returns:
107106 Number of new chunks added.
108107 """
109- import subprocess
110108 root_path = Path (root_dir ).resolve ()
111109
112110 # Determine last indexed commit
113111 last_commit = self .manifest .get ("last_indexed_commit" )
114112
115113 # Get current commit
116114 try :
117- res = subprocess .run (["git" , "rev-parse" , "HEAD" ], cwd = str (root_path ), capture_output = True , text = True , check = True )
118- current_commit = res .stdout .strip ()
115+ import git
116+ repo = git .Repo (str (root_path ), search_parent_directories = True )
117+ current_commit = repo .head .commit .hexsha
119118 except Exception as e :
120119 logger .warning (f"Failed to get current git commit: { e } . Incremental indexer falling back to full index." )
121120 return self .index_directory (root_dir )
@@ -131,20 +130,20 @@ def index_incremental(self, root_dir: str | Path) -> int:
131130
132131 # Get changed files
133132 try :
134- diff_res = subprocess . run (
135- [ "git" , " diff" , "--name-only" , last_commit , current_commit ],
136- cwd = str ( root_path ), capture_output = True , text = True , check = True
137- )
138- untracked_res = subprocess . run (
139- [ "git" , "ls-files" , "--others" , "--exclude-standard" ],
140- cwd = str ( root_path ), capture_output = True , text = True , check = True
141- )
133+ changed_files_rel = []
134+ diff = repo . commit ( last_commit ). diff ( current_commit )
135+ for d in diff :
136+ if d . b_path :
137+ changed_files_rel . append ( d . b_path )
138+
139+ untracked = repo . untracked_files
140+ changed_files_rel . extend ( untracked )
142141 except Exception as e :
143142 logger .warning (f"Failed to get git diff: { e } . Falling back to full index." )
144143 self .manifest ["last_indexed_commit" ] = current_commit
145144 return self .index_directory (root_dir )
146145
147- changed_files_rel = diff_res . stdout . splitlines () + untracked_res . stdout . splitlines ( )
146+ changed_files_rel = list ( set ( changed_files_rel ) )
148147 changed_files = [str (root_path / f ) for f in set (changed_files_rel ) if f .strip ()]
149148
150149 if not changed_files :
0 commit comments