Skip to content

Commit

Permalink
[Code] support '/' in getCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedragon committed Apr 10, 2019
1 parent 5f2cc01 commit 820b8d3
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions x-pack/plugins/code/server/git_operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,16 @@ export class GitOperations {
if (revision.toUpperCase() === 'HEAD') {
return await repo.getHeadCommit();
}
try {
return await repo.getBranchCommit(revision);
} catch (e) {
if (e.errno === Error.CODE.ENOTFOUND) {
return checkExists(
() => this.findCommit(repo, revision),
`revision or branch ${revision} not found in ${repo.path()}`
);
} else {
throw e;
}
// branches and tags
const refs = [`refs/remotes/origin/${revision}`, `refs/tags/${revision}`];
const commit = await this.findCommitByRefs(repo, refs);
if (commit === null) {
return (await checkExists(
() => this.findCommit(repo, revision),
`revision or branch ${revision} not found in ${repo.path()}`
)) as Commit;
}
return commit;
}

public async blame(uri: RepositoryUri, revision: string, path: string): Promise<GitBlame[]> {
Expand Down Expand Up @@ -157,6 +155,7 @@ export class GitOperations {
const commit = await this.getCommit(repo, revision);
const tree = await commit.getTree();
let count = 0;

async function walk(t: Tree) {
for (const e of t.entries()) {
if (e.isFile() && e.filemode() !== TreeEntry.FILEMODE.LINK) {
Expand All @@ -169,6 +168,7 @@ export class GitOperations {
}
}
}

await walk(tree);
return count;
}
Expand All @@ -180,6 +180,7 @@ export class GitOperations {
const repo = await this.openRepo(uri);
const commit = await this.getCommit(repo, revision);
const tree = await commit.getTree();

async function* walk(t: Tree): AsyncIterableIterator<FileTree> {
for (const e of t.entries()) {
if (e.isFile() && e.filemode() !== TreeEntry.FILEMODE.LINK) {
Expand All @@ -192,6 +193,7 @@ export class GitOperations {
}
}
}

return await walk(tree);
}

Expand Down Expand Up @@ -355,6 +357,7 @@ export class GitOperations {
const entry = await commit.getEntry(path);
return (await entry.getBlob()).content().toString('utf8');
}

private async walkTree(
fileTree: FileTree,
tree: Tree,
Expand Down Expand Up @@ -417,18 +420,37 @@ export class GitOperations {
return fileTree;
}

private async findCommit(repo: Repository, revision: string): Promise<Commit> {
const obj = await Object.lookupPrefix(
repo,
Oid.fromString(revision),
revision.length,
Object.TYPE.COMMIT
);
if (obj) {
return repo.getCommit(obj.id());
private async findCommit(repo: Repository, revision: string): Promise<Commit | null> {
try {
const obj = await Object.lookupPrefix(
repo,
Oid.fromString(revision),
revision.length,
Object.TYPE.COMMIT
);
if (obj) {
return repo.getCommit(obj.id());
}
return null;
} catch (e) {
return null;
}
}

private async findCommitByRefs(repo: Repository, refs: string[]): Promise<Commit | null> {
if (refs.length === 0) {
return null;
}
const [ref, ...rest] = refs;
try {
return await repo.getReferenceCommit(ref);
} catch (e) {
if (e.errno === Error.CODE.ENOTFOUND) {
return await this.findCommitByRefs(repo, rest);
} else {
throw e;
}
}
// @ts-ignore
return null;
}
}

Expand Down

0 comments on commit 820b8d3

Please sign in to comment.