Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active February 15, 2019 23:34
Show Gist options
  • Save mlms13/82bed7552a7dcfe771009ff2c013ed8e to your computer and use it in GitHub Desktop.
Save mlms13/82bed7552a7dcfe771009ff2c013ed8e to your computer and use it in GitHub Desktop.
Categorize Git Commits
module Git = {
type commitish = Sha(string);
type commit = {
title: string,
descriptiong: string,
author: string,
date: Date.t
};
let getCommitsForRange: (commitish, commitish) => list(commit);
};
module Category = {
type t =
| Feature
| Bugfix
| Performance
| Design
| Docs;
};
module Rule = {
type pattern =
| TitleStartsWith(string)
| TitleEndsWith(string)
| TitleContains(string);
type t =
| Pattern(pattern)
| And(list(t))
| Or(list(t));
let evaluate: (string, t) => bool;
};
module CategoryRule = {
type t = {
category: Category.t,
rule: Rule.t,
};
let make = (category, rule) => {category, rule};
let bugfix = make(Category.Bugfix);
let perf = make(Category.Performance);
let evaulate = (txt, {category, rule}) =>
Rule.evaluate(txt, rule) ? Some(category) : None;
};
module Changelog = {
type t = Map.t(Category.t, list(Git.commit));
let fromCommits: ((Git.commit => option(Category.t)), list(Git.commit)) => t;
};
let categorize = (rules, commit) =>
List.foldLeft(
(category, rule) =>
switch (category) {
| Some(cat) => Some(cat)
| None => CategoryRule.evaluate(commit, rule)
}
None,
rules,
);
/*******************************************************************************
* Example usage
******************************************************************************/
let bugRule = Rule.(or([
pattern(startsWith(":bug:")),
pattern(cotains("fixes #"))
]);
let perfRule = Rule.(pattern(startsWith(":rocket:")));
let myRules = [bugRule, perfRule];
Git.(getCommitsForRange(sha("abcdef"), sha("012345")))
|> Changelog.fromCommits(categorize(myRules));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment