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

feat: separate "git export" module #181

Merged
merged 2 commits into from
May 11, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/fossil.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'npm'
node-version: 18
- name: Install dependencies
run: npm ci
- name: Run ESLint
Expand All @@ -48,8 +49,7 @@ jobs:
run: npm run grammar-test

- name: Package
if: startsWith(github.ref, 'refs/tags/v')
run: rm -rf out && npm run package -- --allow-star-activation
run: rm -rf out && npm run package

- name: Release
uses: softprops/action-gh-release@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ resources/icons/*-dark
resources/icons/*-light
/coverage
*.js
!esbuild.config.js
*.pdf
*.gv
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.github/
.mocharc.js
esbuild.config.js
.nycrc
.vscode
**/.gitignore
Expand Down
37 changes: 37 additions & 0 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const esbuild = require('esbuild');
const minify = process.argv.includes('--minify');
const sourcemap = process.argv.includes('--sourcemap');

function buildConfig(entryPoint, outfile) {
return {
minify,
entryPoints: [entryPoint],
bundle: true,
platform: 'node',
sourcemap,
target: 'node18',
format: 'cjs',
external: ['vscode', './gitExport', './praise'],
...(outfile ? {outfile} : {}),
}
}

async function main() {
const results = await Promise.all([
esbuild.build(
{...buildConfig('./src/main.ts', 'out/main.js')}
),
esbuild.build(
{...buildConfig('./src/praise.ts', 'out/praise.js'), external: ['vscode']}
),
esbuild.build(
{...buildConfig('./src/gitExport.ts', 'out/gitExport.js')}
),
esbuild.build(
{...buildConfig('./media/preview.ts', 'media/preview.js'), platform:'browser', format: 'iife'}
),
])
console.log('fossil extension js files are ready')
}

