summaryrefslogtreecommitdiff
path: root/lib/App/Sqitch/Command/tag.pm
blob: 9aad0dac32edf516cca16aad2ff91fd620019b5c (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package App::Sqitch::Command::tag;

use 5.010;
use strict;
use warnings;
use utf8;
use Moo;
use App::Sqitch::X qw(hurl);
use Types::Standard qw(Str ArrayRef Maybe Bool);
use Locale::TextDomain qw(App-Sqitch);
use List::Util qw(first);
use namespace::autoclean;

extends 'App::Sqitch::Command';
with 'App::Sqitch::Role::ContextCommand';

our $VERSION = 'v1.4.1'; # VERSION

has tag_name => (
    is  => 'ro',
    isa => Maybe[Str],
);

has change_name => (
    is  => 'ro',
    isa => Maybe[Str],
);

has all => (
    is      => 'ro',
    isa     => Bool,
    default => 0
);

has note => (
    is       => 'ro',
    isa      => ArrayRef[Str],
    default  => sub { [] },
);

sub options {
    return qw(
        tag-name|tag|t=s
        change-name|change|c=s
        all|a!
        note|n|m=s@
    );
}

sub configure {
    my ( $class, $config, $opt ) = @_;
    # Just keep options.
    return $opt;
}

sub execute {
    my $self   = shift;
    my ($name, $change, $targets) = $self->parse_args(
        names      => [$self->tag_name, $self->change_name],
        all        => $self->all,
        args       => \@_,
        no_changes => 1,
    );

    if (defined $name) {
        my $note = join "\n\n" => @{ $self->note };
        my (%seen, @plans, @tags);
        for my $target (@{ $targets }) {
            next if $seen{$target->plan_file}++;
            my $plan = $target->plan;
            push @tags => $plan->tag(
                name   => $name,
                change => $change,
                note   => $note,
            );
            push @plans => $plan;
        }

        # Make sure we have a note.
        $note = $tags[0]->request_note(for => __ 'tag');

        # We good, write the plan files back out.
        for my $plan (@plans) {
            my $tag = shift @tags;
            $tag->note($note);
            $plan->write_to( $plan->file );
            $self->info(__x(
                'Tagged "{change}" with {tag} in {file}',
                change => $tag->change->format_name,
                tag    => $tag->format_name,
                file   => $plan->file,
            ));
        }
    } else {
        # Check for missing name.
        if (@_) {
            if (my $target = first { my $n = $_->name; first { $_ eq $n } @_ } @{ $targets }) {
                # Name conflicts with a target.
                hurl tag => __x(
                    'Name "{name}" identifies a target; use "--tag {name}" to use it for the tag name',
                    name => $target->name,
                );
            }
        }

        # Show unique tags.
        my %seen;
        for my $target (@{ $targets }) {
            my $plan = $target->plan;
            for my $tag ($plan->tags) {
                my $name = $tag->format_name;
                $self->info($name) unless $seen{$name}++;
            }
        }
    }

    return $self;
}

1;

__END__

=head1 Name

App::Sqitch::Command::tag - Add or list tags in Sqitch plans

=head1 Synopsis

  my $cmd = App::Sqitch::Command::tag->new(%params);
  $cmd->execute;

=head1 Description

Tags a Sqitch change. The tag will be added to the last change in the plan.

=head1 Interface

=head2 Attributes

=head3 C<tag_name>

The name of the tag to add.

=head3 C<change_name>

The name of the change to tag.

=head3 C<all>

Boolean indicating whether or not to run the command against all plans in the
project.

=head3 C<note>

Text of the tag note.

=head2 Instance Methods

=head3 C<execute>

  $tag->execute($command);

Executes the C<tag> command.

=head1 See Also

=over

=item L<sqitch-tag>

Documentation for the C<tag> command to the Sqitch command-line client.

=item L<sqitch>

The Sqitch command-line client.

=back

=head1 Author

David E. Wheeler <david@justatheory.com>

=head1 License

Copyright (c) 2012-2024 iovation Inc., David E. Wheeler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut