octocode 0.12.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use clap::Args;
use std::io::{self, Write};
use std::process::Command;
use tokio::task::JoinSet;

use octocode::config::Config;
use octocode::indexer::git_utils::GitUtils;
use octocode::utils::diff_chunker;

/// Retry configuration for failed chunk processing
const MAX_RETRIES: usize = 2;
const RETRY_DELAY_MS: u64 = 1000;

/// Retry wrapper for LLM calls with exponential backoff
async fn call_llm_with_retry<F, Fut>(operation: F, context: &str) -> Result<String>
where
	F: Fn() -> Fut,
	Fut: std::future::Future<Output = Result<String>>,
{
	let mut last_error = None;

	for attempt in 1..=MAX_RETRIES + 1 {
		match operation().await {
			Ok(response) => return Ok(response),
			Err(e) => {
				last_error = Some(e);
				if attempt <= MAX_RETRIES {
					eprintln!(
						"Warning: {} attempt {} failed, retrying in {}ms...",
						context, attempt, RETRY_DELAY_MS
					);
					tokio::time::sleep(tokio::time::Duration::from_millis(RETRY_DELAY_MS)).await;
				}
			}
		}
	}

	// All retries failed
	if let Some(e) = last_error {
		Err(anyhow::anyhow!(
			"{} failed after {} attempts: {}",
			context,
			MAX_RETRIES + 1,
			e
		))
	} else {
		Err(anyhow::anyhow!("{} failed with unknown error", context))
	}
}

#[derive(Args, Debug)]
pub struct CommitArgs {
	/// Add all changes before committing
	#[arg(short, long)]
	pub all: bool,

	/// Additional context to help AI generate better commit message (guidance, not the base message)
	#[arg(short, long)]
	pub message: Option<String>,

	/// Skip confirmation prompt
	#[arg(short, long)]
	pub yes: bool,

	/// Skip pre-commit hooks and commit-msg hooks
	/// Note: Pre-commit hooks run automatically if pre-commit binary and config are detected
	#[arg(short, long)]
	pub no_verify: bool,

	/// Rewrite the message of an existing commit by its hash.
	/// Uses that commit's diff to generate a new message, then replaces it in-place.
	/// HEAD commits are amended directly; older commits are rewritten via git rebase.
	#[arg(short, long, value_name = "HASH")]
	pub commit: Option<String>,
}

