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
use crate::commit::{Commit, CommitType};
use crate::repository::Repository;
use anyhow::Result;
use colored::*;
use git2::Commit as Git2Commit;
use semver::Version;

pub enum VersionIncrement {
    Major,
    Minor,
    Patch,
    Auto,
    Manual(String),
}

impl VersionIncrement {
    pub(crate) fn bump(&self, current_version: &Version) -> Result<Version> {
        match self {
            VersionIncrement::Manual(version) => {
                Version::parse(&version).map_err(|err| anyhow!(err))
            }
            VersionIncrement::Auto => self.get_auto_version(current_version),
            VersionIncrement::Major => {
                let mut next = current_version.clone();
                next.increment_major();
                Ok(next)
            }
            VersionIncrement::Patch => {
                let mut next = current_version.clone();
                next.increment_patch();
                Ok(next)
            }
            VersionIncrement::Minor => {
                let mut next = current_version.clone();
                next.increment_minor();
                Ok(next)
            }
        }
    }

    fn get_auto_version(&self, current_version: &Version) -> Result<Version> {
        let mut next_version = current_version.clone();
        let repository = Repository::open(".")?;
        let changelog_start_oid = repository
            .get_latest_tag_oid()
            .unwrap_or_else(|_| repository.get_first_commit().unwrap());

        let head = repository.get_head_commit_oid()?;

        let commits = repository.get_commit_range(changelog_start_oid, head)?;

        let commits: Vec<&Git2Commit> = commits
            .iter()
            .filter(|commit| !commit.message().unwrap_or("").starts_with("Merge "))
            .collect();

        for commit in commits {
            let commit = Commit::from_git_commit(&commit);

            // TODO: prompt for continue on err
            if let Err(err) = commit {
                eprintln!("{}", err);
            } else {
                let commit = commit.unwrap();
                match (
                    &commit.message.commit_type,
                    commit.message.is_breaking_change,
                ) {
                    (CommitType::Feature, false) => {
                        next_version.increment_minor();
                        println!(
                            "Found feature commit {}, bumping to {}",
                            commit.shorthand().blue(),
                            next_version.to_string().green()
                        )
                    }
                    (CommitType::BugFix, false) => {
                        next_version.increment_patch();
                        println!(
                            "Found bug fix commit {}, bumping to {}",
                            commit.shorthand().blue(),
                            next_version.to_string().green()
                        )
                    }
                    (commit_type, true) => {
                        next_version.increment_major();
                        println!(
                            "Found {} commit {} with type : {}",
                            "BREAKING CHANGE".red(),
                            commit.shorthand().blue(),
                            commit_type.get_key_str().yellow()
                        )
                    }
                    (_, false) => println!(
                        "Skipping irrelevant commit {} with type : {}",
                        commit.shorthand().blue(),
                        commit.message.commit_type.get_key_str().yellow()
                    ),
                }
            }
        }

        Ok(next_version)
    }
}

#[cfg(test)]
mod test {
    use crate::version::VersionIncrement;
    use anyhow::Result;
    use semver::Version;

    // Auto version tests resides in test/ dir since it rely on git log
    // To generate the version

    #[test]
    fn major_bump() -> Result<()> {
        let version = VersionIncrement::Major.bump(&Version::new(1, 0, 0))?;
        assert_eq!(version, Version::new(2, 0, 0));
        Ok(())
    }

    #[test]
    fn minor_bump() -> Result<()> {
        let version = VersionIncrement::Minor.bump(&Version::new(1, 0, 0))?;
        assert_eq!(version, Version::new(1, 1, 0));
        Ok(())
    }

    #[test]
    fn patch_bump() -> Result<()> {
        let version = VersionIncrement::Patch.bump(&Version::new(1, 0, 0))?;
        assert_eq!(version, Version::new(1, 0, 1));
        Ok(())
    }
}