Using GitInfo

Feedback, it works for partials still, but, in the templates using block and define, you cannot use this trick as a marker:

 {{ "<!-- ENTERING layouts/taxonomy/tag.html -->" | safeHTML }}
   template content
 {{ "<!-- LEAVING layouts/taxonomy/tag.html -->" | safeHTML }}

It chokes on it. Remove them and it’s fine.

Hi guys, ok, upgraded to v21-DEV when I heard about the v20 release. And, I’m getting some errors now, that I was not getting earlier. Some, like the one regarding Render are warnings so, the site still renders ok.

But, the footer is a problem. Something appears to be stricter now, with the .GitInfo and it no longer wants to render the sha.

The culprit might be the code @rdwatters provided below, I suspect.

This is the error below, and I hope you might have a moment to take a look.
Kind regards,
Rick

Ok, interesting: I had forgot to do git commit, and a couple of files were new and did not yet have sha’s. Once I committed, the footer renders and the ERRORs go away. I need to figure out how to make this a little more forgiving.

1 Like

Well, I’ve got a question, that I hope someone can assist with:

After upgrading to v0.21-DEV-9EC00725 today, I now get an error about building the footer for the 404.html template, which I have in layouts. The error is similar to the above, but it did not appear until today:

 ERROR 2017/04/11 21:01:38 Error while rendering "404": template: /Users/rcogley/dev/RCC-live/layouts/404.html:28:6: executing "footer" at <partial "footer.html...>: error calling partial: template: partials/footer.html:12:127: executing "partials/footer.html" at <.GitInfo.Abbreviated...>: can't evaluate field AbbreviatedHash in type *gitmap.GitInfo

So, like I did with my root index and section index, I added a markdown file and referenced it in the 404.html, using {{ .Content }}. This time however, it does nothing, and the markdown’s content is not pulled into the template.

I also can’t use:

 {{ if and (ne .Kind "taxonomy") (ne .Kind "taxonomyTerm") }}<small class="gray"><em>Updated: {{ .Lastmod }} {{ .GitInfo.AbbreviatedHash }}</em></small>{{ end }}

… because there is no “kind” that matches this error page, I think.

Any workaround for this? I guess I could make a section “error” then add the template and its content for that, and, point to the file using .htaccess. Does Hugo “care” if the 404 is in the root or not?

@RickCogley On the move right now and not near computer, but I’m curious what you get back when you put {{.Kind}} in layouts/404.html…

Well a big “duh” on my part. Checking for kind worked to fix the immediate problem, thanks!

 {{ if and (ne .Kind "taxonomy") (ne .Kind "taxonomyTerm") (ne .Kind "404") }}<small class="gray"><em>Updated: {{ .Lastmod
  }} {{ .GitInfo.AbbreviatedHash }}</em></small>{{ end }}

It would be good to be able to inject 404.md into the 404.html though.

I have not read this entire thread, but wouldn’t

{{ with .GitInfo }}

Be simpler?

1 Like

@bep oof, yeah, it probably would. Thanks! I’ll give it a try today.

By about 80 orders of magnitude. @RickCogley. Sorry, I guess I got hung up on the Kind piece and ignored the obvious. D’oh.

However, when I try {{ with .GitInfo }} I still get errors in hugo server’s log, re all the types that don’t have that - root index, section index etc. Apparently, it expects the template to be applied to content that actually has a .GitInfo to query in the first place, and chokes if you use it as a general filter.

Well, that is a symptom of something else.

Every Page have a GitInfo pointer. It may be nil, but that should with handle just fine.

So, my guess you call this partial from somewhere without the “.” as an argument.

1 Like

Thanks @bep, I’ll go through my templates and have a look. Appreciate the time you take to help!

Hi, I’m puzzled about how to access a GitInfo variable from within a partial included from baseof. It seems like GitInfo is only accessible from within a Page template (not partials or List templates or baseof templates)?

Is there no way to access e.g. {{ .GitInfo.AbbreviatedHash }} from my single.html template, save it in a block, and then reference that block’s value from a partial included by baseof.html (e.g. head.html)? If not, can someone please clarify why?

I can access .GitInfo just fine in my baseof.html (see). Are you setting enableGitInfo = true in your site config?

I’m puzzled about how to access a GitInfo variable from within a partial

Are you passing the “context” … the dot to the partial?

1 Like

Yes, enableGitInfo is true in the site config and as a command line flag.

Are you passing the “context” … the dot to the partial?

Yes. I’m not sure why it wasn’t working. The exact flow of Hugo template logic between baseof blocks, themes, partials, _default, index, lists, etc. is still a little tricky for me to get my head around when debugging. The error Hugo kept emitting was:

execute of template failed: template: index.html:8:17:
executing “index.html” at <.GitInfo.AbbreviatedHash>:
can’t evaluate field AbbreviatedHash in type *gitmap.GitInfo

So accessing the GitInfo hash from baseof (on a single.html layout) works now. Question 2 is, why is the GitInfo hash not accessible outside of a single layout (or single’s baseof)? I can’t get the the git SHA for e.g. a list page template (the template file itself, not the content list)?

May be you are getting confused with all the terminology? There’s isn’t a “single’s baseof”. From what I have seen and used, there’s one and only one baseof.html that’s put in _default (and it’s entirely possible that I am wrong).

Do you want to have a look at how I organize the theme in my “bare bones” theme? It’s using single.html, baseof.html, etc.

1 Like

Thanks! No, I do understand how baseof works, I was referring to the site’s _default/baseof, as in: the content markdown maps to _default/single which overrides blocks defined in _default/baseof which loads some partials, etc.

The question is, why does GitInfo only work for content files versioned in Git and not templates versioned in Git? What if I just want the overall latest SHA of the branch?

This question still never got answered, tho. I wonder if anyone knows.

Because it looks like the GitInfo feature is designed to work only with the contentDir: https://github.com/gohugoio/hugo/blob/b5f39d23b86f9cb83c51da9fe4abb4c19c01c3b7/hugolib/gitinfo.go#L26

2 Likes

Ha! I see. Thank you!