/// Execute the commit command with intelligent pre-commit hook integration.
///
/// Pre-commit hooks are automatically detected and run if:
/// - The `pre-commit` binary is available in PATH
/// - A `.pre-commit-config.yaml` or `.pre-commit-config.yml` file exists
/// - The `--no-verify` flag is not used
///
/// When `--all` is specified, pre-commit runs with `--all-files`.
/// Otherwise, it runs only on staged files (default behavior).
///
/// If pre-commit modifies files, they are automatically re-staged before
/// generating the commit message with AI.
pub async fn execute(config: &Config, args: &CommitArgs) -> Result<()> {
	let current_dir = std::env::current_dir()?;

	// Find git repository root
	let git_root = GitUtils::find_git_root(&current_dir)
		.ok_or_else(|| anyhow::anyhow!("❌ Not in a git repository!"))?;

	// Use git root as working directory for all operations
	let current_dir = git_root;

	// -c/--commit mode: rewrite an existing commit's message
	if let Some(ref hash) = args.commit {
		return rewrite_commit_message(
			&current_dir,
			config,
			hash,
			args.message.as_deref(),
			args.yes,
		)
		.await;
	}

	// Add all files if requested
	if args.all {
		println!("📂 Adding all changes...");
		let output = Command::new("git")
			.args(["add", "."])
			.current_dir(&current_dir)
			.output()?;

		if !output.status.success() {
			return Err(anyhow::anyhow!(
				"Failed to add files: {}",
				String::from_utf8_lossy(&output.stderr)
			));
		}
	}

	// Check if there are staged changes
	let output = Command::new("git")
		.args(["diff", "--cached", "--name-only"])
		.current_dir(&current_dir)
		.output()?;

	if !output.status.success() {
		return Err(anyhow::anyhow!(
			"Failed to check staged changes: {}",
			String::from_utf8_lossy(&output.stderr)
		));
	}

	let staged_files = String::from_utf8(output.stdout)?;
	if staged_files.trim().is_empty() {
		return Err(anyhow::anyhow!(
			"❌ No staged changes to commit. Use 'git add' or --all flag."
		));
	}

	println!("📋 Staged files:");
	for file in staged_files.lines() {
		println!("  â€ĸ {}", file);
	}

	// Run pre-commit hooks if available and not skipped
	if !args.no_verify {
		let originally_staged_files: Vec<String> =
			staged_files.lines().map(|s| s.to_string()).collect();
		run_precommit_hooks(&current_dir, args.all, &originally_staged_files).await?;
	}

	// Check staged changes again after pre-commit (files might have been modified)
	let output = Command::new("git")
		.args(["diff", "--cached", "--name-only"])
		.current_dir(&current_dir)
		.output()?;

	if !output.status.success() {
		return Err(anyhow::anyhow!(
			"Failed to check staged changes after pre-commit: {}",
			String::from_utf8_lossy(&output.stderr)
		));
	}

	let final_staged_files = String::from_utf8(output.stdout)?;
	if final_staged_files.trim().is_empty() {
		return Err(anyhow::anyhow!(
			"❌ No staged changes remaining after pre-commit hooks."
		));
	}

	// Show updated staged files if they changed
	if final_staged_files != staged_files {
		println!("\n📋 Updated staged files after pre-commit:");
		for file in final_staged_files.lines() {
			println!("  â€ĸ {}", file);
		}
	}

	// Generate commit message using AI with intelligent chunking for large diffs
	println!("\n🤖 Generating commit message...");
	let commit_message =
		generate_commit_message_chunked(&current_dir, config, args.message.as_deref()).await?;

	println!("\n📝 Generated commit message:");
	println!("═══════════════════════════════════");
	println!("{}", commit_message);
	println!("═══════════════════════════════════");

	// Confirm with user (unless --yes flag is used)
	if !args.yes {
		print!("\nProceed with this commit? [y/N] ");
		io::stdout().flush()?;

		let mut input = String::new();
		io::stdin().read_line(&mut input)?;

		if !input.trim().to_lowercase().starts_with('y') {
			println!("❌ Commit cancelled.");
			return Ok(());
		}
	}

	// Perform the commit
	println!("💾 Committing changes...");
	let mut git_args = vec!["commit", "-m", &commit_message];
	if args.no_verify {
		git_args.push("--no-verify");
	}
	let output = Command::new("git")
		.args(&git_args)
		.current_dir(&current_dir)
		.output()?;

	if !output.status.success() {
		return Err(anyhow::anyhow!(
			"Failed to commit: {}",
			String::from_utf8_lossy(&output.stderr)
		));
	}

	println!("✅ Successfully committed changes!");

	// Show commit info
	let output = Command::new("git")
		.args(["log", "--oneline", "-1"])
		.current_dir(&current_dir)
		.output()?;

	if output.status.success() {
		let commit_info = String::from_utf8_lossy(&output.stdout);
		println!("📄 Commit: {}", commit_info.trim());
	}

	Ok(())
}

