git-ai 1.1.1

Git AI: Automates commit messages using ChatGPT. Stage your files, and Git AI generates the messages.
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
use anyhow::Result;
use async_openai::config::OpenAIConfig;
use async_openai::types::{ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs};
use async_openai::Client;
use serde_json::Value;
use futures::future::join_all;

use crate::multi_step_analysis::{
  create_analyze_function_tool, create_generate_function_tool, create_score_function_tool, FileDataForScoring, FileWithScore
};
use crate::function_calling::{create_commit_function_tool, CommitFunctionArgs};
use crate::debug_output;

/// Represents a parsed file from the git diff
#[derive(Debug)]
pub struct ParsedFile {
  pub path:         String,
  pub operation:    String,
  pub diff_content: String
}

/// Main entry point for multi-step commit message generation
pub async fn generate_commit_message_multi_step(
  client: &Client<OpenAIConfig>, model: &str, diff_content: &str, max_length: Option<usize>
) -> Result<String> {
  log::info!("Starting multi-step commit message generation");

  // Initialize multi-step debug session
  if let Some(session) = debug_output::debug_session() {
    session.init_multi_step_debug();
  }

  // Parse the diff to extract individual files
  let parsed_files = parse_diff(diff_content)?;
  log::info!("Parsed {} files from diff", parsed_files.len());

  // Track files parsed in debug session
  if let Some(session) = debug_output::debug_session() {
    session.set_total_files_parsed(parsed_files.len());
  }

  // Step 1: Analyze each file individually in parallel
  log::debug!("Analyzing {} files in parallel", parsed_files.len());

  // Create futures for all file analyses
  let analysis_futures: Vec<_> = parsed_files
    .iter()
    .map(|file| {
      let file_path = file.path.clone();
      let operation = file.operation.clone();
      async move {
        log::debug!("Analyzing file: {file_path}");
        let start_time = std::time::Instant::now();
        let payload = format!("{{\"file_path\": \"{file_path}\", \"operation_type\": \"{operation}\", \"diff_content\": \"...\"}}");

        let result = call_analyze_function(client, model, file).await;
        let duration = start_time.elapsed();
        (file, result, duration, payload)
      }
    })
    .collect();

  // Execute all analyses in parallel
  let analysis_results = join_all(analysis_futures).await;

  // Process results and handle errors
  let mut file_analyses = Vec::new();
  for (i, (file, result, duration, payload)) in analysis_results.into_iter().enumerate() {
    match result {
      Ok(analysis) => {
        log::debug!("Successfully analyzed file {}: {}", i, file.path);

        // Extract structured analysis data for debug
        let analysis_result = crate::multi_step_analysis::FileAnalysisResult {
          lines_added:   analysis["lines_added"].as_u64().unwrap_or(0) as u32,
          lines_removed: analysis["lines_removed"].as_u64().unwrap_or(0) as u32,
          file_category: analysis["file_category"]
            .as_str()
            .unwrap_or("source")
            .to_string(),
          summary:       analysis["summary"].as_str().unwrap_or("").to_string()
        };

        // Record in debug session
        if let Some(session) = debug_output::debug_session() {
          session.add_file_analysis_debug(file.path.clone(), file.operation.clone(), analysis_result.clone(), duration, payload);
        }

        file_analyses.push((file, analysis));
      }
      Err(e) => {
        // Check if it's an API key error - if so, propagate it immediately
        let error_str = e.to_string();
        if error_str.contains("invalid_api_key") || error_str.contains("Incorrect API key") || error_str.contains("Invalid API key") {
          return Err(e);
        }
        log::warn!("Failed to analyze file {}: {}", file.path, e);
        // Continue with other files even if one fails
      }
    }
  }

  if file_analyses.is_empty() {
    anyhow::bail!("Failed to analyze any files");
  }

  // Step 2: Calculate impact scores
  let files_data: Vec<FileDataForScoring> = file_analyses
    .iter()
    .map(|(file, analysis)| {
      FileDataForScoring {
        file_path:      file.path.clone(),
        operation_type: file.operation.clone(),
        lines_added:    analysis["lines_added"].as_u64().unwrap_or(0) as u32,
        lines_removed:  analysis["lines_removed"].as_u64().unwrap_or(0) as u32,
        file_category:  analysis["file_category"]
          .as_str()
          .unwrap_or("source")
          .to_string(),
        summary:        analysis["summary"].as_str().unwrap_or("").to_string()
      }
    })
    .collect();

  // Record impact score calculation
  let score_start_time = std::time::Instant::now();
  let score_payload = format!(
    "{{\"files_data\": [{{\"{}\", ...}}, ...]}}",
    if !files_data.is_empty() {
      &files_data[0].file_path
    } else {
      "no files"
    }
  );

  // Start step 2 and 3 in parallel
  // First create the futures for both operations
  let score_future = call_score_function(client, model, files_data);

  // Run the scoring operation
  let scored_files = score_future.await?;
  let score_duration = score_start_time.elapsed();

  // Record in debug session
  if let Some(session) = debug_output::debug_session() {
    session.set_score_debug(scored_files.clone(), score_duration, score_payload);
  }

  // Step 3: Generate commit message candidates
  let generate_start_time = std::time::Instant::now();
  let generate_payload = format!("{{\"files_with_scores\": [...], \"max_length\": {}}}", max_length.unwrap_or(72));

  // Now create and run the generate and select steps in parallel
  let generate_future = call_generate_function(client, model, scored_files.clone(), max_length.unwrap_or(72));

  let candidates = generate_future.await?;
  let generate_duration = generate_start_time.elapsed();

  // Record in debug session
  if let Some(session) = debug_output::debug_session() {
    session.set_generate_debug(candidates.clone(), generate_duration, generate_payload);
  }

  // Step 4: Select the best candidate and format final response
  let final_message_start_time = std::time::Instant::now();
  let final_message = select_best_candidate(client, model, &candidates, &scored_files, diff_content).await?;
  let final_message_duration = final_message_start_time.elapsed();

  // Record in debug session
  if let Some(session) = debug_output::debug_session() {
    session.set_final_message_debug(final_message_duration);
    session.set_commit_result(final_message.clone(), candidates["reasoning"].as_str().unwrap_or("").to_string());
  }

  Ok(final_message)
}

