@@ -17,6 +17,10 @@ struct Cli {
1717 /// Commit SHA (when amending)
1818 #[ arg( default_value = "" ) ]
1919 commit_sha : String ,
20+
21+ /// Enable debug output
22+ #[ arg( long) ]
23+ debug : bool ,
2024}
2125
2226struct Agent {
@@ -98,30 +102,64 @@ fn find_agent_by_env() -> Option<&'static Agent> {
98102 } )
99103}
100104
101- fn find_agent_for_process ( process : & sysinfo:: Process ) -> Option < & ' static Agent > {
105+ fn find_agent_for_process ( process : & sysinfo:: Process , debug : bool ) -> Option < & ' static Agent > {
102106 let name = process. name ( ) . to_string_lossy ( ) ;
107+ if debug {
108+ eprintln ! ( " Checking process name: {}" , name) ;
109+ }
103110 if let Some ( agent) = find_agent_by_name ( & name) {
111+ if debug {
112+ eprintln ! ( " ✓ Matched agent: {}" , agent. email) ;
113+ }
104114 return Some ( agent) ;
105115 }
106116
117+ // Check basename(argv[0])
118+ if let Some ( arg0) = process. cmd ( ) . first ( ) {
119+ let arg0_str = arg0. to_string_lossy ( ) ;
120+ if debug {
121+ eprintln ! ( " Checking basename(argv[0]): {}" , arg0_str) ;
122+ }
123+ if let Some ( agent) = find_agent_by_name ( & arg0_str) {
124+ if debug {
125+ eprintln ! ( " ✓ Matched agent: {}" , agent. email) ;
126+ }
127+ return Some ( agent) ;
128+ }
129+ }
130+
131+ // Check first basename(argv[1:]) that doesn't start with '-'
107132 if let Some ( arg) = process. cmd ( ) . iter ( ) . skip ( 1 ) . find ( |arg| {
108133 let arg_str = arg. to_string_lossy ( ) ;
109134 !arg_str. starts_with ( '-' )
110135 } ) {
111136 let arg_str = arg. to_string_lossy ( ) ;
137+ if debug {
138+ eprintln ! ( " Checking first non-flag arg from argv[1:]: {}" , arg_str) ;
139+ }
112140 if let Some ( agent) = find_agent_by_name ( & arg_str) {
141+ if debug {
142+ eprintln ! ( " ✓ Matched agent: {}" , agent. email) ;
143+ }
113144 return Some ( agent) ;
114145 }
115146 }
116147
117148 None
118149}
119150
120- fn walk_ancestry ( system : & System ) -> Option < & ' static Agent > {
151+ fn walk_ancestry ( system : & System , debug : bool ) -> Option < & ' static Agent > {
121152 let mut current_pid = Pid :: from_u32 ( std:: process:: id ( ) ) ;
122153
154+ if debug {
155+ eprintln ! ( "\n Walking ancestry from PID {}..." , current_pid) ;
156+ }
157+
123158 while let Some ( process) = system. process ( current_pid) {
124- if let Some ( agent) = find_agent_for_process ( process) {
159+ if debug {
160+ eprintln ! ( " PID {}: {:?}" , current_pid, process. name( ) ) ;
161+ }
162+ if let Some ( agent) = find_agent_for_process ( process, debug) {
125163 return Some ( agent) ;
126164 }
127165
@@ -136,7 +174,7 @@ fn walk_ancestry(system: &System) -> Option<&'static Agent> {
136174 None
137175}
138176
139- fn check_process_tree ( system : & System , root_pid : Pid , repo_path : & PathBuf ) -> Option < & ' static Agent > {
177+ fn check_process_tree ( system : & System , root_pid : Pid , repo_path : & PathBuf , debug : bool ) -> Option < & ' static Agent > {
140178 let mut queue = std:: collections:: VecDeque :: new ( ) ;
141179 let mut visited = std:: collections:: HashSet :: new ( ) ;
142180
@@ -152,10 +190,17 @@ fn check_process_tree(system: &System, root_pid: Pid, repo_path: &PathBuf) -> Op
152190 None => continue ,
153191 } ;
154192
155- if let Some ( agent) = find_agent_for_process ( process)
193+ if debug {
194+ eprintln ! ( " Checking PID {}: {:?}" , pid, process. name( ) ) ;
195+ }
196+
197+ if let Some ( agent) = find_agent_for_process ( process, debug)
156198 && let Some ( cwd) = process. cwd ( )
157199 && cwd. starts_with ( repo_path)
158200 {
201+ if debug {
202+ eprintln ! ( " Found agent in tree with matching cwd" ) ;
203+ }
159204 return Some ( agent) ;
160205 }
161206
@@ -169,10 +214,14 @@ fn check_process_tree(system: &System, root_pid: Pid, repo_path: &PathBuf) -> Op
169214 None
170215}
171216
172- fn walk_ancestry_and_descendants ( system : & System , repo_path : & PathBuf ) -> Option < & ' static Agent > {
217+ fn walk_ancestry_and_descendants ( system : & System , repo_path : & PathBuf , debug : bool ) -> Option < & ' static Agent > {
173218 let mut current_pid = Pid :: from_u32 ( std:: process:: id ( ) ) ;
174219 let mut checked_ancestors = std:: collections:: HashSet :: new ( ) ;
175220
221+ if debug {
222+ eprintln ! ( "\n Walking ancestry and descendants..." ) ;
223+ }
224+
176225 loop {
177226 let process = system. process ( current_pid) ?;
178227
@@ -185,12 +234,16 @@ fn walk_ancestry_and_descendants(system: &System, repo_path: &PathBuf) -> Option
185234 _ => break ,
186235 } ;
187236
237+ if debug {
238+ eprintln ! ( " Checking siblings of PID {} (parent: {})" , current_pid, parent_pid) ;
239+ }
240+
188241 for sibling in system. processes ( ) . values ( ) {
189242 if sibling. parent ( ) != Some ( parent_pid) {
190243 continue ;
191244 }
192245
193- if let Some ( agent) = check_process_tree ( system, sibling. pid ( ) , repo_path) {
246+ if let Some ( agent) = check_process_tree ( system, sibling. pid ( ) , repo_path, debug ) {
194247 return Some ( agent) ;
195248 }
196249 }
@@ -244,27 +297,37 @@ fn find_git_root(start_path: &Path) -> Option<PathBuf> {
244297 }
245298}
246299
247- fn detect_agent ( ) -> Option < & ' static Agent > {
300+ fn detect_agent ( debug : bool ) -> Option < & ' static Agent > {
301+ if debug {
302+ eprintln ! ( "=== Agent Detection Debug ===" ) ;
303+ eprintln ! ( "\n Checking environment variables..." ) ;
304+ }
248305 if let Some ( agent) = find_agent_by_env ( ) {
306+ if debug {
307+ eprintln ! ( " ✓ Found agent via env: {}" , agent. email) ;
308+ }
249309 return Some ( agent) ;
250310 }
251311
252312 let current_dir = std:: env:: current_dir ( ) . ok ( ) ?;
253313 let repo_path = find_git_root ( & current_dir) . unwrap_or ( current_dir) ;
314+ if debug {
315+ eprintln ! ( " Repository path: {}" , repo_path. display( ) ) ;
316+ }
254317 let system = System :: new_all ( ) ;
255318
256- if let Some ( agent) = walk_ancestry ( & system) {
319+ if let Some ( agent) = walk_ancestry ( & system, debug ) {
257320 return Some ( agent) ;
258321 }
259322
260- walk_ancestry_and_descendants ( & system, & repo_path)
323+ walk_ancestry_and_descendants ( & system, & repo_path, debug )
261324}
262325
263326fn main ( ) {
264327 let cli = Cli :: parse ( ) ;
265328
266329 let Some ( commit_msg_file) = cli. commit_msg_file else {
267- match detect_agent ( ) {
330+ match detect_agent ( cli . debug ) {
268331 Some ( agent) => println ! ( "{}" , agent. email) ,
269332 None => {
270333 eprintln ! ( "No agent found" ) ;
@@ -274,7 +337,7 @@ fn main() {
274337 return ;
275338 } ;
276339
277- if let Some ( agent) = detect_agent ( )
340+ if let Some ( agent) = detect_agent ( cli . debug )
278341 && let Err ( e) = append_trailers ( & commit_msg_file, agent)
279342 {
280343 eprintln ! ( "aittributor: failed to append trailers: {}" , e) ;
0 commit comments