I'm creating an application which allows collaborative editing of documents utilising git2-rs where my users are not required to be programmers or conversant with git.
The workflow consists of the user pulling the base image main / master writing their edits, then the client application creating a patch and sending it over to the server where it would be applied to their branch instead of them sending the whole edited file.
I also expect the branch access rights to be handled server side so I do not want the clients to have push rights but only pull that's why I wouldn't just allow the clients to push their changes to the branch directly.
As initially stated I use git2-rs to handle the git actions, my problem comes with extracting the patches from a working index. And I was wondering if anyone knows how I can go about this without relying on outside implementations (diffy and git2::Index::conflicts()), which I would but I think am doing something that is already implemented in git2-rs.
Hello so I wanted to answer this as I just figured it out.
Turns out in the early days changes in a repository were shared between developers by emailing git patches, utilising git format-patch or plain old piping the result of git diff to a <.patchfile>. These were then reviewed and applied in the upstream using git apply.
So I figured I could recreate this workflow in my implementation by making the clients create git patches and sharing them to the server where it would apply it to the corresponding branch and utilise checksums to figure out if the operations were correct.
The git2-rs API for this are:
git2::Diff::from_buffer(buffer: &[u8]) -> Result<Diff<'static>, Error> which reads the contents of a git patch file into a git_diff object.
git2::Repository::apply(&self, diff: &Diff<'_>, location: ApplyLocation, options: Option<&mut ApplyOptions<'_>>) -> Result<(), Error> : which applies a Diff to the given repo, making changes directly in the working directory, the index, or both.
git2::Patch::print(&mut self, line_cb: &mut (dyn FnMut(DiffDelta<'_>, Option<DiffHunk<'_>>, DiffLine<'_>) -> bool + '_)) -> Result<(), Error> : which prints the Patch to text via a callback.