turbocommit 3.0.0

A CLI tool to create commit messages with OpenAI GPT models for Git and Jujutsu (JJ) repositories
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
use crate::model;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::process;
use url::Url;

#[derive(Debug)]
pub struct ValidationError {
    field: String,
    message: String,
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.field.red(), self.message)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
    #[serde(default)]
    pub model: model::Model,
    #[serde(default)]
    pub api_endpoint: String,
    #[serde(default)]
    pub api_key_env_var: String,
    #[serde(default)]
    pub default_number_of_choices: i32,
    #[serde(default)]
    pub disable_auto_update_check: bool,
    #[serde(default)]
    pub reasoning_effort: String,
    #[serde(default)]
    pub verbosity: String,
    #[serde(default)]
    pub jj_rewrite_default: bool,
    #[serde(default)]
    pub system_msg: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            model: model::Model("gpt-5.1".to_string()),
            api_endpoint: String::from("https://api.openai.com/v1/chat/completions"),
            api_key_env_var: String::from("OPENAI_API_KEY"),
            default_number_of_choices: 3,
            disable_auto_update_check: false,
            reasoning_effort: String::from("low"),
            verbosity: String::from("medium"),
            jj_rewrite_default: false, // Default to overwrite mode
            system_msg: String::from("You are a specialized AI that generates high-quality conventional commit suggestions from git diffs. Your ONLY purpose is to produce properly formatted commits that follow the exact specification at conventionalcommits.org.

# INPUTS
- You will receive a git diff of staged files
- You MAY also receive a line starting with \"Current description:\" that contains the user's own summary/hint
- Additional user messages may request edits or add requirements

# OUTPUT FORMAT (MANDATORY)
- You MUST respond with JSON that satisfies the provided structured-output schema
- Populate the `suggestions` array with exactly the requested number of entries
- Each suggestion object must contain:
  - `title`: the conventional commit header (`<type>[optional scope][!]: <description>`)
  - `body`: optional string (or null) containing a single concise paragraph explaining the WHY behind the change
- Always emit both keys; set `body` to `null` explicitly when no added context is needed
- Never include Markdown, bullet lists, or explanatory prose outside of the structured fields

# COMMIT PHILOSOPHY
- Prioritize WHY the change exists over WHAT code was touched
- Surface user intent, motivation, and non-obvious implications
- Operate at a higher abstraction level than the diff itself

# VERBOSITY GUIDANCE
- `verbosity = low`: prefer title-only unless context is critically missing
- `verbosity = medium`: include a body when it clarifies motivation
- `verbosity = high`: almost always include a thoughtful body paragraph

# CONVENTIONAL COMMIT RULES
1. Types: feat, fix, docs, style, refac, test, build, ci, chore
2. Scope: optional noun in parentheses describing the impacted area
3. Breaking changes: add `!` before the colon or include a `BREAKING CHANGE:` footer (in body)
4. Description: imperative, lowercase start, no trailing period, focus on intent
5. Body (when present):
   - Blank line between title and body
   - Single paragraph centred on motivation and reasoning
   - Never re-list code changes; explain purpose and impact instead
6. Trust the \"Current description\" hint unless it contradicts the diff. Rephrase it into proper conventional commit style while preserving the core intent.

# EXAMPLES
- feat(auth): implement OAuth2 login flow
  Body: Explain why centralized auth improves security/user experience.
- fix(perf): reduce query latency under load
  Body: Describe the root cause and why the fix works.

# ADDITIONAL INSTRUCTIONS
- Always follow the schema; never emit free-form text
- Respect user edits/revision requests exactly
- If reasoning effort is `none`, keep responses efficient but still valid
- If verbosity is `high`, lean into clear, motivating context without rambling
- Never explain your reasoning or the schema back to the user; just output structured suggestions"),
        }
    }
}