/// Rewrite the commit message of an existing commit identified by `hash`.
///
/// - If the hash resolves to HEAD, uses `git commit --amend` (fast, no rebase needed).
/// - Otherwise, uses a non-interactive `git rebase -i` with a custom sequence editor
///   that marks only the target commit as `reword`, leaving all others as `pick`.
///   The new message is injected via `GIT_EDITOR` pointing to a temp script.
async fn rewrite_commit_message(
	repo_path: &std::path::Path,
	config: &Config,
	hash: &str,
	extra_context: Option<&str>,
	skip_confirm: bool,
) -> Result<()> {
	// Resolve the hash to a full SHA so we can compare with HEAD
	let resolved = Command::new("git")
		.args(["rev-parse", hash])
		.current_dir(repo_path)
		.output()?;
	if !resolved.status.success() {
		return Err(anyhow::anyhow!(
			"❌ Cannot resolve commit '{}': {}",
			hash,
			String::from_utf8_lossy(&resolved.stderr).trim()
		));
	}
	let full_hash = String::from_utf8(resolved.stdout)?.trim().to_string();

	// Resolve HEAD for comparison
	let head = Command::new("git")
		.args(["rev-parse", "HEAD"])
		.current_dir(repo_path)
		.output()?;
	let head_hash = if head.status.success() {
		String::from_utf8(head.stdout)?.trim().to_string()
	} else {
		String::new()
	};

	let is_head = full_hash == head_hash;

	// Get the diff of the target commit to generate a message from
	println!("🔍 Analysing commit {}...", &full_hash[..8]);
	let diff_output = Command::new("git")
		.args(["show", &full_hash, "--format=", "-p"])
		.current_dir(repo_path)
		.output()?;
	if !diff_output.status.success() {
		return Err(anyhow::anyhow!(
			"❌ Failed to get diff for commit '{}': {}",
			hash,
			String::from_utf8_lossy(&diff_output.stderr).trim()
		));
	}
	let diff = String::from_utf8(diff_output.stdout)?;
	if diff.trim().is_empty() {
		return Err(anyhow::anyhow!(
			"❌ Commit '{}' has no diff (merge commit or empty commit).",
			hash
		));
	}

	// Show files touched by this commit
	let files_output = Command::new("git")
		.args(["show", &full_hash, "--name-only", "--format="])
		.current_dir(repo_path)
		.output()?;
	if files_output.status.success() {
		let files = String::from_utf8_lossy(&files_output.stdout);
		println!("📋 Files in commit:");
		for f in files.lines().filter(|l| !l.is_empty()) {
			println!("  â€ĸ {}", f);
		}
	}

	// Generate new commit message from the commit's own diff
	println!("\n🤖 Generating commit message...");
	let commit_message = generate_commit_message_from_diff(&diff, config, extra_context).await?;

	println!("\n📝 Generated commit message:");
	println!("═══════════════════════════════════");
	println!("{}", commit_message);
	println!("═══════════════════════════════════");

	if !skip_confirm {
		print!("\nReplace message of commit {}? [y/N] ", &full_hash[..8]);
		io::stdout().flush()?;
		let mut input = String::new();
		io::stdin().read_line(&mut input)?;
		if !input.trim().to_lowercase().starts_with('y') {
			println!("❌ Cancelled.");
			return Ok(());
		}
	}

	if is_head {
		// Fast path: just amend HEAD
		println!("âœī¸  Amending HEAD commit message...");
		let output = Command::new("git")
			.args(["commit", "--amend", "-m", &commit_message, "--no-edit"])
			.current_dir(repo_path)
			.output()?;
		if !output.status.success() {
			return Err(anyhow::anyhow!(
				"Failed to amend commit: {}",
				String::from_utf8_lossy(&output.stderr).trim()
			));
		}
	} else {
		// Non-HEAD: rewrite via rebase using a temp editor script that injects the message
		println!("âœī¸  Rewriting commit {} via rebase...", &full_hash[..8]);

		// Write the new message to a temp file so the editor script can read it
		let msg_file = repo_path.join(".git").join("OCTOCODE_REWORD_MSG");
		std::fs::write(&msg_file, &commit_message)?;

		// Editor script: replaces the COMMIT_EDITMSG content with our message
		// git rebase -i calls $GIT_EDITOR with the path to the commit message file
		let editor_script = format!("#!/bin/sh\ncp '{}' \"$1\"", msg_file.display());
		let editor_file = repo_path.join(".git").join("octocode_reword_editor.sh");
		std::fs::write(&editor_file, &editor_script)?;
		#[cfg(unix)]
		{
			use std::os::unix::fs::PermissionsExt;
			std::fs::set_permissions(&editor_file, std::fs::Permissions::from_mode(0o755))?;
		}

		// Sequence editor: marks only our target commit as 'reword', rest stay 'pick'
		let seq_script = format!(
			"#!/bin/sh\nsed -i.bak 's/^pick {}/reword {}/' \"$1\"",
			&full_hash[..7],
			&full_hash[..7]
		);
		let seq_file = repo_path.join(".git").join("octocode_seq_editor.sh");
		std::fs::write(&seq_file, &seq_script)?;
		#[cfg(unix)]
		{
			use std::os::unix::fs::PermissionsExt;
			std::fs::set_permissions(&seq_file, std::fs::Permissions::from_mode(0o755))?;
		}

		let output = Command::new("git")
			.args(["rebase", "-i", &format!("{}^", full_hash)])
			.env("GIT_SEQUENCE_EDITOR", &seq_file)
			.env("GIT_EDITOR", &editor_file)
			.current_dir(repo_path)
			.output()?;

		// Clean up temp files regardless of outcome
		let _ = std::fs::remove_file(&msg_file);
		let _ = std::fs::remove_file(&editor_file);
		let _ = std::fs::remove_file(&seq_file);
		let _ = std::fs::remove_file(repo_path.join(".git").join("octocode_reword_editor.sh.bak"));
		let _ = std::fs::remove_file(repo_path.join(".git").join("octocode_seq_editor.sh.bak"));

		if !output.status.success() {
			return Err(anyhow::anyhow!(
				"Failed to rebase: {}",
				String::from_utf8_lossy(&output.stderr).trim()
			));
		}
	}

	println!("✅ Commit message replaced successfully!");

	// Show updated commit info (after rebase the old hash is gone, show latest)
	let log = Command::new("git")
		.args(["log", "--oneline", "-1"])
		.current_dir(repo_path)
		.output()?;
	if log.status.success() {
		println!("📄 Commit: {}", String::from_utf8_lossy(&log.stdout).trim());
	}

	Ok(())
}

