Skip to content

Commit 0674002

Browse files
authored
Merge pull request #45 from Hi7cl4w/fix/14-diffy-no-changes-on-repo-with-submodules
fix: Handle git repositories with submodules correctly
2 parents 5bca5d6 + de66bfe commit 0674002

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

src/service/GitService.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,34 +176,57 @@ class GitService {
176176

177177
/**
178178
* Get the diff in the git repository.
179+
* Handles submodules correctly by using simple-git with safe configuration.
179180
* @returns The diff object is being returned.
180181
*/
181182
async getGitDiff(repo: Repository, _cachedInput = true, nameOnly?: boolean) {
182-
// let diff = await repo.diff(cached);
183-
const git = simpleGit(repo.rootUri.fsPath);
183+
// Configure simple-git for proper submodule handling
184+
const git = simpleGit(repo.rootUri.fsPath, {
185+
binary: "git",
186+
maxConcurrentProcesses: 6,
187+
});
188+
184189
let diff: string | null = "";
185190

186191
// Get exclusion patterns from settings
187192
const excludePatterns = WorkspaceService.getInstance().getExcludeFilesFromDiff();
188193

189-
if (!nameOnly) {
190-
diff = await git.diff(["--cached"]).catch((error) => {
191-
this.showErrorMessage("git repository not found");
192-
console.error(error);
194+
try {
195+
// Check if we're in a valid git repository (handles submodules)
196+
const isRepo = await git.checkIsRepo();
197+
if (!isRepo) {
198+
this.showErrorMessage("Not a git repository");
193199
return null;
194-
});
200+
}
195201

196-
// Apply file filtering if diff was successful
197-
if (diff && excludePatterns.length > 0) {
198-
diff = this.filterDiffByExclusions(diff, excludePatterns);
202+
if (!nameOnly) {
203+
diff = await git.diff(["--cached"]);
204+
205+
// Apply file filtering if diff was successful
206+
if (diff && excludePatterns.length > 0) {
207+
diff = this.filterDiffByExclusions(diff, excludePatterns);
208+
}
209+
} else {
210+
diff = await git.diff(["--cached", "--name-status"]);
199211
}
200-
} else {
201-
diff = await git.diff(["--cached", "--name-status"]).catch((error) => {
202-
this.showErrorMessage("git repository not found");
203-
console.error(error);
204-
return null;
205-
});
212+
} catch (error) {
213+
// Handle specific git errors
214+
if (error && typeof error === "object" && "message" in error) {
215+
const errorMessage = (error as Error).message;
216+
if (errorMessage.includes("not a git repository")) {
217+
this.showErrorMessage("Not a git repository");
218+
} else if (errorMessage.includes("submodule")) {
219+
this.showErrorMessage("Submodule error - ensure submodules are initialized");
220+
} else {
221+
this.showErrorMessage("Git operation failed");
222+
}
223+
} else {
224+
this.showErrorMessage("Git operation failed");
225+
}
226+
console.error("Git diff error:", error);
227+
return null;
206228
}
229+
207230
return diff;
208231
}
209232

src/service/VsCodeLlmService.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,15 @@ ${customInstructions ? `\nADDITIONAL INSTRUCTIONS:\n${customInstructions}\n` : "
137137
138138
Return ONLY the commit message, no explanations or surrounding text.`;
139139
}
140+
const response = await this.getFromVsCodeLlm(instructions, code, progress);
140141

141-
try {
142-
const response = await this.getFromVsCodeLlm(instructions, code, progress);
143-
144-
if (response) {
145-
let message = response.trim();
146-
message = message.replace(/^"/gm, "");
147-
message = message.replace(/"$/gm, "");
148-
return message;
149-
}
150-
return null;
151-
} catch (error) {
152-
// Re-throw the error so it can be caught by the calling function
153-
throw error;
142+
if (response) {
143+
let message = response.trim();
144+
message = message.replace(/^"/gm, "");
145+
message = message.replace(/"$/gm, "");
146+
return message;
154147
}
148+
return null;
155149
}
156150

157151
/**
@@ -163,22 +157,15 @@ Return ONLY the commit message, no explanations or surrounding text.`;
163157
async getExplainedChanges(_string1: string, string2: string): Promise<string | null> {
164158
const instructions =
165159
"You are a bot that explains the changes from the result of 'git diff --cached' that user given. commit message should be a multiple lines where first line doesn't exceed '50' characters by following commit message guidelines based on the given git diff changes without mentioning itself";
160+
const response = await this.getFromVsCodeLlm(instructions, string2);
166161

167-
// Use string2 as the code/diff, string1 is typically instructions but we use our own
168-
try {
169-
const response = await this.getFromVsCodeLlm(instructions, string2);
170-
171-
if (response) {
172-
let message = response.trim();
173-
message = message.replace(/^"/gm, "");
174-
message = message.replace(/"$/gm, "");
175-
return message;
176-
}
177-
return null;
178-
} catch (error) {
179-
// Re-throw the error so it can be caught by the calling function
180-
throw error;
162+
if (response) {
163+
let message = response.trim();
164+
message = message.replace(/^"/gm, "");
165+
message = message.replace(/"$/gm, "");
166+
return message;
181167
}
168+
return null;
182169
}
183170

184171
/**

0 commit comments

Comments
 (0)