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 remote url in multi repo workspace #34

Merged
merged 8 commits into from
Nov 20, 2022
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"typescript": "4.8.3"
},
"dependencies": {
"normalize-git-url": "^3.0.2"
"normalize-git-url": "^3.0.2",
"upath": "^2.0.1"
}
}
53 changes: 29 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as path from 'node:path';
import * as vscode from 'vscode';
import { toUnix } from 'upath';

import { GitExtension } from './git';
import { makeHttpsUrl } from './makeHttpsUrl';
Expand Down Expand Up @@ -38,14 +40,23 @@ export function activate(context: vscode.ExtensionContext) {
})
);

const repository = git.repositories.find((repo) => {
const remote = repo.state.remotes.find(
(remo) => remo.name === 'origin'
const activeTextEditor = vscode.window.activeTextEditor;
if (!activeTextEditor) {
vscode.window.showInformationMessage(
`${EXTENSION_NAME} can't get active text editor`
);
if (!(remote && remote.fetchUrl)) {
return false;
}
return submoduleUrls[normalize(remote.fetchUrl)] !== true;
return;
}

const absolutePath = activeTextEditor.document.fileName;
const normalizedAbsolutePath = path.normalize(
absolutePath.indexOf(path.sep) === 0
? absolutePath
: path.sep + absolutePath
);
const repository = git.repositories.find((repo) => {
const normalizedPath = path.normalize(repo.rootUri.path);
return normalizedAbsolutePath.includes(normalizedPath);
});
if (!repository) {
vscode.window.showInformationMessage(
Expand All @@ -57,30 +68,24 @@ export function activate(context: vscode.ExtensionContext) {
const fetchUrl = repository.state.remotes[0].fetchUrl;
const httpsUrl = normalize(fetchUrl!);

const activeTextEditor = vscode.window.activeTextEditor;
let filePath = '';
if (activeTextEditor) {
const absolutePath = activeTextEditor.document.fileName;
const upperPath = repository.rootUri.fsPath;
const indexOf = absolutePath.indexOf(upperPath);
const relativePath = absolutePath.slice(indexOf + upperPath.length);
filePath = relativePath;
const upperPath = repository.rootUri.fsPath;
const relativePath = path.relative(upperPath, absolutePath);
let filePath = toUnix(relativePath);

const selection = activeTextEditor.selection;
if (selection) {
const start = selection.start.line + 1;
const end = selection.end.line + 1;
filePath += `#L${start}`;
const selection = activeTextEditor.selection;
if (selection) {
const start = selection.start.line + 1;
const end = selection.end.line + 1;
filePath += `#L${start}`;

if (start !== end) {
filePath += `-L${end}`;
}
if (start !== end) {
filePath += `-L${end}`;
}
}

repository.getCommit('HEAD').then((commit) => {
const treeOrBlob = filePath.length === 0 ? 'tree' : 'blob';
const url = `${httpsUrl}/${treeOrBlob}/${commit.hash}${filePath}`;
const url = `${httpsUrl}/${treeOrBlob}/${commit.hash}/${filePath}`;
vscode.env.clipboard.writeText(url);
vscode.window.showInformationMessage(`"${url}" copied`, {
modal: false,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,11 @@ unzipper@^0.10.11:
readable-stream "~2.3.6"
setimmediate "~1.0.4"

upath@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/upath/-/upath-2.0.1.tgz#50c73dea68d6f6b990f51d279ce6081665d61a8b"
integrity sha512-1uEe95xksV1O0CYKXo8vQvN1JEbtJp7lb7C5U9HMsIp6IVwntkH/oNUzyVNQSd4S1sYk2FpSSW44FqMc8qee5w==

update-browserslist-db@^1.0.9:
version "1.0.9"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.9.tgz#2924d3927367a38d5c555413a7ce138fc95fcb18"
Expand Down