git_policy_cli/
commit_facts.rs1use super::candidate_error::CandidateError;
17use super::candidate_prediction::{CandidateRequest, PreparedCandidates, prepare_candidates};
19use super::repository_facts::RepositoryFacts;
21use super::repository_location::RepositoryLocation;
22use super::rule_commit_index::IndexVsHead;
23use super::rule_commit_sequencer::SequencerState;
24use super::rule_worktree_dirty::DirtyScope;
25use super::transaction_git::GitContext;
26use std::ffi::{OsStr, OsString};
27use std::path::Path;
28
29const INDEX_FILE_VARIABLE: &str = "GIT_INDEX_FILE";
31const OBJECT_DIRECTORY_VARIABLE: &str = "GIT_OBJECT_DIRECTORY";
33
34pub struct PrivateIndexFacts<F: RepositoryFacts> {
44 inner: F,
46 private: Vec<(OsString, OsString)>,
48}
49
50impl<F: RepositoryFacts> PrivateIndexFacts<F> {
51 pub fn new(inner: F, index: &Path, object_directory: &Path) -> Self {
61 return PrivateIndexFacts {
62 inner,
63 private: private_entries(index, object_directory),
64 };
65 }
66
67 pub fn use_index(&mut self, index: &Path, object_directory: &Path) {
77 self.private = private_entries(index, object_directory);
78 }
79}
80
81fn private_entries(index: &Path, object_directory: &Path) -> Vec<(OsString, OsString)> {
91 return vec![
92 (OsString::from(INDEX_FILE_VARIABLE), index.as_os_str().to_os_string()),
93 (
94 OsString::from(OBJECT_DIRECTORY_VARIABLE),
95 object_directory.as_os_str().to_os_string(),
96 ),
97 ];
98}
99
100impl<F: RepositoryFacts> RepositoryFacts for PrivateIndexFacts<F> {
101 fn location(&mut self) -> Result<RepositoryLocation, String> {
102 return self.inner.location();
103 }
104
105 fn index_vs_head(&mut self) -> Result<IndexVsHead, String> {
106 return self.inner.index_vs_head();
107 }
108
109 fn worktree_dirty(&mut self, scope: DirtyScope) -> Result<bool, String> {
110 return self.inner.worktree_dirty(scope);
111 }
112
113 fn sequencer_state(&mut self) -> Result<SequencerState, String> {
114 return self.inner.sequencer_state();
115 }
116
117 fn remote_guess_creates_branch(&mut self, target: &OsStr) -> Result<bool, String> {
118 return self.inner.remote_guess_creates_branch(target);
119 }
120
121 fn candidates(&mut self, request: &CandidateRequest) -> Result<PreparedCandidates, CandidateError> {
122 let context: GitContext = self.inner.transaction_context();
123 let mut overlay: Vec<(OsString, OsString)> = context.overlay.clone();
124 overlay.extend(self.private.iter().cloned());
125 return prepare_candidates(
126 context.real_git.as_path(),
127 context.global_prefix.as_slice(),
128 overlay.as_slice(),
129 request,
130 );
131 }
132
133 fn asked_location(&self) -> Option<Result<RepositoryLocation, String>> {
134 return self.inner.asked_location();
135 }
136
137 fn transaction_context(&self) -> GitContext {
138 return self.inner.transaction_context();
139 }
140}
141
142impl<F: RepositoryFacts> RepositoryFacts for &mut F {
144 fn location(&mut self) -> Result<RepositoryLocation, String> {
145 return (**self).location();
146 }
147
148 fn index_vs_head(&mut self) -> Result<IndexVsHead, String> {
149 return (**self).index_vs_head();
150 }
151
152 fn worktree_dirty(&mut self, scope: DirtyScope) -> Result<bool, String> {
153 return (**self).worktree_dirty(scope);
154 }
155
156 fn sequencer_state(&mut self) -> Result<SequencerState, String> {
157 return (**self).sequencer_state();
158 }
159
160 fn remote_guess_creates_branch(&mut self, target: &OsStr) -> Result<bool, String> {
161 return (**self).remote_guess_creates_branch(target);
162 }
163
164 fn candidates(&mut self, request: &CandidateRequest) -> Result<PreparedCandidates, CandidateError> {
165 return (**self).candidates(request);
166 }
167
168 fn asked_location(&self) -> Option<Result<RepositoryLocation, String>> {
169 return (**self).asked_location();
170 }
171
172 fn transaction_context(&self) -> GitContext {
173 return (**self).transaction_context();
174 }
175}
176
177#[cfg(test)]
179#[path = "commit_facts_tests.rs"]
180mod tests;