summaryrefslogtreecommitdiff
path: root/perl5/Local/MrRepo/Repo.pm
blob: 8def464132c944d9b8f474222ce378df5d78f05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package Local::MrRepo::Repo;

# Copyright (C) 2019-2020 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Exporter 'import';
use File::Spec::Functions qw(rel2abs);
use Local::Interactive qw(system_pty_capture);
use List::Util qw(all);

# Quoting perldoc perlmodlib: "As a general rule, if the module is
# trying to be object oriented then export nothing. If it's just a
# collection of functions then @EXPORT_OK anything but use @EXPORT
# with caution."
our @EXPORT_OK = ();

# constructor
sub new {
    my ($class, $toplevel) = @_;

    bless {toplevel => rel2abs($toplevel), updated => 0} => $class;
}

# attributes
sub toplevel { shift->{toplevel} }
sub updated  { shift->{updated}  }

# public methods
sub auto_commit { shift->_mr_cmd("-m", "autoci") }
sub update {
    my $self = shift;

    # note that this may also stow
    my $result = $self->_mr_cmd("update");
    $self->{updated} = 1
      if $result->{exit} == 0
      # permit nonzero exit for package that might still be in NEW
      || $result->{output}
      =~ /^dgit: source package [a-z0-9+.-]+ does not exist in suite [a-z]+\s*$/m;
    return $result;
}
sub status {
    my $self = shift;

    my $result = $self->_mr_cmd("status");

    # in the case of command success with zero output, get rid of mr's
    # own output, so caller can just check for an empty string
    if ($result->{exit} == 0) {
        my @output = grep /[[:print:]]/, split "\n", $result->{output};
        $result->{output} = "" if all { /^mr status: / } @output;
    }

    return $result;
}

# private methods
sub _mr_cmd {
    # here we interpolate @_ into a string because
    # Local::Interactive::_sysptycap_script cannot handle a list of
    # arguments (though Local::Interactive::_sysptycap_tty can)

    system_pty_capture("mr -d " . shift->toplevel . " @_");
}

1;