Skip to content

Commit 3bfc26f

Browse files
committed
wip
1 parent b942281 commit 3bfc26f

File tree

6 files changed

+141
-9
lines changed

6 files changed

+141
-9
lines changed

src/clientlib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,25 @@ pub(crate) struct ConfigHook;
5959
#[derive(Cfgv, Debug)]
6060
pub(crate) struct LocalRepo {
6161
#[cfgv_id]
62-
repo: String,
63-
hooks: Vec<ManifestHook>,
62+
pub(crate) repo: String,
63+
pub(crate) hooks: Vec<ManifestHook>,
6464
}
6565

6666
#[allow(dead_code)]
6767
#[derive(Cfgv, Debug)]
6868
pub(crate) struct MetaRepo {
6969
#[cfgv_id]
70-
repo: String,
71-
hooks: Vec<ConfigHook>,
70+
pub(crate) repo: String,
71+
pub(crate) hooks: Vec<ConfigHook>,
7272
}
7373

7474
#[allow(dead_code)]
7575
#[derive(Cfgv, Debug)]
7676
pub(crate) struct RemoteRepo {
7777
#[cfgv_id]
78-
repo: String,
79-
rev: String,
80-
hooks: Vec<ConfigHook>,
78+
pub(crate) repo: String,
79+
pub(crate) rev: String,
80+
pub(crate) hooks: Vec<ConfigHook>,
8181
}
8282

