简体   繁体   English

使用 SHA1 合并后无法并行 git push

[英]Can't git push in parallel after merge using SHA1

I'm trying to git push in parallel after merging branches合并分支后,我试图并行执行 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");
    }
}

For successful merges i get an error:对于成功的合并,我收到一个错误:

Error: Command failed: git push <remote> b9c4d95dc9408c072bbbd487110fee13a2f42c01:testing/martin/auto_merge_4

fatal: b9c4d95dc9408c072bbbd487110fee13a2f42c01 cannot be resolved to branch

I also tried to do:我也尝试这样做:

git push <remote> testing/martin/auto_merge_4:testing/martin/auto_merge_4

Without any better results:没有更好的结果:

error: src refspec testing/martin/auto_merge_4 does not match any

Also tried:还试过:

git push <remote> HEAD:testing/martin/auto_merge_4

With result:结果:

! [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.

Pulling didn't help拉没有帮助

What am i doing wrong?我究竟做错了什么?

as a note, i'm trying to push from inside gtilab job to gitlab hosted repository.作为说明,我试图从 gtilab 作业内部推送到 gitlab 托管存储库。

I have tried various combinations but none of them seem to work,我尝试了各种组合,但似乎都不起作用,

If you wish to use a hash ID on the left (source) side of a push refspec, it's wise to use a full name on the right side:如果您希望在推送引用规范的左侧(源)侧使用哈希 ID,则在右侧使用全名是明智的:

git push <remote> <hash>:refs/heads/br/an/ch

for instance.例如。 The refs/heads/ part, now explicitly spelled out, is implied when the left side name is a branch name, but a raw hash ID is not a branch name.现在明确拼出的refs/heads/部分在左侧名称是分支名称时隐含,但原始哈希 ID 不是分支名称。 (It's not a tag name either, so if you wish to have the other Git create a tag name, spell that out with refs/tags/na/me/of/ta/g or whatever.) (它也不是标签名,所以如果你希望让另一个 Git 创建一个标签名,用refs/tags/na/me/of/ta/g或其他什么拼写出来。)


As for:至于:

error: src refspec testing/martin/auto_merge_4 does not match any

This just means that in the left side of the refspec—the part before the colon—could not be matched to either refs/heads/testing/martin/auto_merge_4 or refs/tags/testing/martin/auto_merge_4 .这只是意味着在 refspec 的左侧(冒号之前的部分)无法与refs/heads/testing/martin/auto_merge_4refs/tags/testing/martin/auto_merge_4 Your Git therefore has no idea whether this is supposed to be a branch, a tag, or something else, and it has no idea what hash ID to use either.因此,您的Git不知道这是否应该是一个分支,标签,或别的东西它根本不知道使用什么哈希ID两种。 A more typical git push would read:一个更典型的git push应该是:

git push <remote> <name>

and the name part implies that the refspec is name : name .并且name部分暗示 refspec 是name : name Your Git then tries the name as tag first, then branch, and if one of those two works, knows to ask the other Git to set a tag, or a branch.然后您的 Git 首先尝试将name作为标签,然后进行分支,如果这两个中的一个有效,则知道要求另一个 Git 设置标签或分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM