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
207
208
209
210
211
212
// Copyright 2016 Kitware, Inc.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use impl_prelude::*;

#[derive(Debug, Default, Clone, Copy)]
/// Check commit message subjects for invalid patterns.
pub struct CommitSubject {
    /// The minimum length allowed for the summary line.
    min_summary: usize,
    /// The maximum length allowed for the summary line.
    max_summary: usize,

    /// Whether to deny work-in-progress commits or not.
    check_wip: bool,

    /// Whether to deny `fixup!` and `squash!` commits or not.
    check_rebase_commands: bool,
}

impl CommitSubject {
    /// Checks commit message subjects for invalid patterns
    ///
    /// Patterns which are checked for:
    ///   - overly long or short summary lines;
    ///   - work-in-progress messages; and
    ///   - `fixup!` and `squash!` messages.
    ///
    /// Commit messages which appear to have been auto generated by actions such as merging or
    /// reverting commits will skip the summary line length limit (if enforced).
    pub fn new() -> Self {
        CommitSubject {
            min_summary: 8,
            max_summary: 78,

            check_wip: true,

            check_rebase_commands: true,
        }
    }

    /// Check the summary line with the given limits.
    pub fn with_summary_limits(&mut self, min: usize, max: usize) -> &mut Self {
        self.min_summary = min;
        self.max_summary = max;
        self
    }

    /// Checks for work-in-progress commits
    ///
    /// Commit messages which mention `WIP` or `wip` at the beginning of their commit messages are
    /// rejected since they are (nominally) incomplete.
    pub fn check_work_in_progress(&mut self, wip: bool) -> &mut Self {
        self.check_wip = wip;
        self
    }

    /// Check for rebase commands
    ///
    /// Rebase commands include commits which begin with `fixup! ` or `squash! `. These subjects
    /// are used to indicate that the commit belongs somewhere else in the branch and should be
    /// completed before merging.
    pub fn check_rebase_commands(&mut self, rebase: bool) -> &mut Self {
        self.check_rebase_commands = rebase;
        self
    }

    /// Whether the summary is generated or not.
    ///
    /// The commit summaries generated by `git merge` and `git revert` can be long, but since they
    /// are auto-generated, allow them.
    fn is_generated_subject(summary: &str) -> bool {
        false ||
            summary.starts_with("Merge ") ||
            summary.starts_with("Revert ")
    }
}

impl Check for CommitSubject {
    fn name(&self) -> &str {
        "commit-subject"
    }

    fn check(&self, _: &CheckGitContext, commit: &Commit) -> Result<CheckResult> {
        let mut result = CheckResult::new();
        let lines = commit.message.trim().lines().collect::<Vec<_>>();

        if lines.is_empty() {
            result.add_error(format!("commit {} has an invalid commit subject; it is empty.",
                                     commit.sha1));
            return Ok(result);
        }

        let summary = &lines[0];
        let summary_len = summary.len();

        if summary_len < self.min_summary {
            result.add_error(format!("commit {} has an invalid commit subject; the first line \
                                      must be at least {} characters.",
                                     commit.sha1,
                                     self.min_summary));
        }

        if !Self::is_generated_subject(summary) && self.max_summary < summary_len {
            result.add_error(format!("commit {} has an invalid commit subject; the first line \
                                      must be no longer than {} characters.",
                                     commit.sha1,
                                     self.max_summary));
        }

        if lines.len() >= 2 {
            if lines.len() >= 2 && !lines[1].is_empty() {
                result.add_error(format!("commit {} has an invalid commit subject; the second \
                                          line must be empty.",
                                         commit.sha1));
            }

            if lines.len() == 2 {
                result.add_error(format!("commit {} has an invalid commit subject; it cannot be \
                                          exactly two lines.",
                                         commit.sha1));
            } else if lines[2].is_empty() {
                result.add_error(format!("commit {} has an invalid commit subject; the third \
                                          line must not be empty.",
                                         commit.sha1));
            }
        }

        if self.check_wip && (summary.starts_with("WIP") || summary.starts_with("wip")) {
            result.add_error(format!("commit {} cannot be merged; it is marked as a \
                                      work-in-progress (WIP).",
                                     commit.sha1));
        }

        if self.check_rebase_commands {
            if summary.starts_with("fixup! ") {
                result.add_error(format!("commit {} cannot be merged; it is marked as a fixup \
                                          commit.",
                                         commit.sha1));
            } else if summary.starts_with("squash! ") {
                result.add_error(format!("commit {} cannot be merged; it is marked as a commit \
                                          to be squashed.",
                                         commit.sha1));
            }
        }

        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use checks::CommitSubject;
    use checks::test::*;

    static BAD_TOPIC: &'static str = "5f7284fe1599265c90550b681a4bf0763bc1de21";

    #[test]
    fn test_check_subject() {
        let check = CommitSubject::new();
        let mut conf = GitCheckConfiguration::new();

        conf.add_check(&check);

        let result = test_check("test_check_subject", BAD_TOPIC, &conf);

        assert_eq!(result.warnings().len(), 0);
        assert_eq!(result.alerts().len(), 0);
        assert_eq!(result.errors().len(), 11);
        assert_eq!(result.errors()[0],
                   "commit 5f7284fe1599265c90550b681a4bf0763bc1de21 cannot be merged; it is marked \
                    as a commit to be squashed.");
        assert_eq!(result.errors()[1],
                   "commit 54d673ff559a72ce6343bd9526a950d79034b24e cannot be merged; it is marked \
                    as a fixup commit.");
        assert_eq!(result.errors()[2],
                   "commit 11dbbbff3f32445d74d1a8d96df0a49381c81ba0 cannot be merged; it is marked \
                    as a work-in-progress (WIP).");
        assert_eq!(result.errors()[3],
                   "commit 9039b9a4813fa019229e960033fe1ae8514a0c8e cannot be merged; it is marked \
                    as a work-in-progress (WIP).");
        assert_eq!(result.errors()[4],
                   "commit e478f630b586e331753477eba88059d644927be8 cannot be merged; it is marked \
                    as a work-in-progress (WIP).");
        assert_eq!(result.errors()[5],
                   "commit 3a6fe6d56fbf11c667b6c88bdd7d851a8dcac0b1 has an invalid commit subject; \
                    the second line must be empty.");
        assert_eq!(result.errors()[6],
                   "commit 3a6fe6d56fbf11c667b6c88bdd7d851a8dcac0b1 has an invalid commit subject; \
                    the third line must not be empty.");
        assert_eq!(result.errors()[7],
                   "commit b1ca628043ed78625551420e2dbbd8cf74fde2c4 has an invalid commit subject; \
                    the second line must be empty.");
        assert_eq!(result.errors()[8],
                   "commit b1ca628043ed78625551420e2dbbd8cf74fde2c4 has an invalid commit subject; \
                    it cannot be exactly two lines.");
        assert_eq!(result.errors()[9],
                   "commit 1afc6b3584580488917fc61aa5e5298e98583805 has an invalid commit subject; \
                    the first line must be no longer than 78 characters.");
        assert_eq!(result.errors()[10],
                   "commit 234de3c3f17ab29f0b7644ae96242e31a3dd634c has an invalid commit subject; \
                    the first line must be at least 8 characters.");
        assert_eq!(result.temporary(), false);
        assert_eq!(result.allowed(), false);
        assert_eq!(result.pass(), false);
    }
}