gitru 0.2.11

a lightweight, configurable Git commit message validation tool
Documentation
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
//! Git repository type detection and path conventions
//!
//! This module models three kinds of Git repositories based on Git’s actual
//! filesystem behavior:
//!
//! - **Normal repository**: the working directory contains a `.git/` directory.
//! - **Worktree repository**: the working directory contains a `.git` file
//!   pointing to `.git/worktrees/<name>`. Hooks still reside in the main
//!   repository’s `.git/hooks`. The `commondir` file points back to the main
//!   `.git` directory.
//! - **Submodule repository**: the working directory contains a `.git` file
//!   pointing to `.git/modules/<submodule_name>`. Hooks live inside
//!   `.git/modules/<submodule_name>/hooks`. The parent repository’s `.git`
//!   directory is treated as the `super_git_dir`.
//!
//! Field conventions:
//! - `GitKind::NormalRepo.git_dir` always points to the repository’s own `.git` directory.
//! - `GitKind::Worktree.git_dir` points to the worktree’s gitdir (`.git/worktrees/<name>`).
//! - `GitKind::Worktree.main_git_dir` points to the main repository’s `.git` directory.
//! - `GitKind::Submodule.git_dir` points to the submodule’s gitdir (`.git/modules/<name>`).
//! - `GitKind::Submodule.super_git_dir` points to the parent repository’s `.git` directory.
//!
//! Provided functionality:
//! - `detect_git_kind(path)` detects whether the repository is a normal repo,
//!   worktree, or submodule by inspecting the `.git` file/directory.
//! - `GitKind::hook_path(hook_name)` returns the actual hook installation path.
//! - `GitKind::config_path(repo, file_name)` returns the configuration file path
//!   located in the working directory root.
//!
//! Design goals:
//! - Avoid relying on Git version–specific behavior.
//! - Centralize repository‑type detection and path derivation logic.
//! - Ensure hook and config paths behave consistently across all repo types.

use crate::error::git_error::GitKindError;
use std::fs;
use std::path::{Path, PathBuf};

pub enum GitKind {
    NormalRepo {
        git_dir: PathBuf,
        workdir: PathBuf,
    },
    Worktree {
        git_dir: PathBuf,
        main_git_dir: PathBuf,
        workdir: PathBuf,
    },
    Submodule {
        git_dir: PathBuf,
        super_git_dir: PathBuf,
        workdir: PathBuf,
    },
}

impl GitKind {
    /// Returns the working directory of this repository.
    pub fn workdir(&self) -> &Path {
        match self {
            GitKind::NormalRepo { workdir, .. } => workdir,
            GitKind::Worktree { workdir, .. } => workdir,
            GitKind::Submodule { workdir, .. } => workdir,
        }
    }

    /// Returns the root directory where Git hooks are installed,
    /// following Git’s actual behavior for normal repos, worktrees, and submodules.
    pub fn hooks_root(&self) -> PathBuf {
        match self {
            GitKind::NormalRepo { git_dir, .. } => git_dir.join("hooks"),
            GitKind::Worktree { main_git_dir, .. } => main_git_dir.join("hooks"),
            GitKind::Submodule { git_dir, .. } => git_dir.join("hooks"),
        }
    }

    /// Returns the full installation path of a specific Git hook.
    /// The hook location depends on the repository type.
    pub fn hook_path(&self, hook_name: &str) -> PathBuf {
        self.hooks_root().join(hook_name)
    }

    /// Returns the installation path of a configuration file.
    /// Config files are always stored at the root of the working directory.
    pub fn config_path(&self, file_name: &str) -> PathBuf {
        self.workdir().join(file_name)
    }
}

pub fn detect_current_repo() -> Result<GitKind, GitKindError> {
    detect_git_kind(".")
}

