| 122 | } |
| 123 | |
| 124 | func (Git) commitMsg(messageFile string) error { |
| 125 | if mg.Verbose() { |
| 126 | fmt.Println("Running commit-msg hook") |
| 127 | } |
| 128 | if messageFile == "" { |
| 129 | messageFile = ".git/COMMIT_EDITMSG" |
| 130 | } |
| 131 | f, err := os.Open(messageFile) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | defer f.Close() |
| 136 | s := bufio.NewScanner(f) |
| 137 | s.Scan() |
| 138 | commitMsg := s.Text() |
| 139 | |
| 140 | if commitMsg == "" { |
| 141 | return errors.New("commit message must not be empty") |
| 142 | } |
| 143 | |
| 144 | if strings.HasPrefix(commitMsg, "fixup! ") || strings.HasPrefix(commitMsg, "Merge ") { |
| 145 | return nil |
| 146 | } |
| 147 | |
| 148 | // Check length: |
| 149 | switch { |
| 150 | case len(commitMsg) > 72: |
| 151 | return errors.New("commit message must be shorter than 72 characters") |
| 152 | case len(commitMsg) > 50: |
| 153 | // TODO: Warn. |
| 154 | } |
| 155 | |
| 156 | // Check topics: Message structure: |
| 157 | split := strings.SplitN(commitMsg, ": ", 2) |
| 158 | if len(split) != 2 { |
| 159 | return fmt.Errorf("commit message must contain topics from %s", |
| 160 | strings.Join(gitCommitPrefixes, ",")) |
| 161 | } |
| 162 | |
| 163 | // Check topics: |
| 164 | topics := strings.Split(split[0], ",") |
| 165 | var unknownTopics []string |
| 166 | nextTopic: |
| 167 | for _, topic := range topics { |
| 168 | for _, allowed := range gitCommitPrefixes { |
| 169 | if strings.TrimSpace(topic) == allowed { |
| 170 | continue nextTopic |
| 171 | } |
| 172 | } |
| 173 | unknownTopics = append(unknownTopics, topic) |
| 174 | } |
| 175 | if len(unknownTopics) > 0 { |
| 176 | return fmt.Errorf("commit messages must only topics from %s (and not %s)", |
| 177 | strings.Join(gitCommitPrefixes, ","), |
| 178 | strings.Join(unknownTopics, ",")) |
| 179 | } |
| 180 | |
| 181 | words := strings.Fields(split[1]) |