Tadman answer is almost complete. However I red the man page for git log and found something interesting in the format section (you can also have it here).
You can truncate any placeholder using a previous placeholder such as %<(5,trunc). Here the command will truncate right if length of the next placeholder is more than 5 or pad right otherwise.
So in your case, this will truncate to 5:
system("git log --pretty=format:'[%<(5,trunc)%h]: %an'")
The only issue here is that you will only have 3 usefull digits, because when it truncate, format add .. to show that your placeholder is not complete. So an example of result would be :
[8b9..]: Jack Johnson [5fe..]: Popeye [2cb..]: Jack Johnson [e5d..]: Jack Johnson [605..]: Plastic Bertrand [20c..]: Plastic Bertrand
EDIT:
You can easily remove those last dots using:
system("git log --pretty=format:'[%<(7,trunc)%h]: %an' | tr -d .")
Then you would have the clean result expected:
[8b972]: Jack Johnson [5fe3d]: Popeye [2cbe0]: Jack Johnson [e5d06]: Jack Johnson [605d7]: Plastic Bertrand [20cae]: Plastic Bertrand