ngerakines / commitment

A commit message generator that I use.
http://whatthecommit.com/
MIT License
1.2k stars 195 forks source link

Encode message directly into permalink? #273

Open ferdnyc opened 4 months ago

ferdnyc commented 4 months ago

With the existing hash-function based permalinking, there's tension between the desire to deterministically create the same string from the same hash each time, vs. having the hash select only a template string, with the randomized elements being chosen anew each time it's loaded. (See #109, for example.)

But it strikes me that the messages in question are small enough, that it might make sense to just encode the message itself into the permalink. That way, it can be recreated without any dependency on the hash function used, or the lookup implementation.

Python's standard library already has the base64 module, with its urlsafe_b64encode and urlsafe_b64decode functions. Here's how a typical message looks when encoded to URLsafe base64:

>>> from base64 import urlsafe_b64encode, urlsafe_b64decode
>>> msg = "Put everything in its right place"
>>> coded = urlsafe_b64encode(msg.encode("utf-8"))
>>> coded
b'UHV0IGV2ZXJ5dGhpbmcgaW4gaXRzIHJpZ2h0IHBsYWNl'

The message can easily be reversed back into its text form:

>>> urlsafe_b64decode(coded).decode("utf-8")
'Put everything in its right place'

The longest message in the data, currently, is this one at 263 characters:

I'll mention this again, if you're git-blaming this, don't come slap me personally. This code straight ported from another project and we WILL refactor this in the future. This is a temporary solution. OK I guess you can slap me for porting this as is, but still.

Encoded to base64, it becomes 352 characters, which is long but not unreasonable as a URL length. (Have you seen Google Maps URLs?)