UNPKG

@jupyterlab/git

Version:

A JupyterLab extension for version control using git

117 lines (116 loc) 3.53 kB
import { PathExt } from '@jupyterlab/coreutils'; import { ContextCommandIDs } from './tokens'; /** Get the filename from a path */ export function extractFilename(path) { if (path[path.length - 1] === '/') { return path; } else { return PathExt.basename(path); } } /** * Wrap mouse event handler to stop event propagation * @param fn Mouse event handler * @returns Mouse event handler that stops event from propagating */ export function stopPropagationWrapper(fn) { return (event) => { event === null || event === void 0 ? void 0 : event.stopPropagation(); fn(event); }; } export function decodeStage(x, y) { var _a; /** * All combinations of statuses for merge conflicts * @see https://git-scm.com/docs/git-status#_short_format */ const unmergedCombinations = { D: ['D', 'U'], A: ['U', 'A'], U: ['D', 'A', 'U'] }; // If the file has a merge conflict if (((_a = unmergedCombinations[x]) !== null && _a !== void 0 ? _a : []).includes(y)) { return 'unmerged'; } // If file is untracked if (x === '?' && y === '?') { return 'untracked'; } else { // If file is staged if (x !== ' ') { return y !== ' ' ? 'partially-staged' : 'staged'; } // If file is unstaged but tracked if (y !== ' ') { return 'unstaged'; } } return null; } /** * Returns a promise which resolves after a specified duration. * * @param ms - duration (in milliseconds) * @returns a promise */ export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } /** * A callback function to display a file diff between two commits. * @param commands the command registry. * @returns a callback function to display a file diff. */ export const openFileDiff = (commands) => /** * A callback function to display a file diff between two commits. * * @param commit Commit data. * @param previousCommit Previous commit data to display the diff against. If not specified, the diff will be against the preceding commit. * * @returns A callback function. */ (commit, previousCommit) => /** * Returns a callback to be invoked on click to display a file diff. * * @param filePath file path. * @param isText indicates whether the file supports displaying a diff. * @param previousFilePath when file has been relocated. * @returns callback. */ (filePath, isText, previousFilePath) => /** * Callback invoked upon clicking to display a file diff. * * @param event - event object */ async (event) => { var _a; // Prevent the commit component from being collapsed: event === null || event === void 0 ? void 0 : event.stopPropagation(); if (isText) { try { commands.execute(ContextCommandIDs.gitFileDiff, { files: [ { filePath, previousFilePath, isText, context: { previousRef: (_a = previousCommit === null || previousCommit === void 0 ? void 0 : previousCommit.commit) !== null && _a !== void 0 ? _a : commit.pre_commits[0], currentRef: commit.commit } } ] }); } catch (err) { console.error(`Failed to open diff view for ${filePath}.\n${err}`); } } };