Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit 8badb3f

Browse files
committed
feat(main.ts): Add support for storing and loading commit styles
- Added new functions to store and retrieve commit styles * Styles can be stored per-author or as a default * Styles are stored in the config directory - Updated main function to use stored styles * Learned styles are stored and used by default * Explicit format can still be specified via flags - Removed force-setting Conventional format in learning mode BREAKING CHANGE: The `--reset-format` flag has been removed. Use `--format` to explicitly set the commit format.
1 parent 1969c20 commit 8badb3f

1 file changed

Lines changed: 63 additions & 34 deletions

File tree

main.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ async function analyzeCommitStyle(commits: string, apiKey: string): Promise<stri
204204
205205
Format as plain text with no markdown or bullets. Number each rule.
206206
207-
EXAMPLES:
208-
${CONVENTIONAL_FORMAT}
209-
210207
Analyze these commits:
211208
212209
${commits}`
@@ -410,21 +407,30 @@ async function listAuthors(): Promise<void> {
410407
console.log('└────────┴──────────────────────────────────────────────────────────────┘\n');
411408
}
412409

413-
// Add new functions to handle style storage
410+
// Add new functions for format management
414411
async function storeCommitStyle(style: string, author?: string): Promise<void> {
415412
const configDir = await getConfigDir();
416-
const fileName = author ? `style-${author.replace(/[^a-zA-Z0-9]/g, '-')}` : 'style-repo';
417-
const stylePath = join(configDir, fileName);
413+
const stylePath = author
414+
? join(configDir, `style-${author}`)
415+
: join(configDir, "default-style");
418416
await Deno.writeTextFile(stylePath, style);
419417
}
420418

421419
async function getStoredCommitStyle(author?: string): Promise<string | null> {
422420
try {
423421
const configDir = await getConfigDir();
424-
const fileName = author ? `style-${author.replace(/[^a-zA-Z0-9]/g, '-')}` : 'style-repo';
425-
const stylePath = join(configDir, fileName);
426-
const style = await Deno.readTextFile(stylePath);
427-
return style.trim();
422+
// Try author-specific style first
423+
if (author) {
424+
try {
425+
const authorStylePath = join(configDir, `style-${author}`);
426+
return await Deno.readTextFile(authorStylePath);
427+
} catch {
428+
// Fall back to default style
429+
}
430+
}
431+
// Try default style
432+
const defaultStylePath = join(configDir, "default-style");
433+
return await Deno.readTextFile(defaultStylePath);
428434
} catch {
429435
return null;
430436
}
@@ -452,9 +458,8 @@ async function getDefaultFormat(): Promise<CommitFormat | null> {
452458
// Update main function to use stored styles
453459
async function main(): Promise<void> {
454460
const flags = parse(Deno.args, {
455-
string: ["author", "style", "format"],
456-
boolean: ["help", "learn", "list-authors", "reset-format"],
457-
default: { help: false, learn: false, "list-authors": false },
461+
string: ["author", "format"],
462+
boolean: ["help", "learn", "list-authors"],
458463
alias: { h: "help" },
459464
});
460465

@@ -537,6 +542,49 @@ For more information, visit: https://github.com/sidedwards/auto-commit
537542
}
538543
}
539544

545+
// Handle format selection
546+
if (flags.learn) {
547+
// Learning mode - analyze and store the style
548+
const commits = await getCommitHistory(flags.author);
549+
const styleGuide = await analyzeCommitStyle(commits, apiKey);
550+
551+
// Store as both author-specific (if specified) and default style
552+
if (flags.author) {
553+
await storeCommitStyle(styleGuide, flags.author);
554+
}
555+
await storeCommitStyle(styleGuide); // Store as default
556+
557+
// Remove this line - don't force Conventional format
558+
// await storeDefaultFormat(CommitFormat.CONVENTIONAL);
559+
560+
console.log(`\nLearned and saved commit style${flags.author ? ` for ${flags.author}` : ''}`);
561+
} else if (flags.format) {
562+
// Explicit format specified
563+
const formatInput = flags.format.toLowerCase();
564+
let selectedFormat = CommitFormat.CONVENTIONAL;
565+
566+
// Handle common typos and variations
567+
if (formatInput.includes('kern')) {
568+
selectedFormat = CommitFormat.KERNEL;
569+
} else if (formatInput.includes('sem')) {
570+
selectedFormat = CommitFormat.SEMANTIC;
571+
} else if (formatInput.includes('ang')) {
572+
selectedFormat = CommitFormat.ANGULAR;
573+
} else if (formatInput.includes('con')) {
574+
selectedFormat = CommitFormat.CONVENTIONAL;
575+
}
576+
577+
const template =
578+
selectedFormat === CommitFormat.KERNEL ? KERNEL_FORMAT :
579+
selectedFormat === CommitFormat.SEMANTIC ? SEMANTIC_FORMAT :
580+
selectedFormat === CommitFormat.ANGULAR ? ANGULAR_FORMAT :
581+
CONVENTIONAL_FORMAT;
582+
583+
await storeCommitStyle(template);
584+
// Store the format as default
585+
await storeDefaultFormat(selectedFormat);
586+
}
587+
540588
// Use format flag if provided
541589
let selectedFormat = CommitFormat.CONVENTIONAL; // default
542590
if (typeof flags.format === 'string') { // Type check the flag
@@ -557,13 +605,6 @@ For more information, visit: https://github.com/sidedwards/auto-commit
557605

558606
console.log(`Using commit format: ${selectedFormat}`);
559607

560-
// Set the appropriate format template
561-
let commitPrompt = flags.learn ? CONVENTIONAL_FORMAT :
562-
selectedFormat === CommitFormat.KERNEL ? KERNEL_FORMAT :
563-
selectedFormat === CommitFormat.SEMANTIC ? SEMANTIC_FORMAT :
564-
selectedFormat === CommitFormat.ANGULAR ? ANGULAR_FORMAT :
565-
CONVENTIONAL_FORMAT;
566-
567608
if (flags.learn) {
568609
try {
569610
const commits = await getCommitHistory(flags.author);
@@ -572,12 +613,6 @@ For more information, visit: https://github.com/sidedwards/auto-commit
572613
// Store the learned style
573614
await storeCommitStyle(styleGuide, flags.author);
574615

575-
commitPrompt = `You are a Git Commit Message Generator that follows the conventions below:
576-
577-
${styleGuide}
578-
579-
Generate commit messages directly without explanations or markdown formatting.`;
580-
581616
console.log("\nLearned and saved commit style from repository history.");
582617
} catch (error) {
583618
console.error("Failed to learn commit style:", error);
@@ -587,12 +622,6 @@ Generate commit messages directly without explanations or markdown formatting.`;
587622
// Try to load previously learned style
588623
const storedStyle = await getStoredCommitStyle(flags.author);
589624
if (storedStyle) {
590-
commitPrompt = `You are a Git Commit Message Generator that follows the conventions below:
591-
592-
${storedStyle}
593-
594-
Generate commit messages directly without explanations or markdown formatting.`;
595-
596625
console.log("\nUsing previously learned commit style.");
597626
}
598627
}
@@ -659,7 +688,7 @@ Generate commit messages directly without explanations or markdown formatting.`;
659688
// Create the system prompt
660689
const systemPrompt = `You are a Git Commit Message Generator. Generate ONLY a commit message following this commit format:
661690
662-
${flags.learn ? commitPrompt : formatTemplate}
691+
${flags.learn || flags.author ? await getStoredCommitStyle(flags.author) : formatTemplate}
663692
664693
IMPORTANT:
665694
1. Base your message on ALL changes in the diff
@@ -671,7 +700,7 @@ IMPORTANT:
671700
7. Never include issue numbers unless they appear in the diff
672701
8. Do not include any format templates or placeholders
673702
9. Never output the response format template itself
674-
10. Only include ONE header line (<type>(<scope>): <subject>)
703+
10. Only include ONE header line
675704
11. Never duplicate any lines, especially the header
676705
12. Sort changes by priority and logical groups
677706
13. Never include preamble or explanation

0 commit comments

Comments
 (0)