async fn generate_commit_message_chunked(
	repo_path: &std::path::Path,
	config: &Config,
	extra_context: Option<&str>,
) -> Result<String> {
	// Get the diff of staged changes
	let output = Command::new("git")
		.args(["diff", "--cached"])
		.current_dir(repo_path)
		.output()?;

	if !output.status.success() {
		return Err(anyhow::anyhow!(
			"Failed to get diff: {}",
			String::from_utf8_lossy(&output.stderr)
		));
	}

	let diff = String::from_utf8(output.stdout)?;

	if diff.trim().is_empty() {
		return Err(anyhow::anyhow!("No staged changes found"));
	}

	// Get list of staged files to analyze extensions
	let staged_files = GitUtils::get_staged_files(repo_path)?;
	let changed_files = staged_files.join("\n");

	// Analyze file extensions
	let has_markdown_files = changed_files
		.lines()
		.any(|file| file.ends_with(".md") || file.ends_with(".markdown") || file.ends_with(".rst"));

	let has_non_markdown_files = changed_files.lines().any(|file| {
		!file.ends_with(".md")
			&& !file.ends_with(".markdown")
			&& !file.ends_with(".rst")
			&& !file.trim().is_empty()
	});

	// Count files and changes
	let file_count = diff.matches("diff --git").count();
	let additions = diff
		.matches("\n+")
		.count()
		.saturating_sub(diff.matches("\n+++").count());
	let deletions = diff
		.matches("\n-")
		.count()
		.saturating_sub(diff.matches("\n---").count());

	// Build the guidance section
	let mut guidance_section = String::new();
	if let Some(context) = extra_context {
		guidance_section = format!("\n\nUser guidance for commit intent:\n{}", context);
	}

	// Build docs type restriction based on file analysis
	let docs_restriction = if has_non_markdown_files && !has_markdown_files {
		// Only non-markdown files changed - explicitly forbid docs
		"\n\nCRITICAL - DOCS TYPE RESTRICTION:\n\
		- NEVER use 'docs(...)' when only non-markdown files are changed\n\
		- Current changes include ONLY non-markdown files (.rs, .js, .py, .toml, etc.)\n\
		- Use 'fix', 'feat', 'refactor', 'chore', etc. instead of 'docs'\n\
		- 'docs' is ONLY for .md, .markdown, .rst files or documentation-only changes"
	} else if has_non_markdown_files && has_markdown_files {
		// Mixed files - provide guidance
		"\n\nDOCS TYPE GUIDANCE:\n\
		- Use 'docs(...)' ONLY if the primary change is documentation\n\
		- If code changes are the main focus, use appropriate code type (fix, feat, refactor)\n\
		- Mixed changes: prioritize the most significant change type"
	} else {
		// Only markdown files or no files detected - allow docs
		""
	};

	// Check if we need to chunk the diff
	let chunks = diff_chunker::chunk_diff(&diff);

	if chunks.len() == 1 {
		// Single chunk - use existing logic
		let prompt = create_commit_prompt(
			&chunks[0].content,
			file_count,
			additions,
			deletions,
			&guidance_section,
			docs_restriction,
		);
		return call_llm_with_retry(
			|| call_llm_for_commit_message(&prompt, config),
			"Single chunk commit message",
		)
		.await;
	}

	// Multiple chunks - process in parallel for better performance
	println!(
		"📝 Processing large diff in {} chunks in parallel...",
		chunks.len()
	);

	let responses = process_commit_chunks_parallel(
		&chunks,
		file_count,
		additions,
		deletions,
		&guidance_section,
		docs_restriction,
		config,
	)
	.await;

	if responses.is_empty() {
		return Ok("chore: update files".to_string());
	}

	// Combine responses into final commit message
	let combined = diff_chunker::combine_commit_messages(responses);

	// Always apply AI refinement for chunked messages
	println!("đŸŽ¯ Refining commit message with AI...");
	match refine_commit_message_with_ai(&combined, config).await {
		Ok(refined) => Ok(refined),
		Err(e) => {
			eprintln!(
				"Warning: AI refinement failed ({}), using combined message",
				e
			);
			Ok(combined)
		}
	}
}

