git-hooks-manager 0.4.2

An attempt to make sharing git hooks among team members easier
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use std::collections::HashMap;
use std::env;
use std::fs::{File, Permissions};
use std::io::{stdin, stdout, Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

use clap::{App, Arg, SubCommand};
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use shlex::Shlex;

use crate::utils::{execute_cmd, get_files, get_local_repo_path, matches, prefix_path};

mod git;
mod utils;

#[cfg(test)]
mod tests {
    use crate::{git, ExternalHookRepo, Hook, HookConfig, HookEvent};
    use std::env::{current_dir, set_current_dir};
    use tempdir::TempDir;

    #[test]
    fn test_merge() {
        let mut conf = HookConfig {
            hooks: vec![Hook {
                name: "test1".to_string(),
                on_event: None,
                on_file_regex: None,
                action: Some("exe2".to_string()),
                setup_script: None,
            }],
            repos: vec![ExternalHookRepo {
                url: "dummy".to_string(),
                hooks: vec![Hook {
                    name: "test1".to_string(),
                    on_event: Some(vec![HookEvent::PreCommit]),
                    on_file_regex: Some(vec![".*".to_string()]),
                    action: Some("exe1".to_string()),
                    setup_script: Some("hello.sh".to_string()),
                }],
                version: None,
            }],
        };
        assert_ne!(conf.hooks[0].action, conf.repos[0].hooks[0].action);
        conf.update_repos_config();
        assert_eq!(conf.hooks[0].action, conf.repos[0].hooks[0].action);
    }

    #[test]
    fn test_external_repo_with_version() {
        let dir = TempDir::new("git-hooks-tests").expect("could not create tempdir");
        let old_dir = current_dir().expect("could not get current dir");
        set_current_dir(dir.path()).expect("could not cd to temp dir");
        git::init(None).expect("could not init repo");
        let mut er = ExternalHookRepo {
            url: "https://github.com/paulollivier/rust-hooks".to_string(),
            version: Some("0e74c2b9c6b1cf4ff36d7eedbee8e8093acacaac".to_string()),
            hooks: vec![],
        };
        let r = er.init();
        assert!(r.is_ok());
        let cloned_dir = dir
            .path()
            .join(".git")
            .join("hook-repos")
            .join("rust-hooks");
        assert!(cloned_dir.join("hooks.yml").exists());
        set_current_dir(cloned_dir).expect("could not cd to cloned dir");
        let r = git::get_hash("HEAD");
        assert!(r.is_ok());
        assert_eq!(
            "0e74c2b9c6b1cf4ff36d7eedbee8e8093acacaac".to_string(),
            r.unwrap()
        );
        set_current_dir(old_dir).expect("could not revert current dir");
    }
}

/// Represents the possible placeholders to be substituted to actual file values.
/// The singular variants mean that the action is to be executed for each file found.
enum ActionFileToken {
    Files,
    File,
    ChangedFiles,
    ChangedFile,
    Root,
}

impl ActionFileToken {
    /// Returns the variant from a textual representation
    /// ```rust
    /// assert_eq!(ActionFileToken::File, ActionFileToken::from_str("{file}"));
    /// assert_eq!(ActionFileToken::ChangedFiles, ActionFileToken::from_str("{changed_files}"));
    /// ```
    fn from_str(token: &str) -> Option<ActionFileToken> {
        match token {
            "{file}" => Some(ActionFileToken::File),
            "{files}" => Some(ActionFileToken::Files),
            "{changed_files}" => Some(ActionFileToken::ChangedFiles),
            "{changed_file}" => Some(ActionFileToken::ChangedFile),
            "{root}" => Some(ActionFileToken::Root),
            _ => None,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Copy, Clone)]
#[serde(rename_all = "kebab-case")]
enum HookEvent {
    ApplyPatchMsg,
    CommitMsg,
    PostCommit,
    PostUpdate,
    PreApplyPatch,
    PreCommit,
    PreMergeCommit,
    PrePush,
    PreRebase,
    PreReceive,
    PrepareCommitMsg,
    Update,
}

static ALL_HOOK_EVENTS: &[HookEvent] = &[
    HookEvent::ApplyPatchMsg,
    HookEvent::CommitMsg,
    HookEvent::PostCommit,
    HookEvent::PostUpdate,
    HookEvent::PreApplyPatch,
    HookEvent::PreCommit,
    HookEvent::PreMergeCommit,
    HookEvent::PrePush,
    HookEvent::PreRebase,
    HookEvent::PreReceive,
    HookEvent::PrepareCommitMsg,
    HookEvent::Update,
];

impl HookEvent {
    fn to_kebab_case(&self) -> &'static str {
        match self {
            HookEvent::ApplyPatchMsg => "apply-patch-msg",
            HookEvent::CommitMsg => "commit-msg",
            HookEvent::PostCommit => "post-commit",
            HookEvent::PostUpdate => "post-update",
            HookEvent::PreApplyPatch => "pre-apply-patch",
            HookEvent::PreCommit => "pre-commit",
            HookEvent::PreMergeCommit => "pre-merge-commit",
            HookEvent::PrePush => "pre-push",
            HookEvent::PreRebase => "pre-rebase",
            HookEvent::PreReceive => "pre-receive",
            HookEvent::PrepareCommitMsg => "prepare-commit-msg",
            HookEvent::Update => "update",
        }
    }
    fn from_kebab_case(s: &str) -> Option<Self> {
        match s {
            "apply-patch-msg" => Some(HookEvent::ApplyPatchMsg),
            "commit-msg" => Some(HookEvent::CommitMsg),
            "post-commit" => Some(HookEvent::PostCommit),
            "post-update" => Some(HookEvent::PostUpdate),
            "pre-apply-patch" => Some(HookEvent::PreApplyPatch),
            "pre-commit" => Some(HookEvent::PreCommit),
            "pre-merge-commit" => Some(HookEvent::PreMergeCommit),
            "pre-push" => Some(HookEvent::PrePush),
            "pre-rebase" => Some(HookEvent::PreRebase),
            "pre-receive" => Some(HookEvent::PreReceive),
            "prepare-commit-msg" => Some(HookEvent::PrepareCommitMsg),
            "update" => Some(HookEvent::Update),
            _ => None,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(default)]
struct Hook {
    name: String,
    on_event: Option<Vec<HookEvent>>,
    on_file_regex: Option<Vec<String>>,
    action: Option<String>,
    setup_script: Option<String>,
}

impl Clone for Hook {
    fn clone(&self) -> Self {
        let mut h = Hook::default();
        h.name = self.name.clone();
        if let Some(self_on_event) = &self.on_event {
            let mut on_event = Vec::new();
            for e in self_on_event {
                on_event.push(*e);
            }
            h.on_event = Some(on_event);
        }
        if let Some(regex) = &self.on_file_regex {
            let mut on_file_regex = Vec::new();
            for r in regex {
                on_file_regex.push(r.clone());
            }
            h.on_file_regex = Some(on_file_regex);
        }
        if let Some(action) = &self.action {
            h.action = Some(action.clone());
        }
        if let Some(setup_script) = &self.setup_script {
            h.setup_script = Some(setup_script.clone());
        }
        h
    }
}

fn run_hook(hook: &Hook, hook_repo_path: &str) -> anyhow::Result<()> {
    let root = git::root().expect("Could not get git root.");
    let mut should_run = true;
    // expand PATH
    let mut bin_path = env::var("PATH").expect("PATH is not set in the env.");
    bin_path.push_str(&format!(":{}", hook_repo_path));
    debug!("New $PATH: {}", &bin_path);
    let mut env = HashMap::new();
    env.insert("PATH".to_string(), bin_path);
    // parse the action cli
    let mut action = Shlex::new(
        hook.action
            .as_ref()
            .expect("None action on hook exec")
            .as_str(),
    );
    let cmd = action.next().unwrap();
    let args: Vec<String> = action.collect();
    let mut final_args: Vec<String> = Vec::new();
    for arg in &args {
        if let Some(token) = ActionFileToken::from_str(&arg) {
            match token {
                ActionFileToken::Files => {
                    let mut files = get_files(
                        &root,
                        &hook
                            .on_file_regex
                            .as_ref()
                            .unwrap_or(&vec![".*".to_string()]),
                    )?;
                    should_run = !files.is_empty();
                    final_args.append(&mut files);
                }
                ActionFileToken::File => {
                    unimplemented!("we should check for the token before, as it changes the whole execution logic");
                }
                ActionFileToken::ChangedFiles => {
                    let mut changed_files: Vec<String> = git::changed_files(true)?
                        .iter()
                        .map(|f| Path::new(f))
                        .filter(|p| {
                            matches(
                                p,
                                &(*hook
                                    .on_file_regex
                                    .as_ref()
                                    .unwrap_or(&vec![".*".to_string()])),
                            )
                        })
                        .map(|p| p.display().to_string())
                        .collect();
                    should_run = !changed_files.is_empty();
                    final_args.append(&mut changed_files);
                }
                ActionFileToken::ChangedFile => {
                    // TODO: implement me
                    unimplemented!();
                }
                ActionFileToken::Root => {
                    final_args.push(root.clone());
                }
            }
        } else if should_run {
            final_args.push(arg.to_string());
        } else {
            info!("Could find any files to run hook on");
        }
    }
    let (s, _, _) = execute_cmd(&cmd, &final_args, Some(&root), Some(&env))?;
    debug!(
        "finished executing {} with exit status {}",
        cmd,
        s.code().unwrap()
    );
    if !s.success() {
        Err(anyhow::Error::msg(format!(
            "{:?} reported execution failure: {:?}",
            hook,
            s.code()
        )))
    } else {
        let index_files = git::changed_files(true)?;
        let changed_files = git::changed_files(false)?;
        let files_to_re_add: Vec<&String> = changed_files
            .iter()
            .filter(|f| index_files.contains(f))
            .collect();
        if !files_to_re_add.is_empty() {
            debug!("we must re-add those files: {:#?}", files_to_re_add);
            git::add(&files_to_re_add)?;
        }
        Ok(())
    }
}

#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(default)]
struct ExternalHookRepo {
    hooks: Vec<Hook>,
    url: String,
    version: Option<String>,
}

impl ExternalHookRepo {
    pub fn init(&mut self) -> anyhow::Result<()> {
        let clone_dir = get_local_repo_path(&self.url)?;
        debug!("cloning {} to {}", &self.url, &clone_dir);
        git::pull(&self.url, &clone_dir)?;
        if let Some(v) = &self.version {
            git::checkout(v, &clone_dir)?;
        }
        let mut repo_config = String::new();
        File::open(format!("{}/{}", clone_dir, "hooks.yml"))?.read_to_string(&mut repo_config)?;
        debug!("Got hooks.yml");
        let hook_repo: ExternalHookRepo = serde_yaml::from_str(&repo_config)?;
        debug!("{:?}", hook_repo);
        self.hooks = hook_repo.hooks;
        self.setup()
    }

    /// runs the optional setup scripts
    fn setup(&self) -> anyhow::Result<()> {
        let mut env = HashMap::new();
        env.insert(
            "PATH".to_string(),
            prefix_path(&get_local_repo_path(&self.url)?),
        );
        for hook in &self.hooks {
            if hook.setup_script.is_some() {
                utils::execute_cmd(
                    hook.setup_script.as_ref().expect("should not happen"),
                    &[] as &[&str],
                    Some(&get_local_repo_path(&self.url)?),
                    Some(&env),
                )?;
            }
        }
        Ok(())
    }
}

#[derive(Deserialize, Serialize, Debug)]
struct HookConfig {
    repos: Vec<ExternalHookRepo>,
    hooks: Vec<Hook>,
}

impl HookConfig {
    fn from_file(filename: Option<&str>) -> anyhow::Result<HookConfig> {
        let mut conf_content = String::new();
        let p = filename.unwrap_or(".hooks.yml");
        match File::open(p) {
            Ok(mut f) => {
                f.read_to_string(&mut conf_content)?;
            }
            Err(e) => {
                error!("could not read config file {}: {}", p, e);
            }
        }
        let mut conf: HookConfig = serde_yaml::from_str(&conf_content)?;
        conf.update_repos_config();
        debug!("{:?}", conf);
        conf.repos
            .iter_mut()
            .map(|repo| {
                debug!("init {:?}", repo.url);
                let r = repo.init();
                if let Err(e) = r {
                    warn!(
                        "Got an error while attempting to initialize repo {}: {}",
                        repo.url, e
                    );
                }
            })
            .for_each(drop); // consume the iterator
        Ok(conf)
    }

    /// Installs itself as a hook
    fn init(self, events: &[HookEvent]) -> anyhow::Result<()> {
        for event in events {
            let mut hook_script = File::create(format!(
                "{}/.git/hooks/{}",
                git::root()?,
                event.to_kebab_case()
            ))?;
            hook_script.set_permissions(Permissions::from_mode(0o755))?;
            hook_script.write_all(
                format!("#!/bin/bash -e\ngit-hooks run {}\n", event.to_kebab_case()).as_bytes(),
            )?;
        }
        //TODO: create .hooks.yml if not existing?
        Ok(())
    }

    /// finds defined values in the hook definitions, and overrides the definitions in repos
    fn update_repos_config(&mut self) {
        // TODO error[E0500]: closure requires unique access to `self` but it is already borrowed
        let hooks = &self.hooks;
        self.repos
            .iter_mut()
            .map(|repo| {
                repo.hooks
                    .iter_mut()
                    .map(|h| {
                        let hooks: Vec<&Hook> =
                            hooks.iter().filter(|hook| hook.name == h.name).collect();
                        if !hooks.is_empty() {
                            let hook = hooks[0];
                            if h.name == hook.name {
                                if let Some(on_event) = &hook.on_event {
                                    h.on_event = Some(on_event.clone());
                                }
                                if let Some(on_file_regex) = &hook.on_file_regex {
                                    h.on_file_regex = Some(on_file_regex.clone());
                                }
                                if let Some(action) = &hook.action {
                                    h.action = Some(action.clone());
                                }
                                if let Some(setup_script) = &hook.setup_script {
                                    h.setup_script = Some(setup_script.clone());
                                }
                            }
                        }
                    })
                    .for_each(drop);
            })
            .for_each(drop);
    }
}

fn ask_for_user_confirmation(prompt: &str) -> anyhow::Result<bool> {
    print!("{}: ", prompt);
    stdout().flush()?;
    let mut input = String::new();
    stdin().read_line(&mut input)?;
    Ok(match input.trim() {
        "Y" | "y" => true,
        "N" | "n" => false,
        _ => {
            println!("Incorrect input. Try again.");
            ask_for_user_confirmation(prompt)?
        }
    })
}

fn update() -> anyhow::Result<()> {
    use self_update::cargo_crate_version;
    let status = self_update::backends::github::Update::configure()
        .repo_owner("paulollivier")
        .repo_name("git-hooks")
        .bin_name("git-hooks-linux-amd64")
        .show_download_progress(true)
        .current_version(cargo_crate_version!())
        .build()?
        .update()?;
    if status.updated() {
        println!("Downloaded a new version: `{}`!", status.version());
    } else {
        println!("No available update.");
    }
    Ok(())
}

fn main() -> anyhow::Result<()> {
    pretty_env_logger::try_init()?;
    let app = App::new("git-hooks")
        .author("Paul Ollivier <contact@paulollivier.fr>")
        .about("A git hooks manager\nhttps://github.com/paulollivier/git-hooks")
        .subcommand(SubCommand::with_name("self-update").about("git-hooks will try to update itself."))
        .subcommand(SubCommand::with_name("init").about("Install the git hooks in .git/hooks"))
        .subcommand(
            SubCommand::with_name("run")
                .about("Runs the configured hooks for a given event")
                .arg(Arg::with_name("event")
                    .index(1)
                    .help("Runs the hook for the given event, eg. \"pre-commit\", \"post-commit\"")
                    .required(true)
                    .possible_values(&ALL_HOOK_EVENTS.iter().map(|e| e.to_kebab_case()).collect::<Vec<&'static str>>())
                ),
        );
    let matches = app.get_matches();
    debug!("{:?}", matches);
    match matches.subcommand() {
        ("self-update", _) => {
            update()?;
        }
        ("init", _) => {
            debug!("reading conf");
            let conf = HookConfig::from_file(None)?;
            debug!("merged conf: {:#?}", conf);
            if ask_for_user_confirmation(
                "This will overwrite all the hooks in .git/hooks. Are you sure? [Y/N]",
            )? {
                conf.init(ALL_HOOK_EVENTS)?;
                println!("I have init'd myself successfully! 🚀");
            } else {
                println!("Operation cancelled by user.");
            }
        }
        ("run", args) => {
            debug!("reading conf");
            let conf = HookConfig::from_file(None)?;
            let active_hooks_names: Vec<String> =
                conf.hooks.iter().map(|h| h.name.clone()).collect();
            debug!("merged conf: {:#?}", conf);
            if let Some(arg_matches) = args {
                if let Some(event) = arg_matches.value_of("event") {
                    let mut has_executed_hook = false;
                    let mut had_error = false;
                    let event = HookEvent::from_kebab_case(event).expect(
                        "Could not unwrap event, although it should be present, thanks to clap",
                    );
                    conf.repos
                        .iter()
                        .map(|repo| {
                            repo.hooks
                                .iter()
                                // filter hooks with the right event
                                .filter(|&hook| {
                                    (*hook).on_event.as_ref().unwrap_or(&vec![HookEvent::PreCommit]).contains(&event)
                                })
                                // filter hooks with their IDs present.
                                .filter(|&hook| {
                                    active_hooks_names.contains(&hook.name)
                                })
                                .map(|hook| {
                                    debug!("would run hook {:?}", hook);
                                    if let Err(e) = run_hook(&hook,
                                                             &get_local_repo_path(&repo.url)
                                                                 .expect("could not get local root repo when attempting to run hook")) {
                                        warn!(
                                            "An error occurred while executing {}: {}",
                                            hook.name, e
                                        );
                                        had_error = true;
                                    }
                                    has_executed_hook = true;
                                }).for_each(drop);
                        })
                        .for_each(drop);
                    if !has_executed_hook {
                        info!("Nothing to do.");
                    }
                    if had_error {
                        return Err(anyhow::Error::msg("a hook reported malfunction"));
                    }
                }
            }
        }
        _ => {
            // Should not happen, clap handles this
            error!("A subcommand must be set! see help (-h)");
            return Err(anyhow::Error::msg("no command given"));
        }
    };
    Ok(())
}