Skip to content

Commit 4ea1894

Browse files
Add open_from_env helper (josh-project#1281)
This allows to make cache::new a private function and remove some boilerplate from josh-filter.rs Change: open-from-env
1 parent e02309f commit 4ea1894

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

josh-core/src/cache.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,27 @@ impl Transaction {
8787
))
8888
}
8989

90+
pub fn open_from_env(load_cache: bool) -> JoshResult<Transaction> {
91+
let repo = git2::Repository::open_from_env()?;
92+
let path = repo.path().to_owned();
93+
if load_cache {
94+
load(&path)?
95+
};
96+
97+
Ok(Transaction::new(
98+
repo,
99+
gix::ThreadSafeRepository::open(path)?.to_thread_local(),
100+
None,
101+
))
102+
}
103+
90104
pub fn status(&self, _msg: &str) {
91105
/* let mut t2 = self.t2.borrow_mut(); */
92106
/* write!(t2.out, "{}", msg).ok(); */
93107
/* t2.out.flush().ok(); */
94108
}
95109

96-
pub fn new(
110+
fn new(
97111
repo: git2::Repository,
98112
oxide_repo: gix::Repository,
99113
ref_prefix: Option<&str>,

josh-filter/src/bin/josh-filter.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,9 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
147147

148148
let mut filterobj = josh::filter::parse(&specstr)?;
149149

150-
let repo = git2::Repository::open_from_env()?;
151-
if !args.get_flag("no-cache") {
152-
josh::cache::load(repo.path())?;
153-
}
150+
let transaction = josh::cache::Transaction::open_from_env(!args.get_flag("no-cache"))?;
154151

155-
let oxide_repo = gix::ThreadSafeRepository::open(repo.path())?;
156-
let transaction = josh::cache::Transaction::new(repo, oxide_repo.to_thread_local(), None);
157152
let repo = transaction.repo();
158-
159153
let input_ref = args.get_one::<String>("input").unwrap();
160154

161155
let mut refs = vec![];
@@ -418,9 +412,7 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
418412
std::mem::drop(finish);
419413

420414
if let Some(query) = args.get_one::<String>("query") {
421-
let repo = git2::Repository::open_from_env()?;
422-
let oxide_repo = gix::ThreadSafeRepository::open(repo.path())?;
423-
let transaction = josh::cache::Transaction::new(repo, oxide_repo.to_thread_local(), None);
415+
let transaction = josh::cache::Transaction::open_from_env(false)?;
424416
let commit_id = transaction.repo().refname_to_id(update_target)?;
425417
print!(
426418
"{}",

0 commit comments

Comments
 (0)