refname_match(): Caller must declare if we're matching local or remote refs

In preparation for adding alternative ways of expanding refspecs, we
need to take into account that not all such alternatives may be
appropriate in all circumstances. Specifically, refname_match() is used
to check whether a given shorthand name matches a given full refname,
but that full refname does not always belong in the local repo, rather
it is sometimes taken from list of refs sent over from a remote repo.

When a remote refname is passed, it would be wrong to apply expansion
rules whose behavior rely on configuration specific to the local repo.
Hence, when calling refname_match(), we must specify whether we want
expansion of a local ref or a remote ref.

Implement this differentiation by adding a flags argument to
refname_match() that takes either REFNAME_MATCH_LOCAL or
REFNAME_MATCH_REMOTE. Furthermore, tag each expansion rule with the
flags it supports (currently all rules support expansion in both
local and remote contexts, since the rules are constant and independent
of local repo details). Finally, teach refname_match() to skip rules
that don't support the given context.

The callers of refname_match now pass the appropriate context in the
new flags argument. The same flags argument could be added to the other
functions that use ref_expand_rules as well (dwim_ref(), dwim_log() and
shorten_unambiguous_ref()), but are AFAICS always called in a local
context.

Signed-off-by: Johan Herland <johan@herland.net>
WIP: refs.c: Add rules for resolving refs using remote refspecs

FIXME: Add more tests, with multiple new-style remotes, and making sure shortening is unambiguous, etc.
FIXME: Add removal of { ref_expand_txtly, ref_shorten_txtly, "refs/remotes/%.*s" } as the next commit (does it pass tests?).
FIXME: What about { ref_expand_txtly, ref_shorten_txtly, "refs/remotes/%.*s/HEAD" }, txtly -> refspec entry?
FIXME: What about prettify_refname()?

The "$remote/$branch" expression is often used as a shorthand with the
intention of referring to the remote-tracking branch corresponding to
the given $branch in the $remote repo.

Currently, Git resolves this by prepending "refs/remotes/" to the given
"$remote/$branch" expression, resulting in "refs/remotes/$remote/$branch",
which is equivalent to the above intention only when conventional
refspecs are being used (e.g. refs/heads/*:refs/remotes/origin/*).
Correspondingly, a full remote-tracking branch name, can only be
shortened to the "$remote/$branch" form if it textually matches the
"refs/remotes/$remote/$branch" format.

If unconventional refspecs (e.g. refs/heads/*:refs/remotes/origin/heads/*)
are being used, then the current expansion ("refs/remotes/$remote/$branch")
fails to match the remote-tracking branches from the configured remotes.
Ditto for the corresponding shortening.

Instead of doing pure textual expansion (from "$remote/$branch" to
"refs/remotes/$remote/$branch"), we should expand by finding all remotes
matching "$remote", look up their fetch refspecs, and map
"refs/heads/$branch" through them to find the appropriate remote-tracking
branch name corresponding to "$remote/$branch". This would yield the
correct remote-tracking branch in all cases, both for conventional and
unconventional refspecs.

Likewise, when shortening full refnames for remote-tracking branches into
their shorthand form ("$remote/$branch"), we should find the refspec with
the RHS that matches the full remote-tracking branch name, and map
through that refspec to produce the corresponding LHS (minus the leading
"refs/heads/" part), and from that construct the corresponding
"$remote/$branch" shorthand.

This patch adds a new expansion method - ref_expand_refspec() - and a
corresponding shortening method - ref_shorten_refspec() - that implements
the remote refspec traversal and expanding/shortening logic described
above. These expand/shorten methods complement the existing
ref_expand_txtly() and ref_shorten_txtly() methods that implement the
current textual expanding/shortening logic.

Implementing the proper expanding/shortening of "$remote/$branch" is now
a simple matter of adding another entry to the ref_expand_rules list,
using the new ref_expand_refspec() and ref_shorten_refspec() functions.

Note that the existing "refs/remotes/*" textual expansion/shortening rule
is kept to preserve backwards compatibility for refs under refs/remotes/*
that are not covered by any configured refspec.

Signed-off-by: Johan Herland <johan@herland.net>
DOA: Prepare for removal of textual refs/remotes/* substitution