workerlib/
git_gc.rs

1//!
2//! Git binary for git cleanup operations
3//!
4
5use std::path::Path;
6
7use crate::execute::{Command, CommandBuilder};
8
9const DEFAULT_GC_EXPIRY: &str = "1d";
10
11/// Git Garbage Collection
12pub struct Gc<'a> {
13    /// Path to Lorry's git config
14    pub config_path: &'a Path,
15    /// Earliest date to prune temporary objects from
16    pub prune_expiry_date: Option<&'a str>,
17    /// If the --auto flag should be enabled, typically should be false since
18    /// if it is enabled prune is unlikely to be ran frequently.
19    pub auto: bool,
20}
21
22impl CommandBuilder for Gc<'_> {
23    fn build(&self, current_dir: &Path) -> tokio::process::Command {
24        let mut cmd = Command::new("git");
25        cmd.current_dir(current_dir);
26        cmd.envs([(
27            crate::git_config::GIT_CONFIG_GLOBAL,
28            self.config_path.to_string_lossy().as_ref(),
29        )]);
30        let expiry = self.prune_expiry_date.unwrap_or(DEFAULT_GC_EXPIRY);
31        let prune_arg = format!("--prune={expiry}");
32        let mut args = vec!["gc", "--quiet", &prune_arg];
33        if self.auto {
34            args.push("--auto")
35        }
36        cmd.args(args);
37        cmd
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::test_server::{TestBuilder, DEFAULT_WORKSPACE};
44
45    use super::*;
46
47    #[tokio::test]
48    pub async fn run_git_gc_bare() {
49        let test = TestBuilder::default().workspace(DEFAULT_WORKSPACE);
50        let gc = Gc {
51            config_path: Path::new("/dev/null"),
52            prune_expiry_date: None,
53            auto: false,
54        };
55        gc.build(test.workspace.unwrap().repository_path().as_path())
56            .output()
57            .await
58            .unwrap();
59    }
60}