/// Generate a commit message from a pre-fetched diff string (used by -c/--commit rewrite mode).
///
/// Shares all prompt-building and chunking logic with `generate_commit_message_chunked`
/// but accepts the diff directly instead of reading staged changes.
async fn generate_commit_message_from_diff(
	diff: &str,
	config: &Config,
	extra_context: Option<&str>,
) -> Result<String> {
	// Derive file list from the diff headers (no git staging involved)
	let changed_files: String = diff
		.lines()
		.filter(|l| l.starts_with("diff --git "))
		.filter_map(|l| l.split(" b/").nth(1))
		.collect::<Vec<_>>()
		.join("\n");

	let has_markdown_files = changed_files
		.lines()
		.any(|f| f.ends_with(".md") || f.ends_with(".markdown") || f.ends_with(".rst"));

	let has_non_markdown_files = changed_files.lines().any(|f| {
		!f.ends_with(".md")
			&& !f.ends_with(".markdown")
			&& !f.ends_with(".rst")
			&& !f.trim().is_empty()
	});

	let file_count = diff.matches("diff --git").count();
	let additions = diff
		.matches("\n+")
		.count()
		.saturating_sub(diff.matches("\n+++").count());
	let deletions = diff
		.matches("\n-")
		.count()
		.saturating_sub(diff.matches("\n---").count());

	let guidance_section = extra_context
		.map(|c| format!("\n\nUser guidance for commit intent:\n{}", c))
		.unwrap_or_default();

	let docs_restriction = if has_non_markdown_files && !has_markdown_files {
		"\n\nCRITICAL - DOCS TYPE RESTRICTION:\n\
		- NEVER use 'docs(...)' when only non-markdown files are changed\n\
		- Current changes include ONLY non-markdown files (.rs, .js, .py, .toml, etc.)\n\
		- Use 'fix', 'feat', 'refactor', 'chore', etc. instead of 'docs'\n\
		- 'docs' is ONLY for .md, .markdown, .rst files or documentation-only changes"
	} else if has_non_markdown_files && has_markdown_files {
		"\n\nDOCS TYPE GUIDANCE:\n\
		- Use 'docs(...)' ONLY if the primary change is documentation\n\
		- If code changes are the main focus, use appropriate code type (fix, feat, refactor)\n\
		- Mixed changes: prioritize the most significant change type"
	} else {
		""
	};

	let chunks = diff_chunker::chunk_diff(diff);

	if chunks.len() == 1 {
		let prompt = create_commit_prompt(
			&chunks[0].content,
			file_count,
			additions,
			deletions,
			&guidance_section,
			docs_restriction,
		);
		return call_llm_with_retry(
			|| call_llm_for_commit_message(&prompt, config),
			"Single chunk commit message",
		)
		.await;
	}

	println!(
		"📝 Processing large diff in {} chunks in parallel...",
		chunks.len()
	);

	let responses = process_commit_chunks_parallel(
		&chunks,
		file_count,
		additions,
		deletions,
		&guidance_section,
		docs_restriction,
		config,
	)
	.await;

	if responses.is_empty() {
		return Ok("chore: update files".to_string());
	}

	let combined = diff_chunker::combine_commit_messages(responses);

	println!("đŸŽ¯ Refining commit message with AI...");
	match refine_commit_message_with_ai(&combined, config).await {
		Ok(refined) => Ok(refined),
		Err(e) => {
			eprintln!(
				"Warning: AI refinement failed ({}), using combined message",
				e
			);
			Ok(combined)
		}
	}
}

