Skip to content

Commit

Permalink
Lock improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-centore committed May 13, 2024
1 parent 9f5474c commit 7c3a974
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/gitlib/git-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ const performRebaseHelper = async ({

const fromBranch = tempBranchName;

const returnValue = await spawnTerminal({
const { returnCode } = await spawnTerminal({
command: {
cmd: 'git',
args: ['rebase', '--onto', to, `${fromBranch}~`, fromBranch],
},
dir,
mainWindow,
});
if (returnValue !== 0) {
if (returnCode !== 0) {
let waitingForRebaseComplete = true;
while (waitingForRebaseComplete) {
if (!rebaseStatusInProgress()) {
Expand Down
20 changes: 16 additions & 4 deletions src/main/gitlib/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export const spawnTerminal = async ({
command: Command;
dir: string;
mainWindow: BrowserWindow;
}): Promise<number> => {
}): Promise<{ out?: string; returnCode: number }> => {
if (ptyProcess) {
// TODO Lock instead
console.log('Error: Process already running!');
return -1;
return { returnCode: -1 };
}

// const shPath = await shellPath();
Expand All @@ -37,6 +37,11 @@ export const spawnTerminal = async ({
cols: 80,
rows: 10,
cwd: dir,
env: {
// Forces language to English
// Needed for automatic processing (e.g. to retry if lock exists)
LC_ALL: 'C',
},
// TODO: Fix path
// env: process.env,
});
Expand All @@ -46,16 +51,23 @@ export const spawnTerminal = async ({
`${cmd} ${args.map((arg) => escapeShellArg(arg)).join(' ')}\r\n`,
);

const outObj = { out: '' };

ptyProcess.onData((ptyData) => {
mainWindow?.webContents.send('terminal-out', ptyData);
outObj.out += ptyData;
});

const ptyProcessFinal = ptyProcess;
return new Promise<number>((resolve) => {
return new Promise((resolve) => {
ptyProcessFinal.onExit((x) => {
mainWindow?.webContents.send('terminal-out', '\r\n> ');
ptyProcess = null;
resolve(x.exitCode);
const result = {
returnCode: x.exitCode,
out: outObj.out,
};
resolve(result);
});
});
};
Expand Down
20 changes: 13 additions & 7 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,23 @@ ipcMain.handle('run-cmds', async (_event, data) => {
}

for (const command of data as Command[]) {
while (fs.existsSync(`${dir}/.git/index.lock`)) {
console.log('Lock exists, waiting...');
await sleep(100);
}
const returnValue = await spawnTerminal({
const result = await spawnTerminal({
command,
dir,
mainWindow,
});
if (returnValue !== 0) {
return returnValue;
if (result.returnCode !== 0) {
if (
result.out?.includes(
'Another git process seems to be running in this repository',
)
) {
console.log('Lock exists, retrying...');
// Retry if a lock causes the git process to fail
await sleep(300);
} else {
return result.returnCode;
}
}

await reloadGitTree({ mainWindow });
Expand Down

0 comments on commit 7c3a974

Please sign in to comment.