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

fix: diff files now have unique paths #694

Merged
merged 13 commits into from
Oct 28, 2019
6 changes: 3 additions & 3 deletions src/historyView/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { exists, lstat } from "../fs";
import { configuration } from "../helpers/configuration";
import { IRemoteRepository } from "../remoteRepository";
import { SvnRI } from "../svnRI";
import { dumpSvnFile } from "../tempFiles";
import { createTempSvnRevisionFile } from "../tempFiles";

export enum LogTreeItemKind {
Repo = 1,
Expand Down Expand Up @@ -263,7 +263,7 @@ async function downloadFile(
window.showErrorMessage("Failed to open path");
throw e;
}
return dumpSvnFile(arg, revision, out);
return createTempSvnRevisionFile(arg, revision, out);
}

export async function openDiff(
Expand Down Expand Up @@ -293,7 +293,7 @@ export async function openFileRemote(
window.showErrorMessage("Failed to open path");
return;
}
const localUri = await dumpSvnFile(arg, against, out);
const localUri = await createTempSvnRevisionFile(arg, against, out);
const opts: TextDocumentShowOptions = {
preview: true
};
Expand Down
20 changes: 15 additions & 5 deletions src/tempFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ import * as os from "os";
import * as path from "path";
import { Uri } from "vscode";
import { exists, mkdir, writeFile } from "./fs";
import * as crypto from "crypto";

export const tempdir = path.join(os.tmpdir(), "vscode-svn");

export async function dumpSvnFile(
snvUri: Uri,
export async function createTempSvnRevisionFile(
svnUri: Uri,
revision: string,
payload: string
): Promise<Uri> {
if (!await exists(tempdir)) {
if (!(await exists(tempdir))) {
await mkdir(tempdir);
}
const fname = `r${revision}_${path.basename(snvUri.fsPath)}`;
const fpath = path.join(tempdir, fname);

const fname = `r${revision}_${path.basename(svnUri.fsPath)}`;
const hash = crypto.createHash("md5");
const data = hash.update(svnUri.path);
const filePathHash = data.digest("hex");

if (!(await exists(path.join(tempdir, filePathHash)))) {
await mkdir(path.join(tempdir, filePathHash));
}

const fpath = path.join(tempdir, filePathHash, fname);
await writeFile(fpath, payload);
return Uri.file(fpath);
}
54 changes: 54 additions & 0 deletions src/test/tempFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as assert from "assert";
import { Uri } from "vscode";
import { exists } from "../fs/exists";
import { readFile } from "../fs/read_file";
import * as os from "os";
import { join } from "path";
import { createTempSvnRevisionFile } from "../tempFiles";

/*
On windows node will return the temp fir in the C drive as 'C:\'
where url.fspath will start with 'c:\'
*/
let osTmpDir = os.tmpdir();
osTmpDir = osTmpDir[0].toLowerCase() + osTmpDir.substr(1);
const tempRevisionPath = join(
osTmpDir,
"vscode-svn",
"1181ae15a77d83ac0b077051dfed21ed",
"r30_test.js"
);

suite("Test temp file creation", () => {
test("Temp files matches expected", async () => {
const svnUri = Uri.parse("http://example.com/svn/test/trunk/test.js");

const revisionUri = await createTempSvnRevisionFile(
svnUri,
"30",
"test content"
);

assert.equal(revisionUri.fsPath, tempRevisionPath);
});

test("Temp file is created", async () => {
const svnUri = Uri.parse("http://example.com/svn/test/trunk/test.js");

await createTempSvnRevisionFile(svnUri, "30", "test content");

assert.ok(await exists(tempRevisionPath));
});

test("Temp contents are correct", async () => {
const svnUri = Uri.parse("http://example.com/svn/test/trunk/test.js");

const revisionUri = await createTempSvnRevisionFile(
svnUri,
"30",
"test content"
);

assert.equal(await readFile(revisionUri.fsPath), "test content");
});
});