Skip to content

Commit

Permalink
wip: command: stage/unstage files
Browse files Browse the repository at this point in the history
  • Loading branch information
weedz committed Feb 19, 2024
1 parent 7f5b6c6 commit eec056a
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 130 deletions.
6 changes: 6 additions & 0 deletions src/Common/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const enum IpcAction {
GET_RECENT_REPOSITORIES,
OPEN_REPOSITORY,
PULL,
GET_UNSTAGED_CHANGES,
GET_STAGED_CHANGES,
}

export type IpcActionParams = {
Expand Down Expand Up @@ -153,6 +155,8 @@ export type IpcActionParams = {
[IpcAction.GET_RECENT_REPOSITORIES]: null;
[IpcAction.OPEN_REPOSITORY]: string;
[IpcAction.PULL]: null;
[IpcAction.GET_UNSTAGED_CHANGES]: null;
[IpcAction.GET_STAGED_CHANGES]: null;
};

export type IpcActionReturn = {
Expand Down Expand Up @@ -222,6 +226,8 @@ export type IpcActionReturn = {
[IpcAction.GET_RECENT_REPOSITORIES]: string[];
[IpcAction.OPEN_REPOSITORY]: boolean;
[IpcAction.PULL]: boolean;
[IpcAction.GET_UNSTAGED_CHANGES]: PatchObj[];
[IpcAction.GET_STAGED_CHANGES]: PatchObj[];
};

export type RepoStatus = {
Expand Down
1 change: 1 addition & 0 deletions src/Common/TypeHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PromiseOrSync<T> = T | Promise<T>;
3 changes: 0 additions & 3 deletions src/Main/NodegitEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export const enum RevwalkSORT {
TIME = 2,
}

export const enum DiffDELTA {
RENAMED = 4,
}
export const enum DiffOPTION {
INCLUDE_UNTRACKED = 8,
RECURSE_UNTRACKED_DIRS = 16,
Expand Down
29 changes: 17 additions & 12 deletions src/Main/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { currentProfile, getAppConfig, getAuth, signatureFromActiveProfile, sign
import { setLastKnownHead, type Context } from "./Context.js";
import { gpgSign, gpgVerify } from "./GPG.js";
import { sendAction } from "./IPC.js";
import { CheckoutSTRATEGY, DiffDELTA, DiffFIND, DiffOPTION, NodeGitErrorCODE, ObjectTYPE, ResetTYPE, RevwalkSORT, StatusOPT, StatusSHOW } from "./NodegitEnums.js";
import { CheckoutSTRATEGY, DiffFIND, DiffOPTION, NodeGitErrorCODE, ObjectTYPE, ResetTYPE, RevwalkSORT, StatusOPT, StatusSHOW } from "./NodegitEnums.js";
import { sendEvent } from "./WindowEvents.js";
import { DiffDelta } from "../Common/Utils.js";

declare module "nodegit" {
interface Repository {
Expand Down Expand Up @@ -198,7 +199,7 @@ export async function getFileCommits(repo: nodegit.Repository, params: IpcAction
// FIXME: HistoryEntry should set commit.repo.
const historyEntries = await revwalk.fileHistoryWalk(currentName, params.num || 50000);

if (historyEntries[0].status === DiffDELTA.RENAMED as unknown as nodegit.Diff.DELTA) {
if (historyEntries[0].status === DiffDelta.RENAMED as unknown as nodegit.Diff.DELTA) {
// We always "follow renames" if the file is renamed in the first commit
followRenames = true;
}
Expand All @@ -215,11 +216,11 @@ export async function getFileCommits(repo: nodegit.Repository, params: IpcAction

historyCommit.path = currentName;

if (entry.status === DiffDELTA.RENAMED as unknown as nodegit.Diff.DELTA) {
if (entry.status === DiffDelta.RENAMED as unknown as nodegit.Diff.DELTA) {
historyCommit.path = entry.oldName;
}

if (entry.status === DiffDELTA.RENAMED as unknown as nodegit.Diff.DELTA && followRenames) {
if (entry.status === DiffDelta.RENAMED as unknown as nodegit.Diff.DELTA && followRenames) {
followRenames = false;

historyCommit.path = entry.newName;
Expand Down Expand Up @@ -1191,20 +1192,24 @@ export async function loadChanges(): AsyncIpcActionReturnOrError<IpcAction.GET_C
workDirIndexPathMap.staged.clear();
workDirIndexPathMap.unstaged.clear();

const staged = workDirIndexCache.stagedPatches.map(convPatch => {
return {
staged: await loadStagedChanges(),
unstaged: await loadUnstagedChanges(),
};
}
export async function loadUnstagedChanges(): Promise<PatchObj[]> {
return workDirIndexCache.unstagedPatches.map(convPatch => {
const patch = handlePatch(convPatch);
workDirIndexPathMap.staged.set(patch.actualFile.path, convPatch);
workDirIndexPathMap.unstaged.set(patch.actualFile.path, convPatch);
return patch;
});
const unstaged = workDirIndexCache.unstagedPatches.map(convPatch => {
}
export async function loadStagedChanges(): Promise<PatchObj[]> {
return workDirIndexCache.stagedPatches.map(convPatch => {
const patch = handlePatch(convPatch);
workDirIndexPathMap.unstaged.set(patch.actualFile.path, convPatch);
workDirIndexPathMap.staged.set(patch.actualFile.path, convPatch);
return patch;
});
return {
staged,
unstaged,
};
}
export async function getWorkdirHunks(repo: nodegit.Repository, path: string, type: "staged" | "unstaged"): Promise<false | HunkObj[]> {
const patch = workDirIndexPathMap[type].get(path);
Expand Down
127 changes: 75 additions & 52 deletions src/Renderer/Components/CommandPalette/commands.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { IpcAction } from "../../../Common/Actions.js";
import { BranchFromType } from "../../../Common/Branch.js";
import { basename } from "../../../Common/Utils.js";
import { openSettings } from "../../Data/index.js";
import { openFile, openSettings } from "../../Data/index.js";
import { openDialog_BranchFrom, openDialog_Clone, openDialog_SetUpstream, openDialog_compare, openDialog_createTag, openDialog_fileHistory, openDialog_viewCommit } from "../../Data/Dialogs.js";
import { ipcGetData, ipcSendMessage } from "../../Data/IPC.js";
import { Store } from "../../Data/store.js";
import { getType } from "../DiffPane/utility.js";
import type { PromiseOrSync } from "../../../Common/TypeHelpers.js";

export interface Command {
label: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: any;
details?: string;
action: () => void | Promise<Command[]>;
action: () => PromiseOrSync<void | true | Command[]>;
focusAction?: () => PromiseOrSync<void>;
}

function openRecentRepositoryAction(this: Command) {
Expand All @@ -22,6 +25,7 @@ function checkoutBranchAction(this: Command) {
}

export const commandPaletteCommandList: Command[] = [
// REPO
{
label: "Repo: Open repository...",
action() {
Expand All @@ -39,11 +43,10 @@ export const commandPaletteCommandList: Command[] = [
async action() {
const recentRepositories = await ipcGetData(IpcAction.GET_RECENT_REPOSITORIES, null);
return recentRepositories.map(repoPath => ({
label: basename(repoPath),
details: repoPath,
action: openRecentRepositoryAction,
})
);
label: basename(repoPath),
details: repoPath,
action: openRecentRepositoryAction,
}));
}
},
{
Expand All @@ -64,6 +67,45 @@ export const commandPaletteCommandList: Command[] = [
ipcSendMessage(IpcAction.PUSH, null);
}
},
{
label: "Repo: File history...",
action() {
// TODO: Send data to main thread?
openDialog_fileHistory();
}
},
{
label: "Repo: Compare revisions...",
action() {
// TODO: Send data to main thread?
openDialog_compare();
}
},
{
label: "Repo: View commit...",
action() {
// TODO: Send data to main thread?
openDialog_viewCommit();
}
},
// Working directory
{
label: "Working directory: Stage file...",
async action() {
const unstagedChanges = await ipcGetData(IpcAction.GET_UNSTAGED_CHANGES, null);
return unstagedChanges.map(patch => ({
label: `[${getType(patch.status)}] ${patch.actualFile.path}`,
focusAction() {
openFile({ workDir: true, patch, type: "unstaged" });
},
async action() {
await ipcGetData(IpcAction.STAGE_FILE, patch.actualFile.path);
return true;
},
}));
}
},
// BRANCH
{
label: "Branch: Set upstream...",
action() {
Expand All @@ -84,17 +126,20 @@ export const commandPaletteCommandList: Command[] = [
}
},
{
label: "Open in Terminal",
action() {
ipcSendMessage(IpcAction.OPEN_IN_TERMINAL, null);
}
},
{
label: "Open in File Manager",
label: "Branch: Checkout...",
action() {
ipcSendMessage(IpcAction.OPEN_IN_FILE_MANAGER, null);
// TODO: remote branches?
if (!Store.branches) {
return;
}
return Store.branches.local.map(branch => ({
label: branch.normalizedName,
data: branch.name,
action: checkoutBranchAction,
}));
}
},
// STASH
{
label: "Stash: Stash changes",
action() {
Expand All @@ -114,62 +159,40 @@ export const commandPaletteCommandList: Command[] = [
}
},
{
label: "Repo: File history...",
action() {
// TODO: Send data to main thread?
openDialog_fileHistory();
}
},
{
label: "Repo: Compare revisions...",
label: "Blame: File",
action() {
// TODO: Send data to main thread?
openDialog_compare();
// TODO: return a list where the user can search for a file
console.log("TODO: 'Blame: File'");
}
},
{
label: "Repo: View commit...",
label: "Tag: Create at HEAD...",
action() {
// TODO: Send data to main thread?
openDialog_viewCommit();
const headRef = Store.head?.name;
if (headRef) {
openDialog_createTag(headRef);
}
}
},
// Settings and misc.
{
label: "Open preferences",
label: "Open in Terminal",
action() {
openSettings();
ipcSendMessage(IpcAction.OPEN_IN_TERMINAL, null);
}
},
{
label: "Blame: File",
label: "Open in File Manager",
action() {
// TODO: return a list where the user can search for a file
console.log("TODO: 'Blame: File'");
ipcSendMessage(IpcAction.OPEN_IN_FILE_MANAGER, null);
}
},
{
label: "Branch: Checkout...",
label: "Open preferences",
action() {
// TODO: remote branches?
if (!Store.branches) {
return;
}
return Store.branches.local.map(branch => ({
label: branch.normalizedName,
data: branch.name,
action: checkoutBranchAction,
}));
openSettings();
}
},
{
label: "Tag: Create at HEAD...",
action() {
const headRef = Store.head?.name;
if (headRef) {
openDialog_createTag(headRef);
}
}
}

/**
* TOOD:
Expand Down
Loading

0 comments on commit eec056a

Please sign in to comment.