A `sprintf` with no bounds checking looks very dangerous to me.  Use `git_buf_printf` here, please, and probably best just to return a `git_buf`.  In fact, this whole function might be better served living in the `git_buf` namespace.