bin/refs: Add option to print revisions

Allow printing the revision along with the ref. This is very convenient
for looping over the refs in a shell as well as for quickly seeing which
refs are pointed to the same commit.
This commit is contained in:
Dan Nicholson 2023-01-29 09:23:13 -07:00
parent 6b0f42ae37
commit 90dd45fb27
4 changed files with 35 additions and 1 deletions

View File

@ -953,6 +953,7 @@ _ostree_pull() {
_ostree_refs() {
local boolean_options="
$main_boolean_options
--revision -r
--alias -A
--collections -c
--delete

View File

@ -98,6 +98,15 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--revision</option>, <option>-r</option></term>
<listitem><para>
When listing refs, also print their revisions. The revisions
will be separated by a tab character.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--alias</option>, <option>-A</option></term>

View File

@ -27,6 +27,7 @@
static gboolean opt_delete;
static gboolean opt_list;
static gboolean opt_revision;
static gboolean opt_alias;
static char *opt_create;
static gboolean opt_collections;
@ -40,6 +41,7 @@ static gboolean opt_force;
static GOptionEntry options[] = {
{ "delete", 0, 0, G_OPTION_ARG_NONE, &opt_delete, "Delete refs which match PREFIX, rather than listing them", NULL },
{ "list", 0, 0, G_OPTION_ARG_NONE, &opt_list, "Do not remove the prefix from the refs", NULL },
{ "revision", 'r', 0, G_OPTION_ARG_NONE, &opt_revision, "Show revisions in listing", NULL },
{ "alias", 'A', 0, G_OPTION_ARG_NONE, &opt_alias, "If used with --create, create an alias, otherwise just list aliases", NULL },
{ "create", 0, 0, G_OPTION_ARG_STRING, &opt_create, "Create a new ref for an existing commit", "NEWREF" },
{ "collections", 'c', 0, G_OPTION_ARG_NONE, &opt_collections, "Enable listing collection IDs for refs", NULL },
@ -82,7 +84,16 @@ do_ref_with_collections (OstreeRepo *repo,
for (GList *iter = ordered_keys; iter != NULL; iter = iter->next)
{
OstreeCollectionRef *ref = iter->data;
g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
if (opt_revision)
{
const char *rev = g_hash_table_lookup (refs, ref);
g_print ("(%s, %s)\t%s\n", ref->collection_id, ref->ref_name, rev);
}
else
{
g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
}
}
}
else if (opt_create)
@ -203,6 +214,11 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
const char *alias = g_hash_table_lookup (refs, ref);
g_print ("%s -> %s\n", ref, alias);
}
else if (opt_revision)
{
const char *rev = g_hash_table_lookup (refs, ref);
g_print ("%s\t%s\n", ref, rev);
}
else
{
g_print ("%s\n", ref);

View File

@ -55,6 +55,14 @@ assert_file_has_content refs foo
${CMD_PREFIX} ostree --repo=repo refs foo | wc -l > refscount.foo
assert_file_has_content refscount.foo "^5$"
rm -f expected-refs-revs
for ref in foo/test-{1..5}; do
rev=$(${CMD_PREFIX} ostree --repo=repo rev-parse $ref)
echo -e "${ref}\t${rev}" >> expected-refs-revs
done
${CMD_PREFIX} ostree --repo=repo refs --list --revision foo > refs-revs
assert_files_equal refs-revs expected-refs-revs
${CMD_PREFIX} ostree --repo=repo refs --delete 2>/dev/null || true
${CMD_PREFIX} ostree --repo=repo refs | wc -l > refscount.delete1
assert_file_has_content refscount.delete1 "^10$"