summaryrefslogtreecommitdiff
path: root/lib/CIL/Command/Fsck.pm
blob: 2c48b367112417dc386faae36d74ed63f66b623d (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
## ----------------------------------------------------------------------------
# cil is a Command line Issue List
# Copyright (C) 2008 Andrew Chilton
#
# This file is part of 'cil'.
#
# cil 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 <http://www.gnu.org/licenses/>.
#
## ----------------------------------------------------------------------------

package CIL::Command::Fsck;

use strict;
use warnings;

use base qw(CIL::Command);

## ----------------------------------------------------------------------------

sub name { 'fsck' }

sub run {
    my ($self, $cil, $args) = @_;


    # this looks at all the issues it can find and checks for:
    # * validity
    # * all the comments are there
    # * all the attachments are there
    # then it checks each individual comment/attachment for:
    # * ToDo: validity
    # * it's parent exists

    # find all the issues, comments and attachments
    my $issues = $cil->get_issues();
    my $issue = {};
    foreach my $i ( @$issues ) {
        $issue->{$i->name} = $i;
    }
    my $comments = $cil->get_comments();
    my $comment = {};
    foreach my $c ( @$comments ) {
        $comment->{$c->name} = $c;
    }
    my $attachments = $cil->get_attachments();
    my $attachment = {};
    foreach my $a ( @$attachments ) {
        $attachment->{$a->name} = $a;
    }

    # ------
    # issues
    my $errors = {};
    if ( @$issues ) {
        foreach my $i ( sort { $a->Inserted cmp $b->Inserted } @$issues ) {
            my $name = $i->name;

            unless ( $i->is_valid($cil) ) {
                foreach ( @{ $i->errors } ) {
                    push @{$errors->{$name}}, $_;
                }
            }

            # check that all it's comments are there and that they have this parent
            my $comments = $i->CommentList;
            foreach my $c ( @$comments ) {
                # see if this comment exists at all
                if ( exists $comment->{$c} ) {
                    # check the parent is this issue
                    push @{$errors->{$name}}, "comment '$c' is listed under issue '" . $i->name . "' but does not reciprocate"
                        unless $comment->{$c}->Issue eq $i->name;
                }
                else {
                    push @{$errors->{$name}}, "comment '$c' listed in issue '" . $i->name . "' does not exist";
                }
            }

            # check that all it's attachments are there and that they have this parent
            my $attachments = $i->AttachmentList;
            foreach my $a ( @$attachments ) {
                # see if this attachment exists at all
                if ( exists $attachment->{$a} ) {
                    # check the parent is this issue
                    push @{$errors->{$name}}, "attachment '$a' is listed under issue '" . $i->name . "' but does not reciprocate"
                        unless $attachment->{$a}->Issue eq $i->name;
                }
                else {
                    push @{$errors->{$name}}, "attachment '$a' listed in issue '" . $i->name . "' does not exist";
                }
            }

            # check that all it's 'DependsOn' are there and that they have this under 'Precedes'
            my $depends_on = $i->DependsOnList;
            foreach my $d ( @$depends_on ) {
                # see if this issue exists at all
                if ( exists $issue->{$d} ) {
                    # check the 'Precedes' is this issue
                    my %precedes = map { $_ => 1 } @{$issue->{$d}->PrecedesList};
                    push @{$errors->{$name}}, "issue '$d' should precede '" . $i->name . "' but doesn't"
                        unless exists $precedes{$i->name};
                }
                else {
                    push @{$errors->{$name}}, "issue '$d' listed as a dependency of issue '" . $i->name . "' does not exist";
                }
            }

            # check that all it's 'Precedes' are there and that they have this under 'DependsOn'
            my $precedes = $i->PrecedesList;
            foreach my $p ( @$precedes ) {
                # see if this issue exists at all
                if ( exists $issue->{$p} ) {
                    # check the 'DependsOn' is this issue
                    my %depends_on = map { $_ => 1 } @{$issue->{$p}->DependsOnList};
                    push @{$errors->{$name}}, "issue '$p' should depend on '" . $i->name . "' but doesn't"
                        unless exists $depends_on{$i->name};
                }
                else {
                    push @{$errors->{$name}}, "issue '$p' listed as preceding issue '" . $i->name . "' does not exist";
                }
            }
        }
    }
    print_fsck_errors('Issue', $errors);

    # --------
    # comments
    $errors = {};
    # loop through all the comments
    if ( @$comments ) {
        # check that their parent issues exist
        foreach my $c ( sort { $a->Inserted cmp $b->Inserted } @$comments ) {
            # check that the parent of each comment exists
            unless ( exists $issue->{$c->Issue} ) {
                push @{$errors->{$c->name}}, "comment '" . $c->name . "' refers to issue '" . $c->Issue . "' but issue does not exist";
            }
        }
    }
    print_fsck_errors('Comment', $errors);

    # -----------
    # attachments
    $errors = {};
    # loop through all the attachments
    if ( @$attachments ) {
        # check that their parent issues exist
        foreach my $a ( sort { $a->Inserted cmp $b->Inserted } @$attachments ) {
            # check that the parent of each attachment exists
            unless ( exists $issue->{$a->Issue} ) {
                push @{$errors->{$a->name}}, "attachment '" . $a->name . "' refers to issue '" . $a->Issue . "' but issue does not exist";
            }
        }
    }
    print_fsck_errors('Attachment', $errors);

    # ------------
    # nothing left
    CIL::Utils->separator();
}

sub print_fsck_errors {
    my ($entity, $errors) = @_;
    return unless keys %$errors;

    CIL::Utils->separator();
    foreach my $issue_name ( keys %$errors ) {
        CIL::Utils->title( "$entity $issue_name ");
        foreach my $error ( @{$errors->{$issue_name}} ) {
            CIL::Utils->msg("* $error");
        }
    }
}

1;
## ----------------------------------------------------------------------------