UNPKG

@tangelo/tangelo-configuration-toolkit

Version:

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

46 lines (37 loc) 2.27 kB
const execGitCommand = require('./exec-git-command'); const path = require('path'); module.exports = function getTdiBranch(toBranchName) { const tdiPath = path.join(_paths.repo, _paths.tdi); const tdiBranch = {}; // 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`); // Get the first possible branch; prefer release/5.1 over release/5.2: releaseBranches.sort((a, b) => a > b ? 1 : -1); // Set branch name of firstBranch without 'remotes/origin/' tdiBranch.name = releaseBranches[0].replace(/remotes\/origin\//g, ''); // In case of branch switch set from.name to the old branch and name to the new branch if (toBranch) { if (tdiBranch.name > toBranch) _error(`You cannot downgrade to a lower release branch with TCT.`); tdiBranch.from = {name: tdiBranch.name}; tdiBranch.name = toBranch; 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; };