The usual reason to force rebasing is a pathological hatred of merge commits because the person setting the policy doesn't see their value. It's not clear why you'd want to take the disadvantages of rebase (odds are the intermediate commits will not have been tested) but still have the merge; this seems to me like the least valuable merge commit possible. But if you must...
You imply that the merge you want to accept would be "empty"; that's not exactly true. It applies changes to its first parent (though not to its 2nd parent, since it would be a fast-forward if allowed to be).
What I think you're really saying is that you would accept a merge if the first parent is reachable (via parent pointers) from the second parent. So you could take the output of
git rev-list --merges $oldrev..$newrev
and feed each resulting commit ID as the commit-ID arguments in
git merge-base --is-ancestor commit-ID^ commit-ID^2
rejecting if the merge-base
command ever returns non-zero.
(Technically I guess you might also want to make sure the commit didn't have 3 or more parents.)
That still allows something like this
(origin/master)
|
x -- x -- x -------------------- M <--(master)
\ /
x -- x -------x -- x
\ /
x -- x
If avoiding that is a rule, it's significantly harder; you basically would want every merge to be reachable via first-parent pointers from the head commit. (But you can't just say that merges should be reachable from the head commit's first parent, because then you'd still allow
(origin/master)
|
x -- x -- x -------------------- M -- x<--(master)
\ /
x -- x -------x -- x
\ /
x -- x
which is the same thing.) So you could maybe, as a "first step" before you start looking for merges, do a
git rev-list --first-parent $oldrev..$newrev
and hang onto a list of all the commit ID values that returns, so as you find each merge you can confirm that it's in that list.
If this all sounds like no fun at all, I couldn't agree more; which is why I'm not going to the trouble of trying to assemble a working script from this advice, and why I recommend you either allow merges or don't instead of trying to take such an unusual middle ground.