pub fn detect_git_kind(path: impl AsRef<Path>) -> Result<GitKind, GitKindError> {
    let workdir = find_workdir(path)?;
    let workdir = workdir.canonicalize()?;
    let git_path = workdir.join(".git");

    // 1) Normal repository
    if git_path.is_dir() {
        return Ok(GitKind::NormalRepo {
            git_dir: git_path.canonicalize()?,
            workdir,
        });
    }

    // 2) Worktree or submodule
    let content = fs::read_to_string(&git_path)?;
    let gitdir_raw = content
        .strip_prefix("gitdir:")
        .ok_or(GitKindError::InvalidGitDir)?
        .trim();

    let gitdir = resolve_gitdir(&git_path, gitdir_raw)?.canonicalize()?;

    // 2.1 Worktree
    let commondir_path = gitdir.join("commondir");
    if commondir_path.exists() {
        let commondir_raw = fs::read_to_string(&commondir_path)?.trim().to_string();
        let main_git_dir = resolve_gitdir(&gitdir, &commondir_raw)?.canonicalize()?;

        return Ok(GitKind::Worktree {
            git_dir: gitdir.clone(),
            main_git_dir,
            workdir,
        });
    }

    // 2.2 Submodule
    if gitdir.to_string_lossy().contains("/.git/modules/") {
        let super_git_dir = gitdir
            .ancestors()
            .find(|p| p.ends_with(".git"))
            .ok_or(GitKindError::InvalidPath)?
            .canonicalize()?;

        return Ok(GitKind::Submodule {
            git_dir: gitdir.clone(),
            super_git_dir,
            workdir,
        });
    }

    // Fallback: treat as a normal repository
    Ok(GitKind::NormalRepo {
        git_dir: gitdir,
        workdir,
    })
}

fn find_workdir(start: impl AsRef<Path>) -> Result<PathBuf, GitKindError> {
    let mut dir = start.as_ref().canonicalize()?;

    loop {
        if dir.join(".git").exists() {
            return Ok(dir);
        }

        match dir.parent() {
            Some(parent) => dir = parent.to_path_buf(),
            None => return Err(GitKindError::InvalidPath),
        }
    }
}

/// Resolves a gitdir path that may be relative to the `.git` file location.
fn resolve_gitdir(base: &Path, gitdir: &str) -> Result<PathBuf, GitKindError> {
    let p = PathBuf::from(gitdir);
    if p.is_absolute() {
        Ok(p)
    } else {
        let parent = base.parent().ok_or(GitKindError::InvalidPath)?;
        Ok(parent.join(p))
    }
}

#[cfg(test)]
mod tests {
    use crate::util::git_path::{GitKind, detect_git_kind};

    use std::fs;
    use std::path::Path;
    use std::process::Command;
    use tempfile::TempDir;

    fn run(cmd: &mut Command) {
        let status = cmd.status().expect("failed to run command");
        assert!(status.success());
    }

    /// Initialize a Git repository and create a `master` branch
    fn init_repo_with_master(path: &Path) {
        run(Command::new("git").arg("init").arg(path));

        // Create the master branch
        run(Command::new("git")
            .current_dir(path)
            .arg("checkout")
            .arg("-b")
            .arg("master"));

        // Commit at least one file (submodules require an initial commit)
        fs::write(path.join("init.txt"), "init").unwrap();
        run(Command::new("git").current_dir(path).arg("add").arg("."));
        run(Command::new("git")
            .current_dir(path)
            .arg("commit")
            .arg("-m")
            .arg("init"));
    }

    #[test]
    #[ignore]
    fn test_normal_repo() {
        let dir = TempDir::new().unwrap();
        init_repo_with_master(dir.path());

        let kind = detect_git_kind(dir.path()).unwrap();

        match kind {
            GitKind::NormalRepo { git_dir, workdir } => {
                assert!(git_dir.join("HEAD").exists());
                assert!(workdir.join(".git").exists());
            }
            _ => panic!("should detect NormalRepo"),
        }
    }

    #[test]
    #[ignore]
    fn test_worktree() {
        let main = TempDir::new().unwrap();
        let wt = TempDir::new().unwrap();

        // Initialize the main repository (with master)
        init_repo_with_master(main.path());

        // Create a new branch for the worktree
        run(Command::new("git")
            .current_dir(main.path())
            .arg("branch")
            .arg("wt-branch"));

        // Add a worktree using the new branch
        run(Command::new("git")
            .current_dir(main.path())
            .arg("worktree")
            .arg("add")
            .arg(wt.path())
            .arg("wt-branch"));

        let kind = detect_git_kind(wt.path()).unwrap();

        match kind {
            GitKind::Worktree {
                git_dir,
                main_git_dir,
                workdir: _,
            } => {
                assert!(git_dir.join("commondir").exists());
                println!("git_dir: {:?}", git_dir);
                println!("main_git_dir: {:?}", main_git_dir);
                let canonical = main_git_dir.canonicalize().unwrap();
                println!("canonical: {:?}", canonical);
                assert!(canonical.join(".git").join("HEAD").exists());
            }
            _ => panic!("should detect Worktree"),
        }
    }

