Pkg.Write: factor common code for fetching, checking out commits.
fix #4138