Skip to content

Commit

Permalink
check cursor-word chooseRef is actually a commit, otherwise throw away
Browse files Browse the repository at this point in the history
  • Loading branch information
kahole committed Jan 18, 2024
1 parent 153b0d6 commit 3f66cd1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/utils/magitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RefType, Repository } from '../typings/git';
import { PickMenuItem, PickMenuUtil } from '../menu/pickMenu';
import GitTextUtils from '../utils/gitTextUtils';
import * as Constants from '../common/constants';
import { getCommit } from './commitCache';

export interface Selection {
key: string;
Expand Down Expand Up @@ -150,24 +151,31 @@ export default class MagitUtils {

public static async chooseRef(repository: MagitRepository, prompt: string, showCurrent = false, showHEAD = false, allowFreeform = true, remoteOnly = false): Promise<string> {

const getCursorCommitHash: () => PickMenuItem<string> | undefined = () => {
const getCursorCommitHash: () => Promise<PickMenuItem<string> | undefined> = async () => {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor === undefined) {
return;
return undefined;
}
const document = activeEditor.document;
const selection = activeEditor.selection;
const hashWordRange = document.getWordRangeAtPosition(selection.active, /[0-9a-z]{7}/);
if (hashWordRange === undefined) {
return;
return undefined;
}
const hash = document.getText(hashWordRange);

try {
await getCommit(repository.gitRepository, hash);
} catch (error) {
return undefined;
}

return { label: hash, meta: hash };
};

const refs: PickMenuItem<string>[] = [];

const cursorCommitHash = getCursorCommitHash();
const cursorCommitHash = await getCursorCommitHash();

if (cursorCommitHash) {
refs.push(cursorCommitHash);
Expand Down

0 comments on commit 3f66cd1

Please sign in to comment.