问题
I'm using dulwich (a Python library) to access a git repository. When I use get_object
to retrieve a commit, it has a number of attributes. One of those is author
. When I retrieve this attribute, I get bytes
and so the attribute is an an unknown encoding.
Is there an encoding I can safely assume? Does git translate all the metadata to utf-8 before storing it? If it doesn't, how do I know which encoding to use to decode the bytes?
回答1:
Metadata is supposed to be encoded with the value set by the i18n.commitEncoding config value; whenever a commit is created the current value is copied into the 'encoding' header on the object, if set; the default value is UTF-8.
That encoding value is available on Dulwitch objects as the '.encoding' attribute; if it is None
then i18n.commitEncoding
was not explicitly set and you can use UTF-8 as the default.
However! The actual data stored simply follows whatever bytes where handed to git and no re-coding takes place. The configuration value is purely informational. So you need to take into account that an incorrect codec was used, if you are going to use object.encoding or 'utf8'
as the codec, use a sensible error handler or fallback strategy.
来源:https://stackoverflow.com/questions/50825779/do-the-git-repository-data-structures-use-a-canonical-encoding