/// Extracts the file path from git diff header parts.
/// Handles various git prefixes (a/, b/, c/, i/) and /dev/null for deleted files.
///
/// # Arguments
/// * `parts` - The whitespace-split parts from a "diff --git" line
///
/// # Returns
/// * `Option<String>` - The extracted path without prefixes, or None if parsing fails
fn extract_file_path_from_diff_parts(parts: &[&str]) -> Option<String> {
  if parts.len() < 4 {
    return None;
  }

  // Helper to strip git prefixes (a/, b/, c/, i/)
  let strip_prefix = |s: &str| {
    s.trim_start_matches("a/")
      .trim_start_matches("b/")
      .trim_start_matches("c/")
      .trim_start_matches("i/")
      .to_string()
  };

  let new_path = strip_prefix(parts[3]);
  let old_path = strip_prefix(parts[2]);

  // Prefer new path unless it's /dev/null (deleted file)
  Some(if new_path == "/dev/null" || new_path == "dev/null" {
    old_path
  } else {
    new_path
  })
}

/// Parse git diff into individual files
pub fn parse_diff(diff_content: &str) -> Result<Vec<ParsedFile>> {
  let mut files = Vec::new();
  let mut current_file: Option<ParsedFile> = None;
  let mut current_diff = String::new();

  // Debug output
  log::debug!("Parsing diff with {} lines", diff_content.lines().count());

  // Add more detailed logging for debugging
  if log::log_enabled!(log::Level::Debug) && !diff_content.is_empty() {
    // Make sure we truncate at a valid UTF-8 character boundary
    let preview = if diff_content.len() > 500 {
      let truncated_index = diff_content
        .char_indices()
        .take_while(|(i, _)| *i < 500)
        .last()
        .map(|(i, c)| i + c.len_utf8())
        .unwrap_or(0);

      format!("{}... (truncated)", &diff_content[..truncated_index])
    } else {
      diff_content.to_string()
    };
    log::debug!("Diff content preview: \n{preview}");
  }

  // Handle different diff formats
  let mut in_diff_section = false;
  let mut _commit_hash_line: Option<&str> = None;

  // First scan to detect if this is a commit message with hash
  for line in diff_content.lines().take(3) {
    if line.len() >= 40 && line.chars().take(40).all(|c| c.is_ascii_hexdigit()) {
      _commit_hash_line = Some(line);
      break;
    }
  }

  // Process line by line
  for line in diff_content.lines() {
    // Skip commit hash lines and other metadata
    if line.starts_with("commit ") || (line.len() >= 40 && line.chars().take(40).all(|c| c.is_ascii_hexdigit())) || line.is_empty() {
      continue;
    }

    // Check if we're starting a new file diff
    if line.starts_with("diff --git") {
      in_diff_section = true;
      // Save previous file if exists
      if let Some(mut file) = current_file.take() {
        file.diff_content = current_diff.clone();
        log::debug!("Adding file to results: {} ({})", file.path, file.operation);
        files.push(file);
        current_diff.clear();
      }

      // Extract file path more carefully
      let parts: Vec<&str> = line.split_whitespace().collect();
      if let Some(path) = extract_file_path_from_diff_parts(&parts) {
        log::debug!("Found new file in diff: {path}");
        current_file = Some(ParsedFile {
          path,
          operation: "modified".to_string(), // Default, will be updated
          diff_content: String::new()
        });
      }

      // Add the header line to the diff content
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if line.starts_with("new file mode") {
      if let Some(ref mut file) = current_file {
        log::debug!("File {} is newly added", file.path);
        file.operation = "added".to_string();
      }
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if line.starts_with("deleted file mode") {
      if let Some(ref mut file) = current_file {
        log::debug!("File {} is deleted", file.path);
        file.operation = "deleted".to_string();
      }
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if line.starts_with("rename from") || line.starts_with("rename to") {
      if let Some(ref mut file) = current_file {
        log::debug!("File {} is renamed", file.path);
        file.operation = "renamed".to_string();
      }
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if line.starts_with("Binary files") {
      if let Some(ref mut file) = current_file {
        log::debug!("File {} is binary", file.path);
        file.operation = "binary".to_string();
      }
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if line.starts_with("index ") || line.starts_with("--- ") || line.starts_with("+++ ") || line.starts_with("@@ ") {
      // These are important diff headers that should be included
      current_diff.push_str(line);
      current_diff.push('\n');
    } else if in_diff_section {
      current_diff.push_str(line);
      current_diff.push('\n');
    }
  }

  // Don't forget the last file
  if let Some(mut file) = current_file {
    file.diff_content = current_diff;
    log::debug!("Adding final file to results: {} ({})", file.path, file.operation);
    files.push(file);
  }

  // If we didn't parse any files, check if this looks like a raw git diff output
  // from commands like `git show` that include commit info at the top
  if files.is_empty() && !diff_content.trim().is_empty() {
    log::debug!("Trying to parse as raw git diff output with commit info");

    // Extract sections that start with "diff --git"
    let sections: Vec<&str> = diff_content.split("diff --git").skip(1).collect();

    if !sections.is_empty() {
      for (i, section) in sections.iter().enumerate() {
        // Add the "diff --git" prefix back
        let full_section = format!("diff --git{section}");

        // Extract file path from the section more carefully
        let mut found_path = false;

        // Safer approach: iterate through lines and find the path
        let mut extracted_path = String::new();
        for section_line in full_section.lines().take(3) {
          if section_line.starts_with("diff --git") {
            let parts: Vec<&str> = section_line.split_whitespace().collect();
            if let Some(p) = extract_file_path_from_diff_parts(&parts) {
              extracted_path = p;
              found_path = true;
              break;
            }
          }
        }

        if found_path {
          log::debug!("Found file in section {i}: {extracted_path}");
          files.push(ParsedFile {
            path:         extracted_path,
            operation:    "modified".to_string(), // Default
            diff_content: full_section
          });
        }
      }
    }
  }

  // If still no files were parsed, treat the entire diff as a single change
  if files.is_empty() && !diff_content.trim().is_empty() {
    log::debug!("No standard diff format found, treating as single file change");
    files.push(ParsedFile {
      path:         "unknown".to_string(),
      operation:    "modified".to_string(),
      diff_content: diff_content.to_string()
    });
  }

  log::debug!("Parsed {} files from diff", files.len());

  // Add detailed debug output for each parsed file
  if log::log_enabled!(log::Level::Debug) {
    for (i, file) in files.iter().enumerate() {
      let content_preview = if file.diff_content.len() > 200 {
        // Make sure we truncate at a valid UTF-8 character boundary
        let truncated_index = file
          .diff_content
          .char_indices()
          .take_while(|(i, _)| *i < 200)
          .last()
          .map(|(i, c)| i + c.len_utf8())
          .unwrap_or(0);

        format!("{}... (truncated)", &file.diff_content[..truncated_index])
      } else {
        file.diff_content.clone()
      };
      log::debug!("File {}: {} ({})\nContent preview:\n{}", i, file.path, file.operation, content_preview);
    }
  }

  Ok(files)
}

/// Call the analyze function via OpenAI
async fn call_analyze_function(client: &Client<OpenAIConfig>, model: &str, file: &ParsedFile) -> Result<Value> {
  let tools = vec![create_analyze_function_tool()?];

  let system_message = ChatCompletionRequestSystemMessageArgs::default()
    .content("You are a git diff analyzer. Analyze the provided file changes and return structured data.")
    .build()?
    .into();

  let user_message = ChatCompletionRequestUserMessageArgs::default()
    .content(format!(
      "Analyze this file change:\nPath: {}\nOperation: {}\nDiff:\n{}",
      file.path, file.operation, file.diff_content
    ))
    .build()?
    .into();

  let request = CreateChatCompletionRequestArgs::default()
    .model(model)
    .messages(vec![system_message, user_message])
    .tools(tools)
    .tool_choice("analyze")
    .build()?;

  let response = client.chat().create(request).await?;

  if let Some(tool_call) = response.choices[0]
    .message
    .tool_calls
    .as_ref()
    .and_then(|calls| calls.first())
  {
    let args: Value = serde_json::from_str(&tool_call.function.arguments)?;
    Ok(args)
  } else {
    anyhow::bail!("No tool call in response")
  }
}

/// Call the score function via OpenAI
async fn call_score_function(
  client: &Client<OpenAIConfig>, model: &str, files_data: Vec<FileDataForScoring>
) -> Result<Vec<FileWithScore>> {
  let tools = vec![create_score_function_tool()?];

  let system_message = ChatCompletionRequestSystemMessageArgs::default()
    .content("You are a git commit impact scorer. Calculate impact scores for the provided file changes.")
    .build()?
    .into();

  let user_message = ChatCompletionRequestUserMessageArgs::default()
    .content(format!(
      "Calculate impact scores for these {} file changes:\n{}",
      files_data.len(),
      serde_json::to_string_pretty(&files_data)?
    ))
    .build()?
    .into();

  let request = CreateChatCompletionRequestArgs::default()
    .model(model)
    .messages(vec![system_message, user_message])
    .tools(tools)
    .tool_choice("score")
    .build()?;

  let response = client.chat().create(request).await?;

  if let Some(tool_call) = response.choices[0]
    .message
    .tool_calls
    .as_ref()
    .and_then(|calls| calls.first())
  {
    let args: Value = serde_json::from_str(&tool_call.function.arguments)?;
    let files_with_scores: Vec<FileWithScore> = if args["files_with_scores"].is_null() {
      Vec::new() // Return empty vector if null
    } else {
      serde_json::from_value(args["files_with_scores"].clone())?
    };
    Ok(files_with_scores)
  } else {
    anyhow::bail!("No tool call in response")
  }
}

/// Call the generate function via OpenAI
async fn call_generate_function(
  client: &Client<OpenAIConfig>, model: &str, files_with_scores: Vec<FileWithScore>, max_length: usize
) -> Result<Value> {
  let tools = vec![create_generate_function_tool()?];

  let system_message = ChatCompletionRequestSystemMessageArgs::default()
    .content("You are a git commit message generator. Generate concise, descriptive commit messages.")
    .build()?
    .into();

  let user_message = ChatCompletionRequestUserMessageArgs::default()
    .content(format!(
      "Generate commit message candidates (max {} chars) for these scored changes:\n{}",
      max_length,
      serde_json::to_string_pretty(&files_with_scores)?
    ))
    .build()?
    .into();

  let request = CreateChatCompletionRequestArgs::default()
    .model(model)
    .messages(vec![system_message, user_message])
    .tools(tools)
    .tool_choice("generate")
    .build()?;

  let response = client.chat().create(request).await?;

  if let Some(tool_call) = response.choices[0]
    .message
    .tool_calls
    .as_ref()
    .and_then(|calls| calls.first())
  {
    let args: Value = serde_json::from_str(&tool_call.function.arguments)?;
    Ok(args)
  } else {
    anyhow::bail!("No tool call in response")
  }
}

/// Select the best candidate and format the final response
async fn select_best_candidate(
  client: &Client<OpenAIConfig>, model: &str, candidates: &Value, scored_files: &[FileWithScore], original_diff: &str
) -> Result<String> {
  // Use the original commit function to get the final formatted response
  let tools = vec![create_commit_function_tool(Some(72))?];

  let system_message = ChatCompletionRequestSystemMessageArgs::default()
    .content(
      "You are a git commit message expert. Based on the multi-step analysis, \
            select the best commit message and provide the final formatted response."
    )
    .build()?
    .into();

  let user_message = ChatCompletionRequestUserMessageArgs::default()
    .content(format!(
      "Based on this multi-step analysis:\n\n\
            Candidates: {}\n\
            Reasoning: {}\n\n\
            Scored files: {}\n\n\
            Original diff:\n{}\n\n\
            Select the best commit message and format the response using the commit function.",
      serde_json::to_string_pretty(&candidates["candidates"])?,
      candidates["reasoning"].as_str().unwrap_or(""),
      serde_json::to_string_pretty(&scored_files)?,
      original_diff
    ))
    .build()?
    .into();

  let request = CreateChatCompletionRequestArgs::default()
    .model(model)
    .messages(vec![system_message, user_message])
    .tools(tools)
    .tool_choice("commit")
    .build()?;

  let response = client.chat().create(request).await?;

  if let Some(tool_call) = response.choices[0]
    .message
    .tool_calls
    .as_ref()
    .and_then(|calls| calls.first())
  {
    // First, parse as Value to manually handle required fields
    let raw_args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)?;

    // Extract the message which is what we really need
    if let Some(message) = raw_args.get("message").and_then(|m| m.as_str()) {
      return Ok(message.to_string());
    }

    // Fallback to full parsing if the above approach fails
    let args: CommitFunctionArgs = serde_json::from_str(&tool_call.function.arguments)?;
    Ok(args.message)
  } else {
    anyhow::bail!("No tool call in response")
  }
}

/// Alternative: Use the multi-step analysis locally without OpenAI calls
pub fn generate_commit_message_local(diff_content: &str, max_length: Option<usize>) -> Result<String> {
  use crate::multi_step_analysis::{analyze_file, calculate_impact_scores, generate_commit_messages};

  log::info!("Starting local multi-step commit message generation");

  // Parse the diff
  let parsed_files = parse_diff(diff_content)?;

  // Track files parsed in debug session
  if let Some(session) = debug_output::debug_session() {
    session.set_total_files_parsed(parsed_files.len());
  }

  // Step 1: Analyze each file
  let mut files_data = Vec::new();
  for file in parsed_files {
    let analysis = analyze_file(&file.path, &file.diff_content, &file.operation);
    files_data.push(FileDataForScoring {
      file_path:      file.path,
      operation_type: file.operation,
      lines_added:    analysis.lines_added,
      lines_removed:  analysis.lines_removed,
      file_category:  analysis.file_category,
      summary:        analysis.summary
    });
  }

  // Step 2: Calculate scores
  let score_result = calculate_impact_scores(files_data);

  // Step 3: Generate candidates
  let generate_result = generate_commit_messages(score_result.files_with_scores, max_length.unwrap_or(72));

  // Return the first candidate
  Ok(
    generate_result
      .candidates
      .first()
      .cloned()
      .unwrap_or_else(|| "Update files".to_string())
  )
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_parse_diff() {
    let diff = r#"diff --git a/src/main.rs b/src/main.rs
index 1234567..abcdefg 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
 fn main() {
-    println!("Hello");
+    println!("Hello, world!");
+    println!("New line");
 }
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..1111111
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "test"
+version = "0.1.0"
"#;

    let files = parse_diff(diff).unwrap();
    assert_eq!(files.len(), 2);
    assert_eq!(files[0].path, "src/main.rs");
    assert_eq!(files[0].operation, "modified");
    assert_eq!(files[1].path, "Cargo.toml");
    assert_eq!(files[1].operation, "added");

    // Verify files contain diff content
    assert!(!files[0].diff_content.is_empty());
    assert!(!files[1].diff_content.is_empty());
  }

  #[test]
  fn test_parse_diff_with_commit_hash() {
    // Test with a commit hash and message before the diff
    let diff = r#"0472ffa1665c4c5573fb8f7698c9965122eda675 Update files
diff --git a/src/openai.rs b/src/openai.rs
index a67ebbe..da223be 100644
--- a/src/openai.rs
+++ b/src/openai.rs
@@ -15,11 +15,6 @@ use crate::multi_step_integration::{generate_commit_message_local, generate_comm

 const MAX_ATTEMPTS: usize = 3;

-#[derive(Debug, Clone, PartialEq)]
-pub struct Response {
-  pub response: String
-}
-
 #[derive(Debug, Clone, PartialEq)]
 pub struct Request {
   pub prompt:     String,
@@ -28,6 +23,11 @@ pub struct Request {
   pub model:      Model
 }

+#[derive(Debug, Clone, PartialEq)]
+pub struct Response {
+  pub response: String
+}
+
 /// Generates an improved commit message using the provided prompt and diff
 /// Now uses the multi-step approach by default
 pub async fn generate_commit_message(diff: &str) -> Result<String> {
"#;

    let files = parse_diff(diff).unwrap();
    assert_eq!(files.len(), 1);
    assert_eq!(files[0].path, "src/openai.rs");
    assert_eq!(files[0].operation, "modified");

    // Verify diff content contains actual changes
    assert!(files[0].diff_content.contains("pub struct Response"));

    // Verify commit hash line was skipped
    assert!(!files[0]
      .diff_content
      .contains("0472ffa1665c4c5573fb8f7698c9965122eda675"));
  }

  #[test]
  fn test_parse_diff_with_c_i_prefixes() {
    // Test with c/ and i/ prefixes that appear in git hook diffs
    let diff = r#"diff --git c/test.md i/test.md
new file mode 100644
index 0000000..6c61a60
--- /dev/null
+++ i/test.md
@@ -0,0 +1 @@
+# Test File

diff --git c/test.js i/test.js
new file mode 100644
index 0000000..a730e61
--- /dev/null
+++ i/test.js
@@ -0,0 +1 @@
+console.log('Hello');
"#;

    let files = parse_diff(diff).unwrap();
    assert_eq!(files.len(), 2);
    assert_eq!(files[0].path, "test.md", "Should extract clean path without i/ prefix");
    assert_eq!(files[0].operation, "added");
    assert_eq!(files[1].path, "test.js", "Should extract clean path without i/ prefix");
    assert_eq!(files[1].operation, "added");

    // Verify files contain diff content
    assert!(files[0].diff_content.contains("# Test File"));
    assert!(files[1].diff_content.contains("console.log"));
  }

  #[test]
  fn test_parse_diff_with_deleted_file() {
    // Test with a deleted file (where b path is /dev/null)
    let diff = r#"diff --git a/deleted.txt b/dev/null
deleted file mode 100644
index 1234567..0000000
--- a/deleted.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-This file
-will be
-deleted
"#;

    let files = parse_diff(diff).unwrap();
    assert_eq!(files.len(), 1);
    assert_eq!(files[0].path, "deleted.txt", "Should use a path for deleted files");
    assert_eq!(files[0].operation, "deleted");

    // Verify file contains diff content
    assert!(files[0].diff_content.contains("This file"));
  }

  #[test]
  fn test_local_generation() {
    let diff = r#"diff --git a/src/auth.rs b/src/auth.rs
index 1234567..abcdefg 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -10,7 +10,15 @@ pub fn authenticate(user: &str, pass: &str) -> Result<Token> {
-    if user == "admin" && pass == "password" {
-        Ok(Token::new())
-    } else {
-        Err(AuthError::InvalidCredentials)
-    }
+    // Validate input
+    if user.is_empty() || pass.is_empty() {
+        return Err(AuthError::EmptyCredentials);
+    }
+
+    // Check credentials against database
+    let hashed = hash_password(pass);
+    if validate_user(user, &hashed)? {
+        Ok(Token::generate(user))
+    } else {
+        Err(AuthError::InvalidCredentials)
+    }
 }"#;

    let message = generate_commit_message_local(diff, Some(72)).unwrap();
    assert!(!message.is_empty());
    assert!(message.len() <= 72);
  }
}