Skip to content

Commit 0ecfcb2

Browse files
Fix handlebars templates reporting missing objects
The handlebars code was not yet converted to using alternate object stores. We did not notice this issue at first, because templateing is only tested with the cli and the cli does not use alternates. Change: fix-handlebars
1 parent 9f4692e commit 0ecfcb2

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

josh-core/src/cache.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ pub struct Transaction {
7373

7474
impl Transaction {
7575
pub fn open(path: &std::path::Path, ref_prefix: Option<&str>) -> JoshResult<Transaction> {
76+
if !path.exists() {
77+
return Err(josh_error("path does not exist"));
78+
}
7679
Ok(Transaction::new(
7780
git2::Repository::open_ext(
7881
path,

josh-core/src/query.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@ impl GraphQLHelper {
2323
.join(path);
2424
let path = normalize_path(&path);
2525

26-
let transaction = cache::Transaction::open(&self.repo_path, Some(&self.ref_prefix))?;
26+
let transaction = if let Ok(to) =
27+
cache::Transaction::open(&self.repo_path.join("mirror"), Some(&self.ref_prefix))
28+
{
29+
to.repo().odb()?.add_disk_alternate(
30+
self.repo_path
31+
.join("overlay")
32+
.join("objects")
33+
.to_str()
34+
.unwrap(),
35+
)?;
36+
to
37+
} else {
38+
cache::Transaction::open(&self.repo_path, Some(&self.ref_prefix))?
39+
};
2740

2841
let tree = transaction.repo().find_commit(self.commit_id)?.tree()?;
2942

@@ -41,8 +54,26 @@ impl GraphQLHelper {
4154
variables.insert(k.to_string(), juniper::InputValue::scalar(v.render()));
4255
}
4356

44-
let transaction = cache::Transaction::open(&self.repo_path, None)?;
45-
let transaction_overlay = cache::Transaction::open(&self.repo_path, None)?;
57+
let (transaction, transaction_overlay) =
58+
if let Ok(to) = cache::Transaction::open(&self.repo_path.join("overlay"), None) {
59+
to.repo().odb()?.add_disk_alternate(
60+
self.repo_path
61+
.join("mirror")
62+
.join("objects")
63+
.to_str()
64+
.unwrap(),
65+
)?;
66+
(
67+
cache::Transaction::open(&self.repo_path.join("mirror"), None)?,
68+
to,
69+
)
70+
} else {
71+
(
72+
cache::Transaction::open(&self.repo_path, None)?,
73+
cache::Transaction::open(&self.repo_path, None)?,
74+
)
75+
};
76+
4677
let (res, _errors) = juniper::execute_sync(
4778
&query,
4879
None,
@@ -77,7 +108,7 @@ impl handlebars::HelperDef for GraphQLHelper {
77108
h.hash(),
78109
rc.get_current_template_name().unwrap_or(&"/".to_owned()),
79110
)
80-
.map_err(|_| handlebars::RenderError::new("josh"))?,
111+
.map_err(|e| handlebars::RenderError::new(format!("{}", e)))?,
81112
));
82113
}
83114
}
@@ -162,13 +193,25 @@ pub fn render(
162193
drop(obj);
163194
drop(tree);
164195

196+
let repo_path =
197+
if transaction.repo().path().file_name() == Some(std::ffi::OsStr::new("overlay")) {
198+
transaction
199+
.repo()
200+
.path()
201+
.parent()
202+
.ok_or(josh_error("parent"))?
203+
.to_owned()
204+
} else {
205+
transaction.repo().path().to_owned()
206+
};
207+
165208
let mut handlebars = handlebars::Handlebars::new();
166209
handlebars.register_template_string(path, template)?;
167210
handlebars.register_helper("concat", Box::new(helpers::concat_helper));
168211
handlebars.register_helper(
169212
"graphql",
170213
Box::new(GraphQLHelper {
171-
repo_path: transaction.repo().path().to_owned(),
214+
repo_path: repo_path,
172215
ref_prefix: ref_prefix.to_owned(),
173216
commit_id,
174217
}),

tests/proxy/query.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
$ git commit -m "add file2" 1> /dev/null
1818

1919
$ cat > x.graphql <<EOF
20-
> query() {
20+
> query {
2121
> hash
2222
> }
2323
> EOF
@@ -57,6 +57,7 @@
5757
JoshError(Error rendering "tmpl_file" line 1, col 8: Variable "param_val" not found in strict mode.) (no-eol)
5858
$ curl -s http://localhost:8002/real_repo.git?render=tmpl_file\&param_val=12345
5959
param: 12345
60+
sha: 002d20d28aab1ebe3892b01ec1dfc60d43fc598f
6061
$ curl -s http://localhost:8002/real_repo.git?get=sub1/file1
6162
contents1
6263
$ curl -s http://localhost:8002/real_repo.git@refs/changes/123/2:nop.git?get=sub2/on_change

0 commit comments

Comments
 (0)