main()
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1119,19 +1119,18 @@
]
},
"scripts": {
"vscode:prepublish": "npm run esbuild-base -- --minify && npm run esbuild-preview -- --minify",
"compile": "tsc -p ./ && npm run esbuild-preview",
"vscode:prepublish": "npm run bundle -- --minify",
"bundle": "node ./esbuild.config.js",
"esbuild-preview": "esbuild ./media/preview.ts --bundle --outfile=media/preview.js --platform=browser --format=iife",
"package": "vsce package",
"compile": "tsc -p ./ && npm run esbuild-preview",
"package": "vsce package --allow-star-activation",
"deploy": "vsce publish",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js",
"coverage": "node ./node_modules/c8/bin/c8.js --reporter lcov --check-coverage npm run test",
"coverage-ci": "node ./node_modules/c8/bin/c8.js --reporter json-summary --check-coverage npm run test && node ./out/test/summary_as_markdown.js coverage/coverage-summary.json",
"lint": "eslint ./src --ext .ts src media",
"lint:fix": "eslint --fix --ext .ts src media",
"esbuild-base": "esbuild ./src/main.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"esbuild": "npm run esbuild-base -- --sourcemap",
"grammar-test": "vscode-tmgrammar-test ./pikchr/test/*.test.pikchr"
},
"dependencies": {
Expand Down
21 changes: 13 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
ResourceStatus,
ResourcePath,
} from './openedRepository';
import { Model } from './model';
import type { Model } from './model';
import {
FossilResource,
CommitOptions,
Expand All @@ -51,8 +51,7 @@ import { FossilPreviewManager } from './preview';
import { FossilExecutable, FossilCWD } from './fossilExecutable';

import { localize } from './main';
import { PraiseAnnotator } from './praise';
import { Credentials, exportGit, inputExportOptions } from './gitExport';
import type { Credentials } from './gitExport';

type CommandKey =
| 'add'
Expand Down Expand Up @@ -178,7 +177,7 @@ function command(repository?: 1) {
export class CommandCenter {
private readonly disposables: Disposable[];
private readonly previewManager: FossilPreviewManager;
private readonly credentials = new Credentials();
private credentials: Credentials | undefined;

constructor(
private readonly executable: FossilExecutable,
Expand Down Expand Up @@ -1411,7 +1410,8 @@ export class CommandCenter {
if (!editor) {
return;
}
if (PraiseAnnotator.tryDelete(editor)) {
const praise = await require('./praise');
if (praise.PraiseAnnotator.tryDelete(editor)) {
return;
}
const uri = editor.document.uri;
Expand All @@ -1420,18 +1420,23 @@ export class CommandCenter {
return;
}
const praises = await repository.praise(uri.fsPath);
await PraiseAnnotator.create(repository, editor, praises);
await praise.PraiseAnnotator.create(repository, editor, praises);
}

@command(Inline.Repository)
async gitPublish(repository: Repository): Promise<void> {
const options = await inputExportOptions(
const gitExport = await require('./gitExport');

if (!this.credentials) {
this.credentials = new gitExport.Credentials();
}
const options = await gitExport.inputExportOptions(
this.credentials,
repository,
this.disposables
);
if (options) {
await exportGit(options, repository);
await gitExport.exportGit(options, repository);
}
}
@command(Inline.Repository)
Expand Down
40 changes: 23 additions & 17 deletions src/gitExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ type GitMirrorPath = Distinct<
'path that goes as MIRROR option to `fossil export`'
>;
type AutoPushURIUnsafe = Distinct<
Uri,
string,
'URL that goes as --autopush option to `fossil export`'
>;
type AutoPushURISafe = Distinct<
Uri,
string,
'like `AutoPushURIUnsafe`, but without tokens`'
>;
type GitExportOptions = {
Expand Down Expand Up @@ -140,19 +140,23 @@ export async function inputExportOptions(

if (where === toUrl) {
// ask URL
const urlStr = await window.showInputBox({
const urlStr = (await window.showInputBox({
prompt: 'The URL of an empty repository',
ignoreFocusOut: true,
});
})) as AutoPushURIUnsafe;
if (urlStr) {
const url = Uri.parse(urlStr);
const safeAuthority = url.authority.substring(
url.authority.indexOf('@')
);
const safeUrl = url
.with({
authority: url.authority.substring(
url.authority.indexOf('@')
),
})
.toString() as AutoPushURISafe;
return {
path: exportPath,
url: url.with({ authority: safeAuthority }) as AutoPushURISafe,
urlUnsafe: url as AutoPushURIUnsafe,
url: safeUrl,
urlUnsafe: urlStr,
};
}
return;
Expand Down Expand Up @@ -282,7 +286,7 @@ export async function inputExportOptions(
label: '$(github) Use https url with token',
};
const withGit = {
label: '$(key) Use git url without token',
label: '$(key) Use ssh url without token',
};
const auth = await window.showQuickPick([withToken, withGit]);
if (!auth) {
Expand All @@ -293,14 +297,16 @@ export async function inputExportOptions(
let urlUnsafe: AutoPushURIUnsafe;
if (auth === withToken) {
// add token to url
url = Uri.parse(response.data.html_url) as AutoPushURISafe;
urlUnsafe = url.with({
authority: `${session.account.label}:${session.accessToken}@${url.authority}`,
}) as AutoPushURIUnsafe;
url = response.data.html_url as AutoPushURISafe;
const urlParsed = Uri.parse(url);
urlUnsafe = urlParsed
.with({
authority: `${session.account.label}:${session.accessToken}@${urlParsed.authority}`,
})
.toString() as AutoPushURIUnsafe;
} else {
urlUnsafe = (url = Uri.parse(
response.data.git_url
) as AutoPushURISafe) as unknown as AutoPushURIUnsafe;
urlUnsafe = (url = response.data
.ssh_url as AutoPushURISafe) as unknown as AutoPushURIUnsafe;
}
return { path: exportPath, url, urlUnsafe };
}
Expand Down
4 changes: 2 additions & 2 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ import {
AutoIncomingOutgoing,
} from './autoinout';
import * as interaction from './interaction';
import { InteractionAPI, NewBranchOptions } from './interaction';
import type { InteractionAPI, NewBranchOptions } from './interaction';
import { FossilUriParams, toFossilUri } from './uri';

import { localize } from './main';
import { ExecFailure, ExecResult } from './fossilExecutable';
import type { ExecFailure, ExecResult } from './fossilExecutable';
const iconsRootPath = path.join(path.dirname(__dirname), 'resources', 'icons');

type AvailableIcons =
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/gitExportSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class GitExportTestHelper {
);
assert.equal(
items[1].label,
'$(key) Use git url without token'
'$(key) Use ssh url without token'
);
return Promise.resolve(
(() => {
Expand Down Expand Up @@ -328,7 +328,7 @@ class GitExportTestHelper {
) => ({
data: {
html_url: `https://examplegit.com/theuser/${params.name}`,
git_url: `git:examplegit.com/theuser/${params.name}.git`,
ssh_url: `git@github.com:theuser/${params.name}.git`,
},
})
);
Expand Down Expand Up @@ -499,7 +499,7 @@ export function GitExportSuite(this: Suite): void {
sinon.assert.calledOnceWithExactly(
term.fakeTerminal.sendText,
' fossil git export /tmp/gitExport/spn --mainbranch main ' +
'--autopush git:examplegit.com/theuser/spn.git'
'--autopush git@github.com:theuser/spn.git'
);
});

Expand Down
4 changes: 2 additions & 2 deletions src/test/suite/timelineSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as vscode from 'vscode';
import { window } from 'vscode';
import * as assert from 'assert/strict';
import { FossilUriParams, toFossilUri } from '../../uri';
import { FossilCWD } from '../../fossilExecutable';
import type { FossilCWD } from '../../fossilExecutable';
import { add, cleanupFossil, getExecutable, getRepository } from './common';
import { Suite, before } from 'mocha';
import * as sinon from 'sinon';
import { FossilCheckin } from '../../openedRepository';
import type { FossilCheckin } from '../../openedRepository';

// separate function because hash size is different
const uriMatch = (uri: vscode.Uri, checkin: FossilCheckin) =>
Expand Down