impl Config {
    pub fn load_from_path(path: &std::path::Path) -> anyhow::Result<Self> {
        //debug log the path we load from
        println!("Loading config from path: {}", path.display());
        let config = match std::fs::read_to_string(path) {
            Ok(config_str) => match serde_yaml::from_str::<Self>(&config_str) {
                Ok(config) => config,
                Err(err) => {
                    return Err(anyhow::anyhow!("Configuration file parsing error: {}", err));
                }
            },
            Err(err) => match err.kind() {
                std::io::ErrorKind::NotFound => {
                    let msg = format!("Config file not found at: {}", path.display());
                    return Err(anyhow::anyhow!(msg));
                }
                _ => {
                    return Err(anyhow::anyhow!("Error reading configuration file: {}", err));
                }
            },
        };

        // Validate the configuration
        if let Err(validation_errors) = config.validate() {
            let mut error_msg = String::from("Configuration validation errors:\n");
            for error in validation_errors {
                error_msg.push_str(&format!("  {}\n", error));
            }
            error_msg.push_str(&format!(
                "\nConfiguration file location: {}",
                path.display()
            ));

            // If system message is empty, show the default
            if config.system_msg.trim().is_empty() {
                error_msg.push_str("\n\nDefault system message:\n");
                error_msg.push_str(&Self::default().system_msg);
            }

            return Err(anyhow::anyhow!(error_msg));
        }

        // After validation passes, fill in empty system message with default
        let mut config = config;
        if config.system_msg.trim().is_empty() {
            config.system_msg = Self::default().system_msg;
        }

        Ok(config)
    }

