Skip to content

Commit 8a764ad

Browse files
Fix handlebars templates reporting missing objects (#1213)
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 cb041ad commit 8a764ad

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
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
}
@@ -91,6 +122,7 @@ pub fn render(
91122
ref_prefix: &str,
92123
commit_id: git2::Oid,
93124
query_and_params: &str,
125+
split_odb: bool,
94126
) -> JoshResult<Option<String>> {
95127
let mut parameters = query_and_params.split('&');
96128
let query = parameters
@@ -162,13 +194,24 @@ pub fn render(
162194
drop(obj);
163195
drop(tree);
164196

197+
let repo_path = if split_odb {
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
}),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
424424
let commit_id = transaction.repo().refname_to_id(update_target)?;
425425
print!(
426426
"{}",
427-
josh::query::render(&transaction, "", commit_id, query,)?
427+
josh::query::render(&transaction, "", commit_id, query, false)?
428428
.unwrap_or("File not found".to_string())
429429
);
430430
}

josh-proxy/src/bin/josh-proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ async fn serve_query(
13581358
let commit_id =
13591359
josh::filter_commit(&transaction, filter, commit_id, josh::filter::empty())?;
13601360

1361-
josh::query::render(&transaction, "", commit_id, &q)
1361+
josh::query::render(&transaction, "", commit_id, &q, true)
13621362
})
13631363
.in_current_span()
13641364
.await?;

tests/proxy/query.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616
$ git add sub2
1717
$ git commit -m "add file2" 1> /dev/null
1818

19+
$ cat > x.graphql <<EOF
20+
> query {
21+
> hash
22+
> }
23+
> EOF
24+
1925
$ cat > tmpl_file <<EOF
2026
> param: {{ param_val }}
27+
> {{ #with (graphql file="x.graphql") as |gql| }}
28+
> sha: {{ gql.hash }}
29+
> {{ /with }}
2130
> EOF
2231

32+
$ git add x.graphql
2333
$ git add tmpl_file
2434
$ git commit -m "add tmpl_file" 1> /dev/null
2535

@@ -47,6 +57,7 @@
4757
JoshError(Error rendering "tmpl_file" line 1, col 8: Variable "param_val" not found in strict mode.) (no-eol)
4858
$ curl -s http://localhost:8002/real_repo.git?render=tmpl_file\&param_val=12345
4959
param: 12345
60+
sha: 002d20d28aab1ebe3892b01ec1dfc60d43fc598f
5061
$ curl -s http://localhost:8002/real_repo.git?get=sub1/file1
5162
contents1
5263
$ curl -s http://localhost:8002/real_repo.git@refs/changes/123/2:nop.git?get=sub2/on_change

0 commit comments

Comments
 (0)