TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to Quickly and Correctly Generate a Git Log in HTML

94 pointsby foobover 7 years ago

11 comments

jacobparkerover 7 years ago
Reinventing escaping over and over again (which bash scripts in particular seem to encourage) is a suckers game. It&#x27;s difficult to get right and if you&#x27;re constantly redoing it you&#x27;re eventually going to make a mistake. I&#x27;ve worked in web security and it&#x27;s sad to see how likely it is for people with good intentions to mess this up. I&#x27;m glad the author basically came to this conclusion.<p>The winning strategy is to use a library&#x2F;framework&#x2F;whatever for embedding user-provided content into HTML. Sane HTML template libraries will do this. That library has had more time to get it right. Furthermore a well designed API will clearly indicate what is trusted vs. untrusted data and all untrusted data is properly encoded before being embedded. See the &quot;Security Model&quot; section of golangs HTML templates below.<p>An alternative to using the git tools which is appropriate for serious work (shell pipelines are great for prototyping) is libgit2. It has bindings for many languages. It&#x27;s very easy to use (sometimes (not always) easier than the CLI) and often much higher performance vs. big shell pipelines (operating on text gets slow pretty fast, and often you end up using xargs...)<p>An example set of tools: <a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;html&#x2F;template&#x2F;" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;html&#x2F;template&#x2F;</a> + <a href="https:&#x2F;&#x2F;github.com&#x2F;libgit2&#x2F;git2go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;libgit2&#x2F;git2go</a> .<p>It&#x27;s not as succinct as a bash script but it&#x27;s easier to build something that&#x27;s correct. Use the shell to prototype, build it right in a saner environment.
评论 #15293158 未加载
评论 #15294267 未加载
评论 #15294600 未加载
评论 #15295424 未加载
falsedanover 7 years ago
<p><pre><code> git log --pretty=format:&quot;%H%x00%s&quot; | sed &#x27;s&#x2F;&amp;&#x2F;\&amp;amp;&#x2F;g; s&#x2F;&lt;&#x2F;\&amp;lt;&#x2F;g; s&#x2F;&gt;&#x2F;\&amp;gt;&#x2F;g; s&#x2F;&quot;&#x2F;\&amp;quot;&#x2F;g; s&#x2F;&#x27;&quot;&#x27;&quot;&#x27;&#x2F;\&amp;#39;&#x2F;g; s@\(.*\)\x0\(.*\)@&lt;tr&gt;&lt;th&gt;\1&lt;&#x2F;th&gt;&lt;td&gt;\2&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;@&#x27; </code></pre> You could do the dumb html entifying in a real language. The article&#x27;s solution is a straw man, since it&#x27;s promoting their personal language.<p>Why did they see \x01 &amp; \x02 as possible sentinels but not nulls? python is fine with nulls…
评论 #15295556 未加载
评论 #15292980 未加载
评论 #15295024 未加载
tzsover 7 years ago
The underlying problem with the first, simple, approach is that the template it is using to get things from git,<p><pre><code> &quot;&lt;tr&gt; &lt;td&gt;%H&lt;&#x2F;td&gt; &lt;td&gt;%s&lt;&#x2F;td&gt; &lt;tr&gt;&quot; </code></pre> interpolates values that need to be escaped, but includes literal text that must not be escaped. (My guess is that the author meant &quot;&lt;&#x2F;tr&gt;&quot; for the last element, but the article says &quot;&lt;tr&gt;&quot; so I&#x27;m going with that).<p>The author&#x27;s approach to deal with that is to mark the places in the template where escaping will be needed, and then make and use an escaping tool that recognizes those marks and just escapes the marked segments.<p>A simpler approach is to eliminate the underlying problem. For getting the data out of git use a template where the literal text is safe to escape, such as this:<p><pre><code> &quot;%H,%s&quot; </code></pre> The escaping can then be done by a tool that escapes its entire input. That will leave the comma from the template alone, and will not introduce any new commas. The interpolation of %s might have introduced commas, but they will all be after the literal comma from the template. The interpolation of %H will not introduce commas.<p>The output from the escaper can then be transformed into the final output by replacing the first &quot;,&quot; with &quot;&lt;&#x2F;td&gt; &lt;td&gt;&quot;, prepending &quot;&lt;tr&gt; &lt;td&gt;&quot;, and appending &quot;&lt;&#x2F;td&gt; &lt;tr&gt;&quot;. All of these are simple in a shell pipeline using sed.
评论 #15296544 未加载
dahartover 7 years ago
You can skip having to escape any characters or worry if the content is correct, if you put an unformatted git log into a script tag, and then line split and set the content of each element via a JS call.<p>I just tried it, and it works beautifully, no problems with illegal characters.<p>What&#x27;s wrong with this? It&#x27;d be super easy to extend if you want columns or colors or links...<p><pre><code> &lt;script id=&#x27;gitlog&#x27; type=&#x27;text&#x27;&gt; c0c3150f5 09 - 15 dahart Color widget!, #1 improving &lt; hsv &gt; &amp;&amp; things [Finishes #8736345] \m&#x2F; &#x27;&quot;,.;:%$#@* &lt;&#x2F;script&gt; &lt;div id=&#x27;lines&#x27;&gt;&lt;&#x2F;div&gt; $(&#x27;#gitlog&#x27;).html().split(&#x27;\n&#x27;).forEach(line =&gt; { $(&#x27;#lines&#x27;).append($(&#x27;&lt;div class=&quot;line&quot;&#x2F;&gt;&#x27;).text(line)) })</code></pre>
pixelbeat__over 7 years ago
Also consider <a href="https:&#x2F;&#x2F;www.pixelbeat.org&#x2F;scripts&#x2F;ansi2html.sh" rel="nofollow">https:&#x2F;&#x2F;www.pixelbeat.org&#x2F;scripts&#x2F;ansi2html.sh</a> for the general case of (colored) output to html conversion
no_protocolover 7 years ago
`gitweb` is a server that comes with your git install.<p>The `gitweb` web interface includes both a log and shortlog view for repositories. You can probably use those to some benefit.<p>This seems to be the source of the shortlog command:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;gitweb&#x2F;gitweb.perl#L5889" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;gitweb&#x2F;gitweb.perl#L5...</a>
masukomiover 7 years ago
why do people insist upon reinventing the wheel badly:<p>git log --color=always &lt;whatever funky coloring, options, etc you want&gt; | aha &gt; git_log.html<p>side note: aha is not installed by default on macOS but homebrew will fix that for you. Also, it has many color and styling options.
评论 #15298054 未加载
stephenrover 7 years ago
How is it that git still doesn&#x27;t have machine readable output built in?
评论 #15293730 未加载
评论 #15293955 未加载
评论 #15293838 未加载
评论 #15296371 未加载
Sir_Cmpwnover 7 years ago
I don&#x27;t see what was wrong with the first solution. Keep it simple!
评论 #15292956 未加载
mattacularover 7 years ago
&gt;Some programmers might stop here and say, Let&#x27;s switch to a real programming language. Do it the right way.<p>Isn&#x27;t using Python switching to a real programming language?
评论 #15295035 未加载
评论 #15293002 未加载
whipoodleover 7 years ago
I don&#x27;t want to use an API and do it the right way. That&#x27;s too complicated, poindexter! (50 lines of garbage script follow)