Skip to content

Commit 7b1a6c8

Browse files
committed
Git: Fixed error when staging in empty repository
Trying to stage a commit in an empty repository (i.e. no prior commits) results in a git fatal error. This is because in an empty repo, HEAD doesn't point to anything causing `git ls-tree -l HEAD ...` to fail. To fix this, send `treeish` as `'HEAD'` to `getObjectDetails()` only if there is at least one commit in the repo. Else, send an empty string causing `getObjectDetails()` to use `lsFiles` instead of `lsTree`. Fixes microsoft#82026
1 parent 82ca6ba commit 7b1a6c8

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

extensions/git/src/git.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,12 @@ export class Repository {
762762
async log(options?: LogOptions): Promise<Commit[]> {
763763
const maxEntries = options && typeof options.maxEntries === 'number' && options.maxEntries > 0 ? options.maxEntries : 32;
764764
const args = ['log', '-' + maxEntries, `--pretty=format:${COMMIT_FORMAT}%x00%x00`];
765-
const gitResult = await this.run(args);
766-
if (gitResult.exitCode) {
767-
// An empty repo.
765+
766+
let gitResult: IExecutionResult<string>;
767+
try {
768+
gitResult = await this.run(args);
769+
} catch (err) {
770+
// An empty repo
768771
return [];
769772
}
770773

@@ -1164,9 +1167,15 @@ export class Repository {
11641167

11651168
let mode: string;
11661169
let add: string = '';
1170+
let treeish: string = '';
1171+
const commits = await this.log({ maxEntries: 1 });
1172+
1173+
if (commits.length > 0) {
1174+
treeish = 'HEAD';
1175+
}
11671176

11681177
try {
1169-
const details = await this.getObjectDetails('HEAD', path);
1178+
const details = await this.getObjectDetails(treeish, path);
11701179
mode = details.mode;
11711180
} catch (err) {
11721181
if (err.gitErrorCode !== GitErrorCodes.UnknownPath) {

0 commit comments

Comments
 (0)