    pub fn load() -> anyhow::Result<Self> {
        let path = home::home_dir().map_or_else(
            || {
                println!("{}", "Unable to find home directory.".red());
                process::exit(1);
            },
            |path| path.join(".turbocommit.yaml"),
        );

        let config = match std::fs::read_to_string(&path) {
            Ok(config_str) => match serde_yaml::from_str::<Self>(&config_str) {
                Ok(config) => config,
                Err(err) => {
                    return Err(anyhow::anyhow!("Configuration file parsing error: {}", err));
                }
            },
            Err(err) => match err.kind() {
                std::io::ErrorKind::NotFound => {
                    println!(
                        "{}",
                        "No configuration file found, creating one with default values."
                            .bright_black()
                    );
                    let default = Self::default();
                    if let Err(e) = default.save_if_changed() {
                        println!(
                            "{}",
                            format!("Warning: Failed to create default config file: {}", e)
                                .yellow()
                        );
                    }
                    default
                }
                _ => {
                    return Err(anyhow::anyhow!("Error reading configuration file: {}", err));
                }
            },
        };

        // Validate the configuration
        if let Err(validation_errors) = config.validate() {
            let mut error_msg = String::from("Configuration validation errors:\n");
            for error in validation_errors {
                error_msg.push_str(&format!("  {}\n", error));
            }
            error_msg.push_str(&format!(
                "\nConfiguration file location: {}",
                path.display()
            ));

            // If system message is empty, show the default
            if config.system_msg.trim().is_empty() {
                error_msg.push_str("\n\nDefault system message:\n");
                error_msg.push_str(&Self::default().system_msg);
            }

            return Err(anyhow::anyhow!(error_msg));
        }

        // After validation passes, fill in empty system message with default
        let mut config = config;
        if config.system_msg.trim().is_empty() {
            config.system_msg = Self::default().system_msg;
        }

        Ok(config)
    }
    pub fn save_if_changed(&self) -> Result<(), std::io::Error> {
        let path = home::home_dir().map_or_else(
            || {
                println!("{}", "Unable to find home directory.".red());
                process::exit(1);
            },
            |path| path.join(".turbocommit.yaml"),
        );
        let config = match serde_yaml::to_string(self) {
            Ok(config) => config,
            Err(err) => {
                println!("{}", format!("Unable to serialize config: {}", err).red());
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Unable to serialize config",
                ));
            }
        };

        if let Ok(existing_config) = std::fs::read_to_string(&path) {
            if existing_config == config {
                return Ok(());
            }
        }

        std::fs::write(path, config)
    }
    pub fn path() -> std::path::PathBuf {
        home::home_dir().map_or_else(
            || {
                println!("{}", "Unable to find home directory.".red());
                process::exit(1);
            },
            |path| path.join(".turbocommit.yaml"),
        )
    }

    fn validate(&self) -> Result<(), Vec<ValidationError>> {
        let mut errors = Vec::new();
        let default = Self::default();

        // Validate model
        if self.model.0.is_empty() {
            errors.push(ValidationError {
                field: "model".to_string(),
                message: format!("Model cannot be empty (default: {})", default.model.0),
            });
        }

        // Validate API endpoint
        if let Err(_) = Url::parse(&self.api_endpoint) {
            errors.push(ValidationError {
                field: "api_endpoint".to_string(),
                message: format!("Invalid URL format (default: {})", default.api_endpoint),
            });
        }

        // Validate number of choices
        if self.default_number_of_choices < 1 {
            errors.push(ValidationError {
                field: "default_number_of_choices".to_string(),
                message: format!(
                    "Number of choices must be at least 1 (default: {})",
                    default.default_number_of_choices
                ),
            });
        }

        // Validate system message
        if self.system_msg.trim().is_empty() {
            errors.push(ValidationError {
                field: "system_msg".to_string(),
                message: "System message cannot be empty (see default message below)".to_string(),
            });
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    fn create_test_config(content: &str) -> (std::path::PathBuf, tempfile::TempDir) {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join(".turbocommit.yaml");
        fs::write(&file_path, content).unwrap();
        (file_path, dir)
    }

    #[test]
    fn test_default_config_is_valid() {
        let config = Config::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_validate_empty_model() {
        let mut config = Config::default();
        config.model = model::Model(String::new());
        let errors = config.validate().unwrap_err();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].field, "model");
    }

    #[test]
    fn test_validate_invalid_api_endpoint() {
        let mut config = Config::default();
        config.api_endpoint = "not a url".to_string();
        let errors = config.validate().unwrap_err();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].field, "api_endpoint");
    }

    #[test]
    fn test_validate_invalid_number_of_choices() {
        let mut config = Config::default();
        config.default_number_of_choices = 0;
        let errors = config.validate().unwrap_err();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].field, "default_number_of_choices");
    }

    #[test]
    fn test_validate_empty_system_msg() {
        let mut config = Config::default();
        config.system_msg = "".to_string();
        let errors = config.validate().unwrap_err();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].field, "system_msg");
    }

    #[test]
    fn test_validate_multiple_errors() {
        let mut config = Config::default();
        config.model = model::Model(String::new());
        config.system_msg = "".to_string();
        let errors = config.validate().unwrap_err();
        assert_eq!(errors.len(), 2);
    }

    #[test]
    fn test_load_valid_config() {
        let config_content = r#"
model: gpt-5.1
api_endpoint: https://api.openai.com/v1/chat/completions
default_number_of_choices: 3
disable_auto_update_check: true
system_msg: "Test message"
"#;
        let (_file_path, _dir) = create_test_config(config_content);

        // Set the home directory to our temp directory for this test
        std::env::set_var("HOME", _dir.path());

        let config = Config::load();
        assert!(config.is_ok());
        let config = config.unwrap();
        assert!(config.disable_auto_update_check);
    }

    #[test]
    fn test_load_invalid_yaml() {
        let config_content = "invalid: yaml: content: [";
        let (_file_path, _dir) = create_test_config(config_content);

        // Set the home directory to our temp directory for this test
        std::env::set_var("HOME", _dir.path());

        let config = Config::load();
        assert!(
            config.is_err(),
            "Expected config loading to fail with invalid YAML"
        );
    }

    #[test]
    fn test_load_missing_file_creates_default() {
        let _dir = tempdir().unwrap();
        std::env::set_var("HOME", _dir.path());

        // First load should create the file
        let config = Config::load();
        assert!(config.is_ok());

        // Verify the file was created
        let config_path = _dir.path().join(".turbocommit.yaml");
        assert!(config_path.exists());

        // Verify content matches default
        let content = std::fs::read_to_string(config_path).unwrap();
        let loaded_config: Config = serde_yaml::from_str(&content).unwrap();
        assert_eq!(loaded_config.model.0, Config::default().model.0);
        assert_eq!(loaded_config.api_endpoint, Config::default().api_endpoint);
    }

    #[test]
    fn test_validation_error_includes_defaults() {
        let mut config = Config::default();
        config.model = model::Model(String::new());

        let errors = config.validate().unwrap_err();
        let default = Config::default();

        // Find the model error
        let model_error = errors.iter().find(|e| e.field == "model").unwrap();
        assert!(model_error.message.contains(&default.model.0));
    }

    #[test]
    fn test_empty_system_msg_shows_default() {
        let config_content = r#"
model: gpt-5.1
api_endpoint: https://api.openai.com/v1/chat/completions
default_number_of_choices: 3
disable_auto_update_check: false
system_msg: ""
"#;
        let (_file_path, _dir) = create_test_config(config_content);
        std::env::set_var("HOME", _dir.path());

        let error = Config::load().unwrap_err();
        let error_msg = error.to_string();

        // Error should contain the default system message
        assert!(error_msg.contains("Default system message:"));
        assert!(error_msg.contains(&Config::default().system_msg));
    }

    #[test]
    fn test_save_if_changed() {
        let _dir = tempdir().unwrap();
        // Set the home directory to our temp directory for this test
        std::env::set_var("HOME", _dir.path());

        // Create a config with some changes
        let mut config = Config::default();
        config.model = model::Model("gpt-4".to_string());

        // First save should succeed
        assert!(config.save_if_changed().is_ok());

        // Second save with no changes should still be ok
        assert!(config.save_if_changed().is_ok());

        // Verify the file was created with correct content
        let config_path = _dir.path().join(".turbocommit.yaml");
        assert!(config_path.exists());
        let content = std::fs::read_to_string(config_path).unwrap();
        let loaded_config: Config = serde_yaml::from_str(&content).unwrap();
        assert_eq!(loaded_config.model.0, "gpt-4");
    }

    #[test]
    fn test_default_auto_update_check() {
        let config = Config::default();
        assert!(
            !config.disable_auto_update_check,
            "Auto update check should be enabled by default"
        );
    }

    #[test]
    fn test_load_from_path_valid_config() {
        let config_content = r#"
model: gpt-5.1
api_endpoint: https://api.openai.com/v1/chat/completions
default_number_of_choices: 3
disable_auto_update_check: true
system_msg: "Test message"
"#;
        let (file_path, _dir) = create_test_config(config_content);

        let config = Config::load_from_path(&file_path);
        assert!(config.is_ok());
        let config = config.unwrap();
        assert_eq!(config.model.0, "gpt-5.1");
        assert!(config.disable_auto_update_check);
        assert_eq!(config.system_msg, "Test message");
    }

    #[test]
    fn test_load_from_path_invalid_yaml() {
        let config_content = "invalid: yaml: content: [";
        let (file_path, _dir) = create_test_config(config_content);

        let config = Config::load_from_path(&file_path);
        assert!(config.is_err());
        assert!(config
            .unwrap_err()
            .to_string()
            .contains("Configuration file parsing error"));
    }

    #[test]
    fn test_load_from_path_nonexistent_file() {
        let dir = tempdir().unwrap();
        let nonexistent_path = dir.path().join("nonexistent.yaml");

        let config = Config::load_from_path(&nonexistent_path);
        assert!(config.is_err());
    }

    #[test]
    fn test_load_from_path_invalid_config() {
        let config_content = r#"
model: ""  # Empty model is invalid
api_endpoint: not-a-url
default_number_of_choices: 3
disable_auto_update_check: false
system_msg: "Test message"
"#;
        let (file_path, _dir) = create_test_config(config_content);

        let config = Config::load_from_path(&file_path);
        assert!(config.is_err());
        let err = config.unwrap_err().to_string();
        assert!(err.contains("model")); // Should mention empty model error
        assert!(err.contains("api_endpoint")); // Should mention invalid URL error
    }

    #[test]
    fn test_load_from_path_empty_system_msg() {
        let config_content = r#"
model: "gpt-5.1"
api_endpoint: "https://api.openai.com/v1/chat/completions"
default_number_of_choices: 3
disable_auto_update_check: false
system_msg: ""
"#;
        let (file_path, _dir) = create_test_config(config_content);

        let config = Config::load_from_path(&file_path);
        assert!(config.is_err());
        let err = config.unwrap_err().to_string();
        assert!(err.contains("system_msg")); // Should mention system message error
        assert!(err.contains("Default system message:")); // Should show default message
    }
}