2007-07-15 07:14:45 +08:00
|
|
|
#include "builtin.h"
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "refs.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include "tag.h"
|
2008-07-22 02:03:49 +08:00
|
|
|
#include "string-list.h"
|
2009-06-21 12:40:46 +08:00
|
|
|
#include "parse-options.h"
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
|
2009-06-21 12:40:46 +08:00
|
|
|
static const char * const show_ref_usage[] = {
|
2009-11-09 23:04:46 +08:00
|
|
|
"git show-ref [-q|--quiet] [--verify] [--head] [-d|--dereference] [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [pattern*] ",
|
2009-06-21 12:40:46 +08:00
|
|
|
"git show-ref --exclude-existing[=pattern] < ref-list",
|
|
|
|
NULL
|
|
|
|
};
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
|
2009-06-21 12:40:46 +08:00
|
|
|
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
|
|
|
|
quiet, hash_only, abbrev, exclude_arg;
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
static const char **pattern;
|
2009-06-21 12:40:46 +08:00
|
|
|
static const char *exclude_existing_arg;
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
|
2006-12-18 11:27:49 +08:00
|
|
|
static void show_one(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
const char *hex = find_unique_abbrev(sha1, abbrev);
|
|
|
|
if (hash_only)
|
|
|
|
printf("%s\n", hex);
|
|
|
|
else
|
|
|
|
printf("%s %s\n", hex, refname);
|
|
|
|
}
|
|
|
|
|
2006-09-21 15:40:28 +08:00
|
|
|
static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
{
|
|
|
|
struct object *obj;
|
2006-10-01 15:27:27 +08:00
|
|
|
const char *hex;
|
2006-11-20 05:22:44 +08:00
|
|
|
unsigned char peeled[20];
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
|
|
|
|
if (tags_only || heads_only) {
|
|
|
|
int match;
|
|
|
|
|
Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily. Leftover from this will be fixed in a separate step, including
idiotic conversions like
if (!strncmp("foo", arg, 3))
=>
if (!(-prefixcmp(arg, "foo")))
This was done by using this script in px.perl
#!/usr/bin/perl -i.bak -p
if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
}
if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
}
and running:
$ git grep -l strncmp -- '*.c' | xargs perl px.perl
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 17:53:29 +08:00
|
|
|
match = heads_only && !prefixcmp(refname, "refs/heads/");
|
|
|
|
match |= tags_only && !prefixcmp(refname, "refs/tags/");
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
if (!match)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pattern) {
|
|
|
|
int reflen = strlen(refname);
|
|
|
|
const char **p = pattern, *m;
|
|
|
|
while ((m = *p++) != NULL) {
|
|
|
|
int len = strlen(m);
|
|
|
|
if (len > reflen)
|
|
|
|
continue;
|
|
|
|
if (memcmp(m, refname + reflen - len, len))
|
|
|
|
continue;
|
|
|
|
if (len == reflen)
|
|
|
|
goto match;
|
|
|
|
/* "--verify" requires an exact match */
|
|
|
|
if (verify)
|
|
|
|
continue;
|
|
|
|
if (refname[reflen - len - 1] == '/')
|
|
|
|
goto match;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
match:
|
|
|
|
found_match++;
|
2006-11-20 05:22:44 +08:00
|
|
|
|
|
|
|
/* This changes the semantics slightly that even under quiet we
|
|
|
|
* detect and return error if the repository is corrupt and
|
|
|
|
* ref points at a nonexistent object.
|
|
|
|
*/
|
|
|
|
if (!has_sha1_file(sha1))
|
2008-09-01 00:39:19 +08:00
|
|
|
die("git show-ref: bad ref %s (%s)", refname,
|
2006-11-20 05:22:44 +08:00
|
|
|
sha1_to_hex(sha1));
|
|
|
|
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
if (quiet)
|
|
|
|
return 0;
|
2006-10-01 15:27:27 +08:00
|
|
|
|
2006-12-18 11:27:49 +08:00
|
|
|
show_one(refname, sha1);
|
2006-11-20 05:22:44 +08:00
|
|
|
|
|
|
|
if (!deref_tags)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
|
2006-11-22 15:36:35 +08:00
|
|
|
if (!is_null_sha1(peeled)) {
|
|
|
|
hex = find_unique_abbrev(peeled, abbrev);
|
|
|
|
printf("%s %s^{}\n", hex, refname);
|
|
|
|
}
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
}
|
2006-11-20 05:22:44 +08:00
|
|
|
else {
|
|
|
|
obj = parse_object(sha1);
|
|
|
|
if (!obj)
|
2008-09-01 00:39:19 +08:00
|
|
|
die("git show-ref: bad ref %s (%s)", refname,
|
2006-11-20 05:22:44 +08:00
|
|
|
sha1_to_hex(sha1));
|
|
|
|
if (obj->type == OBJ_TAG) {
|
|
|
|
obj = deref_tag(obj, refname, 0);
|
2008-02-18 15:31:54 +08:00
|
|
|
if (!obj)
|
2008-09-01 00:39:19 +08:00
|
|
|
die("git show-ref: bad tag at ref %s (%s)", refname,
|
2008-02-18 15:31:54 +08:00
|
|
|
sha1_to_hex(sha1));
|
2006-11-20 05:22:44 +08:00
|
|
|
hex = find_unique_abbrev(obj->sha1, abbrev);
|
|
|
|
printf("%s %s^{}\n", hex, refname);
|
|
|
|
}
|
|
|
|
}
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-18 09:57:19 +08:00
|
|
|
static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
|
|
|
|
{
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list *list = (struct string_list *)cbdata;
|
2010-06-26 07:41:35 +08:00
|
|
|
string_list_insert(list, refname);
|
2006-12-18 09:57:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
|
|
|
|
* and
|
|
|
|
* (1) strip "^{}" at the end of line if any;
|
|
|
|
* (2) ignore if match is provided and does not head-match refname;
|
|
|
|
* (3) warn if refname is not a well-formed refname and skip;
|
|
|
|
* (4) ignore if refname is a ref that exists in the local repository;
|
|
|
|
* (5) otherwise output the line.
|
|
|
|
*/
|
|
|
|
static int exclude_existing(const char *match)
|
|
|
|
{
|
2010-07-05 03:46:19 +08:00
|
|
|
static struct string_list existing_refs = STRING_LIST_INIT_NODUP;
|
2006-12-18 09:57:19 +08:00
|
|
|
char buf[1024];
|
|
|
|
int matchlen = match ? strlen(match) : 0;
|
|
|
|
|
|
|
|
for_each_ref(add_existing, &existing_refs);
|
|
|
|
while (fgets(buf, sizeof(buf), stdin)) {
|
|
|
|
char *ref;
|
2006-12-19 05:33:47 +08:00
|
|
|
int len = strlen(buf);
|
|
|
|
|
2006-12-18 09:57:19 +08:00
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
|
|
|
buf[--len] = '\0';
|
2006-12-19 05:33:47 +08:00
|
|
|
if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
|
2006-12-18 09:57:19 +08:00
|
|
|
len -= 3;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
for (ref = buf + len; buf < ref; ref--)
|
|
|
|
if (isspace(ref[-1]))
|
|
|
|
break;
|
|
|
|
if (match) {
|
|
|
|
int reflen = buf + len - ref;
|
|
|
|
if (reflen < matchlen)
|
|
|
|
continue;
|
|
|
|
if (strncmp(ref, match, matchlen))
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-16 05:10:25 +08:00
|
|
|
if (check_refname_format(ref, 0)) {
|
2009-03-24 09:09:16 +08:00
|
|
|
warning("ref '%s' ignored", ref);
|
2006-12-18 09:57:19 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-07-22 02:03:49 +08:00
|
|
|
if (!string_list_has_string(&existing_refs, ref)) {
|
2006-12-18 09:57:19 +08:00
|
|
|
printf("%s\n", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-21 12:40:46 +08:00
|
|
|
static int hash_callback(const struct option *opt, const char *arg, int unset)
|
|
|
|
{
|
|
|
|
hash_only = 1;
|
|
|
|
/* Use full length SHA1 if no argument */
|
|
|
|
if (!arg)
|
|
|
|
return 0;
|
|
|
|
return parse_opt_abbrev_cb(opt, arg, unset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int exclude_existing_callback(const struct option *opt, const char *arg,
|
|
|
|
int unset)
|
|
|
|
{
|
|
|
|
exclude_arg = 1;
|
|
|
|
*(const char **)opt->value = arg;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int help_callback(const struct option *opt, const char *arg, int unset)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct option show_ref_options[] = {
|
|
|
|
OPT_BOOLEAN(0, "tags", &tags_only, "only show tags (can be combined with heads)"),
|
|
|
|
OPT_BOOLEAN(0, "heads", &heads_only, "only show heads (can be combined with tags)"),
|
|
|
|
OPT_BOOLEAN(0, "verify", &verify, "stricter reference checking, "
|
|
|
|
"requires exact ref path"),
|
2009-11-09 23:04:46 +08:00
|
|
|
{ OPTION_BOOLEAN, 'h', NULL, &show_head, NULL,
|
|
|
|
"show the HEAD reference",
|
|
|
|
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
|
|
|
|
OPT_BOOLEAN(0, "head", &show_head, "show the HEAD reference"),
|
2009-06-21 12:40:46 +08:00
|
|
|
OPT_BOOLEAN('d', "dereference", &deref_tags,
|
|
|
|
"dereference tags into object IDs"),
|
|
|
|
{ OPTION_CALLBACK, 's', "hash", &abbrev, "n",
|
|
|
|
"only show SHA1 hash using <n> digits",
|
|
|
|
PARSE_OPT_OPTARG, &hash_callback },
|
|
|
|
OPT__ABBREV(&abbrev),
|
2010-11-09 03:54:48 +08:00
|
|
|
OPT__QUIET(&quiet,
|
|
|
|
"do not print results to stdout (useful with --verify)"),
|
2009-06-21 12:40:46 +08:00
|
|
|
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
|
|
|
|
"pattern", "show refs from stdin that aren't in local repository",
|
|
|
|
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
|
|
|
|
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, "show usage",
|
|
|
|
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2009-11-09 23:04:46 +08:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "-h"))
|
|
|
|
usage_with_options(show_ref_usage, show_ref_options);
|
|
|
|
|
2009-06-21 12:40:46 +08:00
|
|
|
argc = parse_options(argc, argv, prefix, show_ref_options,
|
|
|
|
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
|
2009-06-21 12:40:46 +08:00
|
|
|
if (exclude_arg)
|
|
|
|
return exclude_existing(exclude_existing_arg);
|
|
|
|
|
|
|
|
pattern = argv;
|
|
|
|
if (!*pattern)
|
|
|
|
pattern = NULL;
|
2006-12-18 10:08:52 +08:00
|
|
|
|
|
|
|
if (verify) {
|
2007-02-24 01:12:33 +08:00
|
|
|
if (!pattern)
|
|
|
|
die("--verify requires a reference");
|
2006-12-18 10:08:52 +08:00
|
|
|
while (*pattern) {
|
2007-02-24 01:12:33 +08:00
|
|
|
unsigned char sha1[20];
|
|
|
|
|
Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily. Leftover from this will be fixed in a separate step, including
idiotic conversions like
if (!strncmp("foo", arg, 3))
=>
if (!(-prefixcmp(arg, "foo")))
This was done by using this script in px.perl
#!/usr/bin/perl -i.bak -p
if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
}
if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
}
and running:
$ git grep -l strncmp -- '*.c' | xargs perl px.perl
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 17:53:29 +08:00
|
|
|
if (!prefixcmp(*pattern, "refs/") &&
|
2011-11-13 18:22:14 +08:00
|
|
|
!read_ref(*pattern, sha1)) {
|
2006-12-18 10:53:24 +08:00
|
|
|
if (!quiet)
|
2006-12-18 11:27:49 +08:00
|
|
|
show_one(*pattern, sha1);
|
2006-12-18 10:53:24 +08:00
|
|
|
}
|
2006-12-18 10:08:52 +08:00
|
|
|
else if (!quiet)
|
|
|
|
die("'%s' - not a valid ref", *pattern);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
pattern++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
if (show_head)
|
2006-09-21 15:40:28 +08:00
|
|
|
head_ref(show_ref, NULL);
|
|
|
|
for_each_ref(show_ref, NULL);
|
Add "git show-ref" builtin command
It's kind of like "git peek-remote", but works only locally (and thus
avoids the whole overhead of git_connect()) and has some extra
verification features.
For example, it allows you to filter the results, and to choose whether
you want the tag dereferencing or not. You can also use it to just test
whether a particular ref exists.
For example:
git show-ref master
will show all references called "master", whether tags or heads or
anything else, and regardless of how deep in the reference naming
hierarchy they are (so it would show "refs/heads/master" but also
"refs/remote/other-repo/master").
When using the "--verify" flag, the command requires an exact ref path:
git show-ref --verify refs/heads/master
will only match the exact branch called "master".
If nothing matches, show-ref will return an error code of 1, and in the
case of verification, it will show an error message.
For scripting, you can ask it to be quiet with the "--quiet" flag, which
allows you to do things like
git-show-ref --quiet --verify -- "refs/heads/$headname" ||
echo "$headname is not a valid branch"
to check whether a particular branch exists or not (notice how we don't
actually want to show any results, and we want to use the full refname for
it in order to not trigger the problem with ambiguous partial matches).
To show only tags, or only proper branch heads, use "--tags" and/or
"--heads" respectively (using both means that it shows tags _and_ heads,
but not other random references under the refs/ subdirectory).
To do automatic tag object dereferencing, use the "-d" or "--dereference"
flag, so you can do
git show-ref --tags --dereference
to get a listing of all tags together with what they dereference.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-16 02:19:32 +08:00
|
|
|
if (!found_match) {
|
|
|
|
if (verify && !quiet)
|
|
|
|
die("No match");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|