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

IOTIDE-2201 Improve parsing of log output, and fix e-mail address #31

Merged
merged 1 commit into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions packages/hg/src/browser/hg-scm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ConfirmDialog } from '@theia/core/lib/browser/dialogs';
import { EditorOpenerOptions, EditorManager } from '@theia/editor/lib/browser/editor-manager';
import { FileSystem } from '@theia/filesystem/lib/common';
import { WorkspaceCommands } from '@theia/workspace/lib/browser';
import { Repository, Hg, CommitWithChanges, HgFileChange, HgFileStatus, WorkingDirectoryStatus } from '../common';
import { Repository, Hg, CommitWithChanges, HgFileChange, HgFileStatus, WorkingDirectoryStatus, CommitIdentity } from '../common';
import { HG_RESOURCE_SCHEME } from './hg-resource';
import { HgErrorHandler } from './hg-error-handler';
import { EditorWidget } from '@theia/editor/lib/browser';
Expand Down Expand Up @@ -346,13 +346,15 @@ export class HgScmProvider implements ScmProvider {
toRevision: hgCommit.sha
};

const { name: authorName, email: authorEmail } = this.extractNameAndEmail(hgCommit.author);

const scmCommit: HgScmCommit = {
id: hgCommit.sha,
commitDetailUri: this.toCommitDetailUri(hgCommit.sha),
summary: hgCommit.summary,
messageBody: hgCommit.body,
authorName: hgCommit.author.name,
authorEmail: hgCommit.author.email,
authorName,
authorEmail,
authorTimestamp: new Date(hgCommit.author.timestamp * 1000).toISOString(),
authorDateRelative: hgCommit.authorDateRelative,
scmProvider: this,
Expand All @@ -365,8 +367,8 @@ export class HgScmProvider implements ScmProvider {
sha: hgCommit.sha,
summary: hgCommit.summary,
messageBody: hgCommit.body,
authorName: hgCommit.author.name,
authorEmail: hgCommit.author.email,
authorName: this.authorName,
authorEmail: this.authorEmail,
authorTimestamp: new Date(hgCommit.author.timestamp * 1000).toISOString(),
authorDateRelative: hgCommit.authorDateRelative,
};
Expand All @@ -375,6 +377,13 @@ export class HgScmProvider implements ScmProvider {
return scmCommit;
}

private extractNameAndEmail(identity: CommitIdentity): { name: string, email: string } {
const match = identity.nameAndEmail.match(/^(.*) <(.*)>$/);
const name = match ? match[1] : identity.nameAndEmail;
const email = match ? match[2] : '';
return { name, email };
}

public relativePath(uri: string): string {
const parsedUri = new URI(uri);
const hgRepo = { localUri: this.rootUri };
Expand Down
9 changes: 2 additions & 7 deletions packages/hg/src/common/hg-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,9 @@ export interface CommitWithChanges extends Commit {
export interface CommitIdentity {

/**
* The name for the commit.
* The name and email address for the user who did the commit.
*/
readonly name: string;

/**
* The email address for the user who did the commit.
*/
readonly email: string;
readonly nameAndEmail: string;

/**
* The time of the commit (seconds since 1970).
Expand Down
44 changes: 13 additions & 31 deletions packages/hg/src/node/hg-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ export class HgImpl implements Hg {
const revisionId = output[i++].trim();
i += 1;
const author = {
name: 'unknown',
email: 'unknown',
nameAndEmail: 'unknown <unknown>',
timestamp: 0
} as CommitIdentity;
const tip = {
Expand Down Expand Up @@ -479,12 +478,6 @@ export class HgImpl implements Hg {
};
}

private extractFiles(files: string): string[] {
// TODO investigate what Mercurial gives us back if there is a space in the file name.
// This code probably won't cope.
return files === '' ? [] : files.split(' ');
}

async log(repository: Repository, options: Hg.Options.Log = {}): Promise<CommitWithChanges[]> {
const repo = await this.getHgRepo(repository);

Expand All @@ -508,24 +501,13 @@ export class HgImpl implements Hg {
const outputChunks = await repo.runCommand(args);

return outputChunks.map(chunk => {
/*
* Note that 'desc' is not output in json format. This is because the 'desc'
* field may contain characters that would need to be escaped to make valid parsable
* json, yet there is no way to get Mercurial to do this escaping, nor is there any easy
* way for us to modify the text to do the escaping. Therefore the template puts
* the 'desc' field at the end with a separator that is not likely to appear in
* the data.
*/
const lines = chunk.split('end-json-start-desc');
const commitLine = JSON.parse(lines[0]);
const commitLine = JSON.parse(chunk);

const timestamp: number = commitLine.timestamp.split(' ')[0];

const addedFiles = this.extractFiles(commitLine.added);
const modifiedFiles = this.extractFiles(commitLine.modified);
const deletedFiles = this.extractFiles(commitLine.deleted);

const summary: string = lines[1];
const addedFiles = commitLine.added;
const modifiedFiles = commitLine.modified;
const deletedFiles = commitLine.deleted;
const summary = commitLine.desc;

const fileChanges: HgFileChange[] = [];
for (const filename of addedFiles) {
Expand All @@ -539,10 +521,10 @@ export class HgImpl implements Hg {
}

const sha = commitLine.node;
const name = commitLine.author;
const nameAndEmail = commitLine.author;
const authorDateRelative = this.getAuthorDateRelative(timestamp);

const author: CommitIdentity = { name, email: 'unknown', timestamp };
const author: CommitIdentity = { nameAndEmail, timestamp };
return { sha, summary, author, authorDateRelative, fileChanges };
});
}
Expand Down Expand Up @@ -679,12 +661,12 @@ export class HgImpl implements Hg {
*/
const logTemplate: string =
'\\{ "node": "{node}",\\n' +
' "author": "{author}",\\n' +
' "author": {json(author)},\\n' +
' "timestamp": "{date|hgdate}",\\n' +
' "added": "{file_adds}",\\n' +
' "modified": "{file_mods}",\\n' +
' "deleted": "{file_dels}"\n}' +
'end-json-start-desc{desc}';
' "added": {json(file_adds)},\\n' +
' "modified": {json(file_mods)},\\n' +
' "deleted": {json(file_dels)},\\n' +
' "desc": {json(desc)}\n}';

Choose a reason for hiding this comment

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

A question about this format. I've seen that @mcgordonite wrote that he created a different template here. this looks slightly different. Is it by design?

Choose a reason for hiding this comment

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

I think either are fine: this version has the advantage of being trivial to parse with JSON.parse, but the one in your link didn't require as much escaping in the template.


/**
* This template outputs data in JSON format so the output from the 'parent' command is easily parsed.
Expand Down