Skip to content

Commit

Permalink
Switch to regular git status
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-centore committed May 11, 2024
1 parent b773c17 commit 9fbece3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 75 deletions.
110 changes: 42 additions & 68 deletions src/main/gitlib/git-read.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,53 @@
import util from 'util';
import fs from 'fs';
import git from 'isomorphic-git';
import { exec } from 'child_process';
import { ChangeType } from '../../types/types';

export const getModifiedFiles = async (dir: string) => {
const FILE = 0;
const HEAD = 1;
const WORKDIR = 2;
const STAGE = 3;

// TODO: Better types
const statusMapping: Record<string, string> = {
'003': 'added, staged, deleted unstaged',
'020': 'new, untracked',
'022': 'added, staged',
'023': 'added, staged, with unstaged changes',
'100': 'deleted, staged',
'101': 'deleted, unstaged',
'103': 'modified, staged, deleted unstaged',
'111': 'unmodified',
'121': 'modified, unstaged',
'122': 'modified, staged',
'123': 'modified, staged, with unstaged changes',
};

const statusToModifiedMapping: Record<string, ChangeType | null> = {
'003': null,
'020': 'new',
'022': 'new',
'023': 'new',
'100': 'deleted',
'101': 'deleted',
'103': 'modified',
'111': null,
'121': 'modified',
'122': 'modified',
'123': 'modified',
};

const statusMatrix = (await git.statusMatrix({ fs, dir })).filter(
(row) => row[HEAD] !== row[WORKDIR] || row[HEAD] !== row[STAGE],
);

const allUncommitedChanges = statusMatrix.map(
// (row) => `${statusMapping[row.slice(1).join('')]}: ${row[FILE]}`,
(row) => ({
filename: row[FILE],
// TODO: Use something better than the status
status: statusMapping[row.slice(1).join('')],
change: statusToModifiedMapping[row.slice(1).join('')],
}),
// Cannot use isomorphic git here because it currently differs from git behavior:
// https://github.com/isomorphic-git/isomorphic-git/issues/1215

const execPromise = util.promisify(exec);
const { stdout } = await execPromise(
'git status --porcelain -z --no-renames',
{
cwd: dir,
},
);

return allUncommitedChanges;
const statusMapping: Record<
string,
{ change: ChangeType; staged: boolean } | null
> = {
'M ': { change: 'modified', staged: true },
'T ': { change: 'modified', staged: true },
'D ': { change: 'deleted', staged: true },
' M': { change: 'modified', staged: false },
' T': { change: 'modified', staged: false },
' D': { change: 'deleted', staged: false },
UU: { change: 'modified', staged: false },
AA: { change: 'modified', staged: false },
'??': { change: 'new', staged: false },
};

// const execPromise = util.promisify(exec);
// // eslint-disable-next-line no-await-in-loop
// const variant = 'modified' as 'unmerged' | 'modified';
// const { stdout: unmergedFilesRaw } = await execPromise(
// `git --no-pager ls-files --${variant}`,
// { cwd: dir },
// );
// const unmergedFiles = [
// ...new Set(
// unmergedFilesRaw
// .trim()
// .split('\n')
// .filter((line) => line.trim().length > 0)
// .map((line) => (variant === 'unmerged' ? line.split('\t')[1] : line))
// .map((file) => `${dir}/${file}`),
// ),
// ];
// unmergedFiles.sort();
// // console.log({ unmergedFiles });
// return unmergedFiles;
const parsed = stdout
.split('\0')
.filter((row) => row.trim().length > 0)
.flatMap((row) => {
const mapping = statusMapping[row.substring(0, 2)];
if (!mapping) {
return [];
}
return [
{
change: mapping?.change,
staged: mapping?.staged,
filename: row.substring(3),
},
];
});

return parsed;
};

export const rebaseInProgress = async (dir: string) => {
Expand Down
9 changes: 2 additions & 7 deletions src/main/gitlib/git-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ const extractGitTree = async (): Promise<TreeData | null> => {
const unmergedFilenames = unmergedFiles
// These are files which changed as part of the rebase but didn't have conflicts
// They should be excluded from the view
.filter((x) => x.status.includes('unstaged'))
.filter((x) => !x.staged)
.map((x) => x.filename);
const readFilePromise = util.promisify(fs.readFile);
const conflictedFiles = [];
Expand All @@ -284,11 +284,7 @@ const extractGitTree = async (): Promise<TreeData | null> => {
} else if (unmergedFiles.length > 0) {
commit.branchSplits.push({
type: 'modified',
dirtyFiles: unmergedFiles.filter((x) => !!x.status) as {
filename: string;
status: string;
change: ChangeType;
}[],
dirtyFiles: unmergedFiles,
branches: commit.metadata.branches,
rootCommit: commit,
});
Expand Down Expand Up @@ -342,7 +338,6 @@ export const reloadGitTree = async ({
// This happens in rare instances (usually race condition with filesystem)
// Just ignore it and let the tree reload automatically
console.error('Error reloading tree', e);
mainWindow?.webContents.send('git-tree-updated', null);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type TreeModified = {
dirtyFiles: {
filename: string;
change: ChangeType;
staged: boolean;
}[];
branches: Branch[];

Expand Down

0 comments on commit 9fbece3

Please sign in to comment.