[英]Can't git push in parallel after merge using SHA1
合并分支后,我试图并行执行 git push
function checkout(branch) {
execSync(`git checkout origin/${branch}`);
}
async function merge(target, source) {
let hadExceptions = false;
let hadConflict = false;
try {
execSync(`git merge origin/${target} -m "AutoMerge Bot: Merged ${target} into ${source}"`);
} catch (error) {
if (error.stderr && error.stderr.length > 0) {
console.log(error.stderr.toString());
hadExceptions = true;
}
if (error.stdout && error.stdout.length > 0) {
console.log(error.stdout.toString());
//* When it's a merge conflict we do not treat the error as exception as it's expected behavior
if (error.stdout.includes(MergeConflictMessage)) {
await createMergeConflictCard(source, target);
//* If we don't abort the merge we won't be able to checkout
hadConflict = true;
} else {
hadExceptions = true;
}
}
execSync("git merge --abort");
}
return {
hadExceptions,
hadConflict
};
}
async function push(currentSHA, source) {
const remote = `<remote>`;
await new Promise((resolve, reject) => {
exec(`git push ${remote} ${currentSHA}:${source}`, {
maxBuffer: 10000000
},
(error, stdout, stderr) => {
if (error) {
console.log(error.toString());
}
//* We get specific stderr when push is successfull
if (stderr && !stderr.includes("View merge request for")) {
console.log(stderr.toString());
}
if (stdout) {
console.log(stdout.toString());
}
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
}
async function handleMergeRequests(mergeRequestBranches) {
let promises = [];
let exceptions = [];
for (const {
source_branch,
target_branch
} of mergeRequestBranches) {
try {
checkout(source_branch);
const {
hadConflict,
hadExceptions
} = await merge(target_branch, source_branch);
if (hadConflict || hadExceptions) {
exceptions.push({
source_branch,
hadExceptions
});
continue;
}
const currentSHA = execSync(`git rev-parse HEAD`);
promises.push(push(currentSHA, source_branch));
} catch (error) {
console.log(error.toString());
if (error.stdout) console.log(error.stdout.toString());
if (error.stderr) console.log(error.stderr.toString());
if (error.stderr) {
exceptions.push({
source_branch,
hadExceptions
});
}
}
}
await Promise.all(promises);
if (exceptions.length > 0) {
throw new Error("Encountered error, check above for more info");
}
}
对于成功的合并,我收到一个错误:
Error: Command failed: git push <remote> b9c4d95dc9408c072bbbd487110fee13a2f42c01:testing/martin/auto_merge_4
fatal: b9c4d95dc9408c072bbbd487110fee13a2f42c01 cannot be resolved to branch
我也尝试这样做:
git push <remote> testing/martin/auto_merge_4:testing/martin/auto_merge_4
没有更好的结果:
error: src refspec testing/martin/auto_merge_4 does not match any
还试过:
git push <remote> HEAD:testing/martin/auto_merge_4
结果:
! [rejected] HEAD -> testing/martin/auto_merge_4 (non-fast-forward)
error: failed to push some refs to '<remote>'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
拉没有帮助
我究竟做错了什么?
作为说明,我试图从 gtilab 作业内部推送到 gitlab 托管存储库。
我尝试了各种组合,但似乎都不起作用,
如果您希望在推送引用规范的左侧(源)侧使用哈希 ID,则在右侧使用全名是明智的:
git push <remote> <hash>:refs/heads/br/an/ch
例如。 现在明确拼出的refs/heads/
部分在左侧名称是分支名称时隐含,但原始哈希 ID 不是分支名称。 (它也不是标签名,所以如果你希望让另一个 Git 创建一个标签名,用refs/tags/na/me/of/ta/g
或其他什么拼写出来。)
至于:
error: src refspec testing/martin/auto_merge_4 does not match any
这只是意味着在 refspec 的左侧(冒号之前的部分)无法与refs/heads/testing/martin/auto_merge_4
或refs/tags/testing/martin/auto_merge_4
。 因此,您的Git不知道这是否应该是一个分支,标签,或别的东西,它根本不知道使用什么哈希ID两种。 一个更典型的git push
应该是:
git push <remote> <name>
并且name
部分暗示 refspec 是name : name
。 然后您的 Git 首先尝试将name
作为标签,然后进行分支,如果这两个中的一个有效,则知道要求另一个 Git 设置标签或分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.