8383
#[allow(dead_code)]
@@ -107,7 +107,7 @@ impl Cfgv for Repo {
107107
#[allow(dead_code)]
108108
#[derive(Cfgv, Debug)]
109109
pub(crate) struct Config {
110-
repos: Vec<Repo>,
110+
pub(crate) repos: Vec<Repo>,
111111

112112
#[cfgv_default_expr(vec!["pre-commit".into()])]
113113
default_install_hook_types: Vec<String>,

src/commands/run.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::clientlib;
12
use crate::env_ext;
23
use crate::git;
4+
use crate::repository;
35
use crate::staged_files_only;
46
use crate::store;
57
use crate::PreCommitEnv;
@@ -35,10 +37,15 @@ pub(crate) fn cmd(
3537
if stash {
3638
ctx = Some(staged_files_only::StagedFilesOnly::new(
3739
&repo,
38-
store.directory,
40+
&store.directory,
3941
)?);
4042
}
4143

44+
let config = clientlib::load_config(&config)?;
45+
let hooks = repository::all_hooks(config, &store);
46+
dbg!(cmd.hook_stage);
47+
dbg!(hooks);
48+
4249
drop(ctx);
4350
Ok(())
4451
}

src/git.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,63 @@ pub(crate) fn has_unstaged_config(repo: &gix::Repository, config: &str) -> anyho
4444

4545
Ok(retc == 1)
4646
}
47+
48+
// from `git rev-parse --local-env-vars`
49+
// TODO: use gix for this?
50+
const _LOCAL_ENV_VARS: &[&str] = &[
51+
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
52+
"GIT_COMMON_DIR",
53+
"GIT_CONFIG",
54+
"GIT_CONFIG_COUNT",
55+
"GIT_CONFIG_PARAMETERS",
56+
"GIT_DIR",
57+
"GIT_GRAFT_FILE",
58+
"GIT_IMPLICIT_WORK_TREE",
59+
"GIT_INDEX_FILE",
60+
"GIT_INTERNAL_SUPER_PREFIX",
61+
"GIT_NO_REPLACE_OBJECTS",
62+
"GIT_OBJECT_DIRECTORY",
63+
"GIT_PREFIX",
64+
"GIT_REPLACE_REF_BASE",
65+
"GIT_SHALLOW_FILE",
66+
"GIT_WORK_TREE",
67+
];
68+
69+
pub(crate) trait GitNoLocalEnv {
70+
fn git_no_local_env(&mut self) -> &mut Self;
71+
}
72+
73+
impl GitNoLocalEnv for process::Command {
74+
fn git_no_local_env(&mut self) -> &mut Self {
75+
for var in _LOCAL_ENV_VARS {
76+
self.env_remove(var);
77+
}
78+
self
79+
}
80+
}
81+
82+
pub(crate) fn git_no_fs_monitor() -> process::Command {
83+
let mut ret = process::Command::new("git");
84+
ret.args(["-c", "core.useBuiltinFSMonitor=false"]);
85+
ret
86+
}
87+
88+
pub(crate) fn init_repo(path: &str, remote: &str) -> anyhow::Result<()> {
89+
// TODO:
90+
// if os.path.isdir(remote):
91+
// remote = os.path.abspath(remote)
92+
93+
// TODO: add this to gix?
94+
// avoid the user's template so that hooks do not recurse
95+
git_no_fs_monitor()
96+
.git_no_local_env()
97+
.args(["init", "--template=", path])
98+
.output()?;
99+
git_no_fs_monitor()
100+
.git_no_local_env()
101+
.current_dir(path)
102+
.args(["remote", "add", "origin", remote])
103+
.output()?;
104+
105+
Ok(())
106+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod clientlib;
99
mod commands;
1010
mod env_ext;
1111
mod git;
12+
mod repository;
1213
mod staged_files_only;
1314
mod store;
1415

src/repository.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::clientlib::{Config, LocalRepo, ManifestHook, MetaRepo, RemoteRepo, Repo};
2+
use crate::store::Store;
3+
use std::path;
4+
5+
#[derive(Debug)]
6+
pub(crate) struct Hook {
7+
src: String,
8+
prefix: path::PathBuf,
9+
hook: ManifestHook,
10+
}
11+
12+
fn _local_hooks(repo: LocalRepo, store: &Store) -> anyhow::Result<Vec<Hook>> {
13+
anyhow::bail!("not implemented!")
14+
}
15+
16+
fn _meta_hooks(repo: MetaRepo, store: &Store) -> anyhow::Result<Vec<Hook>> {
17+
anyhow::bail!("not implemented!")
18+
}
19+
20+
fn _cloned_hooks(repo: RemoteRepo, store: &Store) -> anyhow::Result<Vec<Hook>> {
21+
let path = store.clone_repo(&repo.repo, &repo.rev)?;
22+
anyhow::bail!("not implemented!")
23+
}
24+
25+
pub(crate) fn all_hooks(config: Config, store: &Store) -> anyhow::Result<Vec<Hook>> {
26+
let mut ret: Vec<Hook> = Vec::new();
27+
28+
for repo in config.repos.into_iter() {
29+
match repo {
30+
Repo::Local(local_repo) => ret.extend(_local_hooks(local_repo, store)?),
31+
Repo::Meta(meta_repo) => ret.extend(_meta_hooks(meta_repo, store)?),
32+
Repo::Remote(repo) => ret.extend(_cloned_hooks(repo, store)?),
33+
}
34+
}
35+
36+
Ok(ret)
37+
}

src/store.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ fn _readonly(d: &path::Path) -> bool {
4141
d.exists() && d.access(AccessMode::WRITE).is_err()
4242
}
4343

44+
trait MakeRepo {
45+
fn make(&self, dest: &str) -> anyhow::Result<()>;
46+
}
47+
48+
struct LocalRepoMaker {}
49+
50+
impl MakeRepo for LocalRepoMaker {
51+
fn make(&self, dest: &str) -> anyhow::Result<()> {
52+
anyhow::bail!("not implemented!");
53+
}
54+
}
55+
56+
struct ClonedRepoMaker<'a> {
57+
repo: &'a str,
58+
rev: &'a str,
59+
}
60+
61+
impl MakeRepo for ClonedRepoMaker<'_> {
62+
fn make(&self, dest: &str) -> anyhow::Result<()> {
63+
anyhow::bail!("not implemented!");
64+
}
65+
}
66+
4467
pub(crate) struct Store {
4568
pub(crate) directory: path::PathBuf,
4669
pub(crate) readonly: bool,
@@ -121,6 +144,10 @@ impl Store {
121144
Ok(ret)
122145
}
123146

147+
pub(crate) fn clone_repo(&self, repo: &str, rev: &str) -> anyhow::Result<String> {
148+
anyhow::bail!("not implemented!");
149+
}
150+
124151
pub(crate) fn mark_config_used(&self, path: &str) -> anyhow::Result<()> {
125152
if self.readonly {
126153
return Ok(());

0 commit comments

Comments
 (0)