Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code] support '/' in getCommit #34774

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to confirm that it's intended to completely ignore refs/head/xxx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the local repository should be read-only, so there is no local branch at all.

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