    #[test]
    #[ignore]
    fn test_submodule() {
        let super_repo = TempDir::new().unwrap();

        // The sub_repo must be inside the super_repo
        let sub_repo = super_repo.path().join("sub_src");
        fs::create_dir(&sub_repo).unwrap();

        // Initialize the super repo
        init_repo_with_master(super_repo.path());

        // Initialize the sub repo (must have at least one commit)
        init_repo_with_master(&sub_repo);

        // ⭐ Use ./sub_src (important)
        // Allow file:// protocol (Git 2.38+ disables it by default)
        run(Command::new("git")
            .current_dir(super_repo.path())
            .arg("-c")
            .arg("protocol.file.allow=always")
            .arg("submodule")
            .arg("add")
            .arg("./sub_src")
            .arg("sub"));

        let sub_path = super_repo.path().join("sub");
        let kind = detect_git_kind(&sub_path).unwrap();

        match kind {
            GitKind::Submodule {
                git_dir,
                super_git_dir,
                workdir: _,
            } => {
                assert!(git_dir.ends_with("modules/sub"));
                assert!(super_git_dir.ends_with(".git"));
            }
            _ => panic!("should detect Submodule"),
        }
    }

    // hook_path / config_path for a Normal Repo
    #[test]
    #[ignore]
    fn test_hook_and_config_normal_repo() {
        let repo_dir = TempDir::new().unwrap();
        init_repo_with_master(repo_dir.path());

        let kind = detect_git_kind(repo_dir.path()).unwrap();

        // hook_path
        let hook = kind.hook_path("commit-msg");
        assert_eq!(
            hook,
            repo_dir
                .path()
                .join(".git")
                .join("hooks")
                .join("commit-msg")
        );

        // config_path
        let cfg = kind.config_path("gitru.toml");
        assert_eq!(cfg, repo_dir.path().join("gitru.toml"));
    }

    // hook_path / config_path for a Worktree
    #[test]
    #[ignore]
    fn test_hook_and_config_worktree() {
        let main = TempDir::new().unwrap();
        init_repo_with_master(main.path());

        // Create a worktree
        let wt = main.path().join("wt");

        run(Command::new("git")
            .current_dir(main.path())
            .arg("worktree")
            .arg("add")
            .arg("-b")
            .arg("wt-branch")
            .arg(&wt));

        let kind = detect_git_kind(&wt).unwrap();

        match kind {
            GitKind::Worktree {
                ref main_git_dir, ..
            } => {
                // hook_path
                let hook = kind.hook_path("commit-msg");
                assert_eq!(hook, main_git_dir.join("hooks").join("commit-msg"));
                println!("hook: {:?}", hook);
                println!("main_git_dir: {:?}", main_git_dir);

                // config_path
                let cfg = kind.config_path("gitru.toml");
                println!("cfg: {:?}", cfg);
                assert_eq!(cfg, wt.join("gitru.toml"));
            }
            _ => panic!("should detect Worktree"),
        }
    }

    // hook_path / config_path for a Submodule
    #[test]
    #[ignore]
    fn test_hook_and_config_submodule() {
        let super_repo = TempDir::new().unwrap();
        init_repo_with_master(super_repo.path());

        // The submodule must be inside the super_repo
        let sub_src = super_repo.path().join("sub_src");
        fs::create_dir(&sub_src).unwrap();
        init_repo_with_master(&sub_src);

        // Add the submodule (important: use ./sub_src + temporarily enable file protocol)
        run(Command::new("git")
            .current_dir(super_repo.path())
            .arg("-c")
            .arg("protocol.file.allow=always")
            .arg("submodule")
            .arg("add")
            .arg("./sub_src")
            .arg("sub"));

        let sub_path = super_repo.path().join("sub");
        let kind = detect_git_kind(&sub_path).unwrap();

        match &kind {
            GitKind::Submodule { git_dir, .. } => {
                // hook_path
                let hook = kind.hook_path("commit-msg");
                println!("hook: {:?}", hook);
                assert_eq!(hook, git_dir.join("hooks").join("commit-msg"));

                // config_path
                let cfg = kind.config_path("gitru.toml");
                println!("cfg: {:?}", cfg);
                assert_eq!(cfg, sub_path.join("gitru.toml"));
            }
            _ => panic!("should detect Submodule"),
        }
    }
}