UNPKG

@tangelo/tangelo-configuration-toolkit

Version:

Tangelo Configuration Toolkit is a command-line toolkit which offers support for developing a Tangelo configuration.

55 lines (46 loc) 2.69 kB
const execGitCommand = require('./exec-git-command'); const path = require('path'); const compareVersions = require('compare-versions'); module.exports = function getTdiBranch(toBranchName) { const tdiPath = path.join(_paths.repo, _paths.tdi); // Fetch all _info(`Fetch TDI submodule`); const cmdFetch = execGitCommand('fetch -pf --all', tdiPath); if (cmdFetch.error) _warn(`Fetch failed\n${cmdFetch.error}`); let toBranch; if (toBranchName) { // Check if specified branch exists; we will update to this branch toBranch = String(toBranchName).replace(/(?:release\/)?(\d+(?:\.[\dxt]+)*)/, `release/$1`); const branchExists = execGitCommand(`branch --remote`, tdiPath).match(`origin/${toBranch}`); if (!branchExists) _error(`TDI branch "${toBranch}" does not exist. Note that TCT can only update to a release branch.`); } // Get remote release branches containing TDI HEAD commit const releaseBranches = execGitCommand(`branch --all --contains ${_git.commitTdi.local().hash}`, tdiPath).match(/remotes\/origin\/release\/[^\s]+/gsm); if (!releaseBranches || releaseBranches.error) _error(`Could not retrieve TDI release branches`); // Create a sorted list of release branches with their versions: const sortedBranches = releaseBranches .map(branch => { return { name: branch.replace('remotes/origin/', ''), version: branch.replace('remotes/origin/release/', '') } }) .sort((a, b) => compareVersions(a.version, b.version)); // Choose the first possible branch; prefer release/5.1 over release/5.2: const tdiBranch = sortedBranches[0]; // In case of branch switch set from.name to the old branch and name to the new branch if (toBranch) { const toVersion = toBranch?.replace('release/', ''); if (compareVersions.compare(tdiBranch.version, toVersion, '>')) _error(`You cannot downgrade to a lower release branch with TCT.`); tdiBranch.from = {name: tdiBranch.name, version: tdiBranch.version}; tdiBranch.name = toBranch; tdiBranch.version = toVersion; const branchHash = execGitCommand(`rev-parse origin/${toBranch}`, tdiPath); const commonAncestorHash = execGitCommand(`merge-base ${_git.commitTdi.local().hash} ${branchHash}`, tdiPath); const commonAncestorDate = execGitCommand(`show ${commonAncestorHash} --no-patch --format=%cd --date=iso-strict `, tdiPath, ['date']).date; tdiBranch.commonAncestor = {date: new Date(commonAncestorDate)}; } // Get number of commits behind tdiBranch.commitsBehind = execGitCommand(`rev-list HEAD...origin/${tdiBranch.name} --count`, tdiPath); return tdiBranch; };