Skip to content

Commit 1841047

Browse files
committed
Merge branch 'main' of github.com:misaelabanto/commita
2 parents a684ba9 + e342df4 commit 1841047

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/cli/commit-handler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class CommitHandler {
2929
async execute(options: CommitOptions): Promise<void> {
3030
console.log(chalk.blue('🤖 Commita - AI-powered auto-commit\n'));
3131

32+
await this.gitService.init();
33+
3234
const patternMatcher = new PatternMatcher(
3335
PatternMatcher.parsePatterns(options.ignore)
3436
);

src/config/config.loader.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { existsSync } from 'fs';
44
import { readFile } from 'fs/promises';
55
import { join } from 'path';
66
import { homedir } from 'os';
7+
import simpleGit from 'simple-git';
78

89
export class ConfigLoader {
910
async load(configPath?: string): Promise<CommitaConfig> {
@@ -24,7 +25,27 @@ export class ConfigLoader {
2425
}
2526

2627
private async loadFromFile(configPath?: string): Promise<Partial<CommitaConfig>> {
27-
const path = configPath || join(process.cwd(), '.commita');
28+
let path = configPath;
29+
30+
if (!path) {
31+
const cwdPath = join(process.cwd(), '.commita');
32+
if (existsSync(cwdPath)) {
33+
path = cwdPath;
34+
} else {
35+
const gitRoot = await this.findGitRoot();
36+
if (gitRoot) {
37+
const rootPath = join(gitRoot, '.commita');
38+
if (existsSync(rootPath)) {
39+
path = rootPath;
40+
}
41+
}
42+
}
43+
}
44+
45+
// Fallback to default check if we still haven't found it or if it was explicitly provided
46+
if (!path) {
47+
path = join(process.cwd(), '.commita');
48+
}
2849

2950
if (!existsSync(path)) {
3051
return {};
@@ -39,6 +60,16 @@ export class ConfigLoader {
3960
}
4061
}
4162

63+
private async findGitRoot(): Promise<string | null> {
64+
try {
65+
const git = simpleGit();
66+
const root = await git.revparse(['--show-toplevel']);
67+
return root.trim();
68+
} catch {
69+
return null;
70+
}
71+
}
72+
4273
private parseKeyValue(content: string): Partial<CommitaConfig> {
4374
const config: Partial<CommitaConfig> = {};
4475
const lines = content.split('\n');
@@ -118,4 +149,3 @@ export class ConfigLoader {
118149
return config;
119150
}
120151
}
121-

src/git/git.service.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@ export interface FileChange {
1010

1111
export class GitService {
1212
private git: SimpleGit;
13+
private rootDir: string;
1314

1415
constructor(workingDir?: string) {
15-
this.git = simpleGit(workingDir || process.cwd());
16+
this.rootDir = workingDir || process.cwd();
17+
this.git = simpleGit(this.rootDir);
18+
}
19+
20+
async init(): Promise<void> {
21+
try {
22+
const root = await this.git.revparse(['--show-toplevel']);
23+
this.rootDir = root.trim();
24+
this.git = simpleGit(this.rootDir);
25+
} catch (error) {
26+
// Fallback to cwd if not a git repo (will likely fail later but handles init)
27+
}
1628
}
1729

1830
async getStagedChanges(): Promise<FileChange[]> {
@@ -72,7 +84,7 @@ export class GitService {
7284

7385
private async getNewFileDiff(filePath: string): Promise<string> {
7486
try {
75-
const fullPath = join(process.cwd(), filePath);
87+
const fullPath = join(this.rootDir, filePath);
7688
if (!existsSync(fullPath)) {
7789
return '';
7890
}

0 commit comments

Comments
 (0)