Skip to content

Commit

Permalink
Improve pull when only submodule is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-- committed Dec 4, 2024
1 parent f9a1935 commit 5dabdbb
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions lib/runners/pull-repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const debug = require('debug')('pullrepo:lib:pull-repo');
const gitHelper = require('../helpers/simple-git');

const DIRTY_STATUS_KEYS = ['modified', 'deleted', 'created', 'conflicted'];
const WIP_COMMIT_MESSAGE = '[multipull] WIP';

module.exports = async function doPullRepo(context, repo) {
debug.enabled && debug(`Processing repository ${repo}...`);
Expand All @@ -28,20 +29,24 @@ async function pullRepoIfNotAhead(context, sg, repo) {
return { files: [], summary: {} };
}

const isLocalClean = isClean(status);
const isLocalClean = isClean(context, repo, status);
debug.enabled && debug('Local branch is not up to date', { ahead: status.ahead, isLocalClean, repo });
if (!status.ahead && isLocalClean) {
return rebaseRepo(context, repo, sg, status);
}

if (!isLocalClean) {
await commitWIP(sg, context, repo, status);
const res = await commitWIP(sg, context, repo, status);
debug.enabled && debug('Created WIP commit', res);
}

const rebase = { success: false };
try {
rebase.result = await rebaseRepo(context, repo, sg, status, true);
rebase.success = true;
debug.enabled && debug('Rebased successfully', rebase.result);
} catch (err) {
debug.enabled && debug('Failed to rebase', err);
if (status.tracking === null) {
await sg.rebase({ '--abort': null });
}
Expand Down Expand Up @@ -95,18 +100,30 @@ async function rebaseRepo(context, repo, sg, status, rebaseWhenPulling = false)
async function commitWIP(sg, context, repo, status) {
const modifiedSubmodules = listModifiedSubmodules(context, repo, status);
if (modifiedSubmodules.length === 0) {
return sg.commit('[multipull] WIP', null, { '--no-verify': null, '-a': null });
debug.enabled && debug('No modified submodules, committing everything');
return sg.commit(WIP_COMMIT_MESSAGE, null, { '--no-verify': null, '-a': null });
}

debug.enabled && debug('Found modified submodules, committing everything but them', modifiedSubmodules);
await sg.add(['.'].concat(modifiedSubmodules));
return sg.commit('[multipull] WIP', null, { '--no-verify': null });
return sg.commit(WIP_COMMIT_MESSAGE, null, { '--no-verify': null });
}

async function resetWIP(sg) {
if (!await isLastCommitWIP(sg)) {
debug.enabled && debug('Last commit is not WIP, not resetting');
return;
}

await sg.reset(['--soft', 'HEAD~1']);
return sg.reset(['HEAD']);
}

async function isLastCommitWIP(sg) {
const res = await sg.log(['-1']);
return res.latest.message === WIP_COMMIT_MESSAGE;
}

function isLocalBranchUpToDate(status) {
if (status.tracking === null) {
return !status.diff_with_origin_main || status.diff_with_origin_main.behind === 0;
Expand All @@ -115,11 +132,20 @@ function isLocalBranchUpToDate(status) {
}
}

function isClean(status) {
function isClean(context, repo, status) {
for (const k of DIRTY_STATUS_KEYS) {
if (status[k].length) {
if (hasNonSubmodulesModified(context, repo, status[k])) {
return false;
}
}
return true;
}

function hasNonSubmodulesModified(context, repo, dirtyPaths) {
for (const dirtyPath of dirtyPaths) {
if (!context.submoduleToParentMap.has(repo + '/' + dirtyPath)) {
return true;
}
}
return false;
}

0 comments on commit 5dabdbb

Please sign in to comment.