This fix several bugs in for-each-ref for %(push) and %(push:remoteref), as explained in the commit messages. Note that there are still several bugs: - the memory leak mentioned by Jeff in https://public-inbox.org/git/20200328131553.GA643242@xxxxxxxxxxxxxxxxxxxxxxx/ - in my patch, to detect if the workflow is triangular, I use: static int is_workflow_triangular(struct branch *branch) { struct remote *fetch_remote = remote_get(remote_for_branch(branch, NULL)); struct remote *push_remote = remote_get(pushremote_for_branch(branch, NULL)); return (fetch_remote && push_remote && fetch_remote != push_remote); } But remote_get will always fallback to 'origin'. So this means that if we set up a pushRemote="foobar" and no 'remote', the workflow is detected as triangular. Whereas in `git push`, this workflow will not be detected as triangular. => So I can check that by looking at *explicit, but I actually have a question about what constitutes a triangular workflow, hence the RFC. Furthermore, the upstream (and simple in non triangular workflow) case of %(push) and (push:remoteref) are essentially via `branch_get_upstream`, which is also used for %(upstream): branch && branch->merge && branch->merge[0] && branch->merge[0]->dst) but `git push` does different checks: if (!branch->merge_nr || !branch->merge || !branch->remote_name) die(_("The current branch %s has no upstream branch.\n"... if (branch->merge_nr != 1) die(_("The current branch %s has multiple upstream branches, " "refusing to push."), branch->name); in particular git push fails if merge_nr !=1 or if branch has no remote, whereas %(push) will still indicates a push branch (assuming I fix is_workflow_triangular). So I'll need to add a `branch_get_push` with these checks instead. So I first send this patch as an RFC, and I'll see how to proceed afterwards to handle these remaining corner cases. Luckily, having a pushRemote but no remote, or several merge in the branch config are probably not too common. => So one question I have first is about the case when we do have a branch.pushRemote but not a branch.remote. Should this still be considered a triangular workflow? According to git-push, no: static int is_workflow_triangular(struct remote *remote) { struct remote *fetch_remote = remote_get(NULL); return (fetch_remote && fetch_remote != remote); } but I would argue that we should. This would change nothing for push.default=upstream, since currently we check that `branch` has a remote_name in `setup_push_upstream` so it fails anyway even if the workflow is not explicitly triangular, but this would make push.default=simple behave as current, exactly as when branch.remote is different from branch.pushRemote (and I would argue that no branch.remote is a particular case of this situation). PS: the first patch has no tests because I add them in the second patch, it is more convenient to add them at once and test both patches. PPS: v4 and v5 are intermediate versions I made but did not send to the ML. Damien Robert (2): remote.c: fix %(push) for triangular workflows remote.c: fix handling of %(push:remoteref) remote.c | 139 ++++++++++++++++++++++++++++++---------- t/t6300-for-each-ref.sh | 81 +++++++++++++++++++++-- 2 files changed, 180 insertions(+), 40 deletions(-) -- Patched on top of v2.26.0-106-g9fadedd637 (git version 2.26.0)