/// Create a standardized commit prompt for LLM processing
fn create_commit_prompt(
	diff_content: &str,
	file_count: usize,
	additions: usize,
	deletions: usize,
	guidance_section: &str,
	docs_restriction: &str,
) -> String {
	format!(
		"STRICT FORMAT: Plain text commit message, NO markdown, NO backticks, NO code blocks.
type(scope): description under 50 chars
Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build
Use imperative mood (add not added, fix not fixed)
Avoid generic words: update, change, modify, various, several
Focus on WHAT functionality changed, not implementation details{}
---
COMMIT TYPE GUIDE:
feat: NEW functionality being added
fix: CORRECTING bugs/errors/broken functionality
refactor: IMPROVING code without changing functionality
perf: OPTIMIZING performance
docs: .md/.markdown/.rst files ONLY
test: ADDING or fixing tests
style: formatting/whitespace (no logic changes)
chore: maintenance (dependencies, build, tooling)
ci: workflows/pipelines
build: Cargo.toml, package.json, Makefile{}
---
FEATURE vs FIX: Working code with bugs = fix, completely new = feat
Examples: fix(auth): resolve token validation error, feat(auth): add OAuth2 support
When in doubt: choose fix if addressing problems, feat if adding new
---
BREAKING CHANGES: Function/API signature changes, removed public methods, interface/trait changes
Library code: mark any public interface changes as breaking
Application code: internal changes dont need marker unless affects config/user-facing
Use type! format and add BREAKING CHANGE footer if detected
---
BODY NEEDED if: 4+ files OR 25+ lines OR multiple change types OR complex refactoring OR breaking changes
Body format (plain text):
- Blank line after subject
- Each point starts with dash space: -
- Focus on key changes and purpose
- Keep bullets concise (1 line max)
- For breaking: add BREAKING CHANGE: description
---
OUTPUT FORMAT: Plain text only
Subject: type(scope): description
If body: blank line then dash bullets
If breaking: BREAKING CHANGE: line
NO code blocks, NO backticks, NO markdown
---
Changes: {} files (+{} -{} lines)

Git diff:
{}

Generate commit message:",
		guidance_section,
		docs_restriction,
		file_count,
		additions,
		deletions,
		diff_content
	)
}
async fn collect_ordered_responses(
	mut join_set: JoinSet<Result<(usize, String)>>,
	expected_count: usize,
) -> Vec<String> {
	let mut ordered_responses = vec![None; expected_count];

	while let Some(result) = join_set.join_next().await {
		match result {
			Ok(Ok((index, response))) => {
				ordered_responses[index] = Some(response);
			}
			Ok(Err(_)) => {
				// Error already logged in spawn
			}
			Err(e) => {
				eprintln!("Warning: Task join error: {}", e);
			}
		}
	}

	// Extract successful responses
	ordered_responses.into_iter().flatten().collect()
}

