App-reposdb

 view release on metacpan or  search on metacpan

lib/App/reposdb.pm  view on Meta::CPAN

        $args{repo}) // {};
    for (qw/commit_time status_time pull_time/) {
        if ($res->{$_}) {
            $res->{"${_}_fmt"} = scalar localtime $res->{$_};
        }
    }
    [200, "OK", $res];
}

$SPEC{add_repo_tag} = {
    v => 1.1,
    summary => 'Add a tag to a repo (by default the current repo)',
    args => {
        %common_args,
        %repo_arg,
        %tags_arg,
    },
};
sub add_repo_tag {
    my %args = @_;

    _set_args_default(\%args);
    my $dbh = _connect_db(\%args);

    return [400, "Please specify repo name"] unless defined $args{repo};

    $dbh->begin_work;
    $dbh->do("INSERT OR IGNORE INTO repos (name) VALUES (?)",
             {}, $args{repo});
    my ($tags) = $dbh->selectrow_array("SELECT tags FROM repos WHERE name=?",
                                       {}, $args{repo});
    $tags //= '';
    my %tags = map { $_ => 1 } split /,/, $tags;
    $tags{$_} = 1 for @{ $args{tags} };
    $dbh->do("UPDATE repos SET tags=? WHERE name=?",
             {}, join(",", sort keys %tags), $args{repo});
    $dbh->commit;
    [200];
}

$SPEC{remove_repo_tag} = {
    v => 1.1,
    summary => 'Remove tag from a repo (by default the current repo)',
    args => {
        %common_args,
        %repo_arg,
        %tags_arg,
    },
};
sub remove_repo_tag {
    my %args = @_;

    _set_args_default(\%args, 1);
    my $dbh = _connect_db(\%args);

    return [400, "Please specify repo name"] unless defined $args{repo};

    $dbh->begin_work;
    my ($tags) = $dbh->selectrow_array("SELECT tags FROM repos WHERE name=?",
                                       {}, $args{repo});
    defined($tags) or return [404, "No such repo '$args{repo}'"];

    my %tags = map { $_ => 1 } split /,/, $tags;
    delete $tags{$_} for @{ $args{tags} };
    $dbh->do("UPDATE repos SET tags=? WHERE name=?",
             {}, join(",", sort keys %tags), $args{repo});
    $dbh->commit;
    [200];
}

$SPEC{remove_all_repo_tags} = {
    v => 1.1,
    summary => 'Remove all tags from a repo (by default the current repo)',
    args => {
        %common_args,
        %repo_arg,
    },
};
sub remove_all_repo_tags {
    my %args = @_;

    _set_args_default(\%args, 1);
    my $dbh = _connect_db(\%args);

    return [400, "Please specify repo name"] unless defined $args{repo};

    $dbh->begin_work;
    my ($tags) = $dbh->selectrow_array("SELECT tags FROM repos WHERE name=?",
                                       {}, $args{repo});
    defined($tags) or return [404, "No such repo '$args{repo}'"];
    $dbh->do("UPDATE repos SET tags=NULL WHERE name=?",
             {}, $args{repo});
    $dbh->commit;
    [200];
}

1;
# ABSTRACT: Manipulate repos.db

__END__

=pod

=encoding UTF-8

=head1 NAME

App::reposdb - Manipulate repos.db

=head1 VERSION

This document describes version 0.007 of App::reposdb (from Perl distribution App-reposdb), released on 2020-10-10.

=head1 SYNOPSIS

See L<reposdb>.

=head1 DESCRIPTION


C<repos.db> is a SQLite database that lists repository names along with some
extra data. They have various uses, but my first use-case for this is to store
last commit/status/pull time (updated via a post-commit git hook or C<gitwrap>).
This is useful to speed up like syncing of repositories in C<Git::Bunch> that
wants to find out which of the hundreds/thousand+ git repositories are "the most
recently used" to prioritize these repositories first. Using information from
C<repos.db> is faster than having to C<git status> or even stat() each repository.

=head1 FUNCTIONS


=head2 add_repo_tag

Usage:

 add_repo_tag(%args) -> [status, msg, payload, meta]

Add a tag to a repo (by default the current repo).

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<repo> => I<str>

=item * B<reposdb_path>* => I<str>

=item * B<tags>* => I<array[str]>



( run in 1.159 second using v1.01-cache-2.11-cpan-39bf76dae61 )