Skip to content

Fix handlebars templates reporting missing objects #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions josh-core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub struct Transaction {

impl Transaction {
pub fn open(path: &std::path::Path, ref_prefix: Option<&str>) -> JoshResult<Transaction> {
if !path.exists() {
return Err(josh_error("path does not exist"));
}
Ok(Transaction::new(
git2::Repository::open_ext(
path,
Expand Down
53 changes: 48 additions & 5 deletions josh-core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ impl GraphQLHelper {
.join(path);
let path = normalize_path(&path);

let transaction = cache::Transaction::open(&self.repo_path, Some(&self.ref_prefix))?;
let transaction = if let Ok(to) =
cache::Transaction::open(&self.repo_path.join("mirror"), Some(&self.ref_prefix))
{
to.repo().odb()?.add_disk_alternate(
self.repo_path
.join("overlay")
.join("objects")
.to_str()
.unwrap(),
)?;
to
} else {
cache::Transaction::open(&self.repo_path, Some(&self.ref_prefix))?
};

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

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

let transaction = cache::Transaction::open(&self.repo_path, None)?;
let transaction_overlay = cache::Transaction::open(&self.repo_path, None)?;
let (transaction, transaction_overlay) =
if let Ok(to) = cache::Transaction::open(&self.repo_path.join("overlay"), None) {
to.repo().odb()?.add_disk_alternate(
self.repo_path
.join("mirror")
.join("objects")
.to_str()
.unwrap(),
)?;
(
cache::Transaction::open(&self.repo_path.join("mirror"), None)?,
to,
)
} else {
(
cache::Transaction::open(&self.repo_path, None)?,
cache::Transaction::open(&self.repo_path, None)?,
)
};

let (res, _errors) = juniper::execute_sync(
&query,
None,
Expand Down Expand Up @@ -77,7 +108,7 @@ impl handlebars::HelperDef for GraphQLHelper {
h.hash(),
rc.get_current_template_name().unwrap_or(&"/".to_owned()),
)
.map_err(|_| handlebars::RenderError::new("josh"))?,
.map_err(|e| handlebars::RenderError::new(format!("{}", e)))?,
));
}
}
Expand All @@ -91,6 +122,7 @@ pub fn render(
ref_prefix: &str,
commit_id: git2::Oid,
query_and_params: &str,
split_odb: bool,
) -> JoshResult<Option<String>> {
let mut parameters = query_and_params.split('&');
let query = parameters
Expand Down Expand Up @@ -162,13 +194,24 @@ pub fn render(
drop(obj);
drop(tree);

let repo_path = if split_odb {
transaction
.repo()
.path()
.parent()
.ok_or(josh_error("parent"))?
.to_owned()
} else {
transaction.repo().path().to_owned()
};

let mut handlebars = handlebars::Handlebars::new();
handlebars.register_template_string(path, template)?;
handlebars.register_helper("concat", Box::new(helpers::concat_helper));
handlebars.register_helper(
"graphql",
Box::new(GraphQLHelper {
repo_path: transaction.repo().path().to_owned(),
repo_path: repo_path,
ref_prefix: ref_prefix.to_owned(),
commit_id,
}),
Expand Down
2 changes: 1 addition & 1 deletion josh-filter/src/bin/josh-filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
let commit_id = transaction.repo().refname_to_id(update_target)?;
print!(
"{}",
josh::query::render(&transaction, "", commit_id, query,)?
josh::query::render(&transaction, "", commit_id, query, false)?
.unwrap_or("File not found".to_string())
);
}
Expand Down
2 changes: 1 addition & 1 deletion josh-proxy/src/bin/josh-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ async fn serve_query(
let commit_id =
josh::filter_commit(&transaction, filter, commit_id, josh::filter::empty())?;

josh::query::render(&transaction, "", commit_id, &q)
josh::query::render(&transaction, "", commit_id, &q, true)
})
.in_current_span()
.await?;
Expand Down
11 changes: 11 additions & 0 deletions tests/proxy/query.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
$ git add sub2
$ git commit -m "add file2" 1> /dev/null

$ cat > x.graphql <<EOF
> query {
> hash
> }
> EOF

$ cat > tmpl_file <<EOF
> param: {{ param_val }}
> {{ #with (graphql file="x.graphql") as |gql| }}
> sha: {{ gql.hash }}
> {{ /with }}
> EOF

$ git add x.graphql
$ git add tmpl_file
$ git commit -m "add tmpl_file" 1> /dev/null

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