/// Process chunks in parallel with proper error handling and resource limits
///
/// Processes multiple diff chunks concurrently using tokio tasks for improved performance.
/// Implements resource limits to prevent system overload and maintains result ordering.
///
/// # Arguments
/// * `chunks` - Array of diff chunks to process
/// * `file_count` - Total number of files changed (for context)
/// * `additions` - Total lines added (for context)
/// * `deletions` - Total lines deleted (for context)
/// * `guidance_section` - User-provided guidance for commit message
/// * `docs_restriction` - Documentation type restrictions
/// * `config` - Application configuration
///
/// # Returns
/// Vector of successful LLM responses in original chunk order
async fn process_commit_chunks_parallel(
	chunks: &[diff_chunker::DiffChunk],
	file_count: usize,
	additions: usize,
	deletions: usize,
	guidance_section: &str,
	docs_restriction: &str,
	config: &Config,
) -> Vec<String> {
	// Limit parallel processing to prevent resource exhaustion
	let chunk_limit = std::cmp::min(chunks.len(), diff_chunker::MAX_PARALLEL_CHUNKS);
	let mut join_set = JoinSet::new();

	// Process chunks in parallel batches
	for (i, chunk) in chunks.iter().take(chunk_limit).enumerate() {
		let chunk_content = chunk.content.clone();
		let chunk_summary = chunk.file_summary.clone();
		let config = config.clone();
		let guidance_section = guidance_section.to_string();
		let docs_restriction = docs_restriction.to_string();

		join_set.spawn(async move {
			println!(
				"  Processing chunk {}/{}: {}",
				i + 1,
				chunk_limit,
				chunk_summary
			);

			let chunk_prompt = create_commit_prompt(
				&chunk_content,
				file_count,
				additions,
				deletions,
				&guidance_section,
				&docs_restriction,
			);

			match call_llm_for_commit_message(&chunk_prompt, &config).await {
				Ok(response) => Ok((i, response)),
				Err(e) => {
					eprintln!("Warning: Chunk {} failed ({})", i + 1, e);
					Err(e)
				}
			}
		});
	}

	// Collect results maintaining order
	collect_ordered_responses(join_set, chunk_limit).await
}

async fn call_llm_for_commit_message(prompt: &str, config: &Config) -> Result<String> {
	use octocode::llm::{LlmClient, Message};

	// Create LLM client from config
	let client = LlmClient::from_config(config)?;

	// Build messages
	let messages = vec![Message::user(prompt)];

	// Call LLM with low temperature for consistent commit messages
	let response = client
		.chat_completion_with_temperature(messages, 0.1)
		.await?;

	Ok(response)
}

/// Refine a verbose commit message using AI to create a concise, deduplicated version
///
/// Takes a combined commit message from multiple chunks and uses AI to:
/// - Remove duplication and redundancy
/// - Create concise bullet points
/// - Maintain proper conventional commit format
/// - Preserve important technical details
///
/// # Arguments
/// * `verbose_message` - The combined verbose commit message from chunks
/// * `config` - Application configuration
///
/// # Returns
/// A refined, concise commit message
async fn refine_commit_message_with_ai(verbose_message: &str, config: &Config) -> Result<String> {
	let refinement_prompt = format!(
		"REFINE COMMIT MESSAGE - CRITICAL: Output must be PLAIN TEXT ONLY, NO MARKDOWN, NO backticks, NO code blocks.

ORIGINAL MESSAGE:
{}

REFINEMENT REQUIREMENTS:
1. Keep conventional format: type(scope): description
2. Choose single best type (feat, fix, refactor, chore, docs, etc.)
3. Remove ALL duplication and redundancy
4. Create concise bullet points for body (plain text, use dash space)
5. Focus on WHAT changed, not implementation details
6. Max 50 chars for subject line
7. Max 72 chars per body line
8. Group related changes together
9. Remove verbose explanations, keep factual and brief

OUTPUT FORMAT (PLAIN TEXT ONLY):
refactor(diff_chunker): improve chunking with limits and robustness

- Add resource limits to prevent exhaustion
- Enhance filename extraction accuracy
- Improve error handling and logging
- Add comprehensive test coverage

Return ONLY refined commit message as plain text, nothing else.",
		verbose_message
	);

	call_llm_with_retry(
		|| call_llm_for_commit_message(&refinement_prompt, config),
		"AI commit message refinement",
	)
	.await
}

