cgit is a ‘hyperfast web frontend for git repositories’.

cgit's display of file contents (example) renders everything inside a pre block, and therefore does not support wrapping of long lines. cgit handles line numbers by coding them separately to the actual content, so adjusting cgit to support line wrapping requires a bit of twiddling.

I assume cgit is set up for source formatting (via source-filter and syntax-highlighting.py). We make a copy of syntax-highlighting.py, and edit the arguments passed to Pygments to add lineanchors='line':

#formatter = HtmlFormatter(style='pastie', nobackground=True)
formatter = HtmlFormatter(style='pastie', nobackground=True, lineanchors='line')


This will insert an anchor (e.g. <a name="line-1"></a>) at the beginning of each line in the pre block. We now need to modify this to include the actual line number:

#sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
import re
output = highlight(data, lexer, formatter, outfile=None)
output = re.sub(r'<a name="line-([0-9]+)"></a>', r'<a name="L\1" href="#L\1">\1</a>', output)
sys.stdout.write(output)


(Processing HTML using regex! The horror!)

Now we can move to the cgit CSS to display these line numbers correctly. As before, we make a copy of cgit.css and hide the original line numbers:

div#cgit table.blob td.linenumbers pre {
visibility: hidden;
}


(We specify visibility: hidden; rather than display: none; so that the correct amount of space will still be reserved, and the vertical line separating the line numbers from the text will remain correctly-positioned.)

Next we allow the text to wrap:

div#cgit table.blob td.lines pre {
white-space: pre-wrap;
position: relative;
}


Finally, we format the new line numbers:

div#cgit table.blob td.lines a::before {
display: inline-block;
width: 4ex;
position: absolute;
left: -5.5ex;
text-align: right;
color: gray;
}


We now update the css and source-filter options in cgitrc to point to these modified versions, et voilà, line wrapping with correct line numbers.

See it in action on my own cgit instance here.