Skip to content

Commit

Permalink
Check if it might be required to merge origin parent branch into curr…
Browse files Browse the repository at this point in the history
…ent work branch
  • Loading branch information
nvuillam committed Nov 26, 2023
1 parent 02e9160 commit 5932206
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [2.5.0] 2023-11-26

- Check if it might be required to merge origin parent branch into current work branch.

## [2.4.0] 2023-11-15

- Add command **Audit -> Suspicious Audit Trail Activities**
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"default": false,
"description": "Disable check of Git Bash as default terminal"
},
"vsCodeSfdxHardis.disableGitMergeRequiredCheck": {
"type": "boolean",
"default": false,
"description": "Disable check if parent branch might need to be merged into current branch"
},
"vsCodeSfdxHardis.disableVsCodeColors": {
"type": "boolean",
"default": false,
Expand Down
49 changes: 31 additions & 18 deletions src/hardis-status-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,38 @@ export class HardisStatusProvider
let gitLabel = `Branch: ${currentBranch}`;
let gitTooltip = "This is the git branch you are currently working on";
let gitCommand = "";
try {
// Fetch all branches to be up to date
await git.fetch(["--all"]);
// Check if parent git branch has been updated since its creation
const parentGitBranch = await getGitParentBranch(git);
const gitDiff = await git.diff([
parentGitBranch || "",
`origin/${parentGitBranch}`,
]);
if (gitDiff.length > 0) {
gitIcon = "warning.svg";
gitLabel = `Branch: ${currentBranch} (not up to date with origin/${parentGitBranch})`;
gitTooltip = `You could merge origin/${parentGitBranch} into your local ${currentBranch} to get the latest updates merged by your colleagues :) This is recommended but not mandatory, ask your release manager for help :)`;
gitCommand = `vscode-sfdx-hardis.openExternal https://sfdx-hardis.cloudity.com/salesforce-ci-cd-merge-parent-branch/`;
const config = vscode.workspace.getConfiguration("vsCodeSfdxHardis");
if (currentBranch.includes("/") && config.get("disableGitMergeRequiredCheck") !== true) {
// Check if current branch is not up to date with origin parent branch
try {
// Fetch parent branch to make it up to date
const parentGitBranch = (await getGitParentBranch()) || "";
await git.fetch("origin", parentGitBranch);
// Get parent branch latest commit
const parentLatestCommit = await git.revparse(
`origin/${parentGitBranch}`
);
// Check if there is a commit in current branch containing the ref of the latest parent branch commit
const currentBranchCommits = await git.log([currentBranch]);
if (
!currentBranchCommits.all.some((currentBranchCommit) =>
currentBranchCommit.message.includes(parentLatestCommit)
)
) {
// Display message if a merge might be required
gitIcon = "warning.svg";
gitLabel = `Branch: ${currentBranch} (not up to date with origin/${parentGitBranch})`;
gitTooltip = `There have been new commit(s) into parent branch origin/${parentGitBranch} since you created ${currentBranch}.
You might need to merge origin/${parentGitBranch} into your local branch.
After merging, refresh VsCode SFDX-Hardis status panel to discard this warning
Note: Disable disableGitMergeRequiredCheck in settings to skip this check.`;
gitCommand = `vscode-sfdx-hardis.openExternal https://sfdx-hardis.cloudity.com/salesforce-ci-cd-merge-parent-branch/`;
}
} catch (e) {
console.warn(
"Unable to check if remote parent git branch is up to date"
);
}
} catch (e) {
console.warn(
"Unable to check if remote parent git branch is up to date"
);
}
// branch info
items.push({
Expand Down
23 changes: 19 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,26 @@ export async function loadFromLocalConfigFile(file: string): Promise<any> {
}
}

export async function getGitParentBranch(git: SimpleGit) {
export async function getGitParentBranch() {
try {
const gitLogRes = await git.show();
if (gitLogRes) {
return gitLogRes.replace("origin/", "");
const outputFromGit = (
await simpleGit({ trimmed: true }).raw("show-branch", "-a")
).split("\n");
const rev = await simpleGit({ trimmed: true }).raw(
"rev-parse",
"--abbrev-ref",
"HEAD"
);
const allLinesNormalized = outputFromGit.map((line) =>
line.trim().replace(/\].*/, "")
);
const indexOfCurrentBranch = allLinesNormalized.indexOf(`* [${rev}`);
if (indexOfCurrentBranch > -1) {
const parentBranch = allLinesNormalized[indexOfCurrentBranch + 1].replace(
/^.*\[/,
""
);
return parentBranch;
}
} catch (e) {
return null;
Expand Down

0 comments on commit 5932206

Please sign in to comment.