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
use chrono::prelude::*;
use git2::Repository;
use pathdiff;

use crate::{config::Config, entry::Entry, errors::DiaryError, utils::git};
pub struct CommitOptions {
    /// The date of the entry to open.
    pub entry_date: Date<Local>,
    pub message: String,
    pub push: bool,
}

pub fn commit(opts: &CommitOptions, config: &Config) -> Result<(), DiaryError> {
    config.initialised()?;

    let diary_entry = Entry::from_config(config)?;
    let entry_path = diary_entry.get_entry_path(&opts.entry_date);
    let relative_path = pathdiff::diff_paths(&entry_path, config.diary_path()).unwrap();

    let repo = Repository::open(config.diary_path())?;

    git::add_and_commit(&repo, &relative_path, &opts.message)?;

    if opts.push {
        git::push_to_origin(&repo)?
    };
    Ok(())
}

#[cfg(test)]

mod test {
    use chrono::prelude::*;
    use git2::Repository;

    use super::{commit, CommitOptions};
    use crate::{
        ops::{init, testing, InitOptions},
        utils::git,
    };

    #[test]
    fn commit_today() {
        let config = testing::temp_config();

        let init_opts = InitOptions {
            path: testing::temp_path(),
            prefix: None,
            git_repo: true,
        };
        init(&init_opts, &config).unwrap();

        let entry_date = Local.ymd(2022, 1, 13);
        testing::new_entry(&config, &entry_date);

        let opts = CommitOptions {
            entry_date,
            message: "Test message".to_string(),
            push: false,
        };
        let repo = Repository::open(config.diary_path()).unwrap();

        commit(&opts, &config).unwrap();

        let last_commit = git::find_last_commit(&repo).unwrap();
        commit(&opts, &config).unwrap();
        assert!(last_commit.is_some());

        let index = repo.index().unwrap();
        assert_eq!(index.len(), 1)
    }

    #[test]
    fn commit_multiple() {
        let config = testing::temp_config();

        let init_opts = InitOptions {
            path: testing::temp_path(),
            prefix: None,
            git_repo: true,
        };
        init(&init_opts, &config).unwrap();

        let entry_date = Local.ymd(2022, 1, 13);
        testing::new_entry(&config, &entry_date);

        let opts = CommitOptions {
            entry_date,
            message: "Test message".to_string(),
            push: false,
        };
        let repo = Repository::open(config.diary_path()).unwrap();

        commit(&opts, &config).unwrap();

        let last_commit = git::find_last_commit(&repo).unwrap();
        assert!(last_commit.is_some());

        let entry_date = Local.ymd(2022, 1, 14);
        testing::new_entry(&config, &entry_date);

        let opts = CommitOptions {
            entry_date,
            message: "Test message".to_string(),
            push: false,
        };
        let repo = Repository::open(config.diary_path()).unwrap();

        commit(&opts, &config).unwrap();

        let last_commit = git::find_last_commit(&repo).unwrap();
        assert!(last_commit.is_some());

        let index = repo.index().unwrap();
        assert_eq!(index.len(), 2)
    }

    #[test]
    #[should_panic(expected = "No such file or directory")]
    fn commit_no_entry() {
        let config = testing::temp_config();

        let init_opts = InitOptions {
            path: testing::temp_path(),
            prefix: None,
            git_repo: true,
        };
        init(&init_opts, &config).unwrap();

        let entry_date = Local.ymd(2022, 1, 13);

        let opts = CommitOptions {
            entry_date,
            message: "Test message".to_string(),
            push: false,
        };

        commit(&opts, &config).unwrap();
    }

    #[test]
    #[should_panic(expected = "remote 'origin' does not exist")]
    fn commit_and_fail_to_push() {
        let config = testing::temp_config();

        let init_opts = InitOptions {
            path: testing::temp_path(),
            prefix: None,
            git_repo: true,
        };
        init(&init_opts, &config).unwrap();

        let entry_date = Local.ymd(2022, 1, 13);

        testing::new_entry(&config, &entry_date);

        let opts = CommitOptions {
            entry_date,
            message: "Test message".to_string(),
            push: true,
        };

        commit(&opts, &config).unwrap();
    }
}