Skip to content

Commit

Permalink
Merge pull request #44 from Diogo-Rossi/master
Browse files Browse the repository at this point in the history
Get "command" `Git prefix emote` back to work
  • Loading branch information
rioukkevin committed Feb 3, 2024
2 parents ac826ab + e9addb6 commit f31a611
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function activate(context: ExtensionContext) {
'vscodeGitCommit.setMessage',
(params) => {
commands.executeCommand('workbench.view.scm');
const repoUri = params.rootUri.toString();
const repoUri = params?.rootUri?.toString() || undefined;
// params?._quickDiffProvider?.repository?.repositoryRoot || undefined;
let repo: Repository | false = getRepo(repoUri);
if (!!repo) {
Expand Down
35 changes: 34 additions & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, extensions } from 'vscode';
import { window, extensions, Uri } from 'vscode';
import { GitExtension, Repository } from '../typings/git';
import { IQuickPickSettings } from '../typings/quickPick';
import { getMode } from './settings';
Expand All @@ -22,6 +22,9 @@ export const getRepo = (repoUri: string): Repository | false => {
(e) => e.rootUri.toString() === repoUri
// (e) => e._repository.repository.repositoryRoot === repoUri
);
if (!repo && window.activeTextEditor) {
return getRepoContainingFile(window.activeTextEditor.document.uri, repos);
}
return repo || repos[0];
};

Expand Down Expand Up @@ -51,3 +54,33 @@ export const getCurrentBranch = (repo: Repository): IQuickPickSettings[] => {
},
];
};

/**
* Get the repository that contains the specified file.
* @param fileUri The uri of the file.
* @returns The repository containing the file, or false if no known repository contains the file.
*/
export function getRepoContainingFile(fileUri: Uri, repos: Repository[]) {
let repoUris = repos.map((rep) => rep.rootUri),
repo = null;
for (let i = 0; i < repoUris.length; i++) {
if (
fileUri
.toString()
.startsWith(pathWithTrailingSlash(repoUris[i].toString())) &&
(repo === null ||
repo.rootUri.toString().length < repoUris[i].toString().length)
)
repo = repos[i];
}
return repo || false;
}

/**
* Get the path with a trailing slash.
* @param path The path.
* @returns The path with a trailing slash.
*/
export function pathWithTrailingSlash(path: string) {
return path.endsWith('/') ? path : path + '/';
}

0 comments on commit f31a611

Please sign in to comment.