/// Check if pre-commit binary is available in PATH
fn is_precommit_available() -> bool {
	Command::new("pre-commit")
		.arg("--version")
		.output()
		.map(|output| output.status.success())
		.unwrap_or(false)
}

/// Check if pre-commit is configured in the repository
fn has_precommit_config(repo_path: &std::path::Path) -> bool {
	repo_path.join(".pre-commit-config.yaml").exists()
		|| repo_path.join(".pre-commit-config.yml").exists()
}

/// Run pre-commit hooks intelligently based on the situation
async fn run_precommit_hooks(
	repo_path: &std::path::Path,
	run_all: bool,
	originally_staged_files: &[String],
) -> Result<()> {
	// Check if pre-commit is available and configured
	if !is_precommit_available() {
		// No pre-commit binary available, skip silently
		return Ok(());
	}

	if !has_precommit_config(repo_path) {
		// No pre-commit config found, skip silently
		return Ok(());
	}

	println!("🔧 Running pre-commit hooks...");

	// Determine which pre-commit command to run
	let pre_commit_args = if run_all {
		// When --all flag is used, run on all files
		vec!["run", "--all-files"]
	} else {
		// Run only on staged files (default pre-commit behavior)
		vec!["run"]
	};

	let output = Command::new("pre-commit")
		.args(&pre_commit_args)
		.current_dir(repo_path)
		.output()?;

	// Pre-commit can return non-zero exit codes for various reasons:
	// - Code 0: All hooks passed
	// - Code 1: Some hooks failed or made changes
	// - Code 3: No hooks to run
	match output.status.code() {
		Some(0) => {
			println!("✅ Pre-commit hooks passed successfully");
		}
		Some(1) => {
			// Hooks made changes or failed
			let stderr = String::from_utf8_lossy(&output.stderr);
			let stdout = String::from_utf8_lossy(&output.stdout);

			if !stdout.is_empty() {
				println!("📝 Pre-commit output:\n{}", stdout);
			}

			// Check if files were modified by pre-commit
			let modified_output = Command::new("git")
				.args(["diff", "--name-only"])
				.current_dir(repo_path)
				.output()?;

			if modified_output.status.success() {
				let all_modified_files = String::from_utf8_lossy(&modified_output.stdout);
				let all_modified_set: std::collections::HashSet<&str> =
					all_modified_files.lines().collect();

				// Only consider files that were originally staged AND were modified by pre-commit
				let staged_and_modified: Vec<&String> = originally_staged_files
					.iter()
					.filter(|file| all_modified_set.contains(file.as_str()))
					.collect();

				if !staged_and_modified.is_empty() {
					println!("🔄 Pre-commit hooks modified originally staged files:");
					for file in &staged_and_modified {
						println!("  â€ĸ {}", file);
					}

					// Re-add modified files to staging area
					println!("📂 Re-staging modified files...");
					for file in &staged_and_modified {
						let add_output = Command::new("git")
							.args(["add", file.trim()])
							.current_dir(repo_path)
							.output()?;

						if !add_output.status.success() {
							eprintln!(
								"âš ī¸  Warning: Failed to re-stage {}: {}",
								file,
								String::from_utf8_lossy(&add_output.stderr)
							);
						}
					}
					println!("✅ Modified files re-staged successfully");
				}
			}

			// If there were actual failures (not just modifications), show them
			if !stderr.is_empty() && stderr.contains("FAILED") {
				println!("âš ī¸  Some pre-commit hooks failed:\n{}", stderr);
				// Don't fail the commit process, let user decide
			}
		}
		Some(3) => {
			println!("â„šī¸  No pre-commit hooks configured to run");
		}
		Some(code) => {
			let stderr = String::from_utf8_lossy(&output.stderr);
			println!("âš ī¸  Pre-commit exited with code {}: {}", code, stderr);
			// Don't fail the commit process for other exit codes
		}
		None => {
			println!("âš ī¸  Pre-commit was terminated by signal");
		}
	}

	Ok(())
}