Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Create Patch for new file from buffer #742

Closed
brandonio21 opened this issue Nov 27, 2017 · 3 comments
Closed

[Support] Create Patch for new file from buffer #742

brandonio21 opened this issue Nov 27, 2017 · 3 comments

Comments

@brandonio21
Copy link
Contributor

Hello all,

I am working with the use case of creating diffs from buffers. Everything is going well as there is decent support for it, especially in the form of Blob.diff_to_buffer. The function works great when changing content and will soon work for deleting a file (In #741). However, I cannot figure out a proper way to add files using Blob.diff_to_buffer.

Do you guys have any suggestions? In libgit2, the proper way is to call git_diff_blob_to_buffer and set the Blob* argument to NULL. However, pygit2 does not have a static function to be able to do this. Do you guys have any suggestions? Here are things I can think of:

  1. Create a special "Empty" blob object (or give it an exists property) that can be used to signify a nonexistent blob and then do diff_to_buffer on that.

  2. Expose a static Patch.from_blob_to_buffer() which can then be called like Patch.from_blob_to_buffer(None, buf).

The other thing I can think of, which does not yet exist, is adding a Tree.diff_to_buffer()/Tree.diff_to_blob() to libgit2, which would solve this problem since the Tree would know whether the blob is being added or removed. (Of course, this would then have to be wrapped in pygit2)

Thoughts?

@jdavid
Copy link
Member

jdavid commented Nov 28, 2017

Hi,

Solution 1 is already there, no?

>>> import pygit2
>>> r = pygit2.Repository('/home/jdavid/sandboxes/pygit2/')
>>> bid = r.create_blob('')
>>> b = r[bid] # Empty blob
>>> print(b.diff_to_buffer('hello').patch)
diff --git a/file b/file
index e69de29..b6fc4c6 100644
--- a/file
+++ b/file
@@ -0,0 +1 @@
+hello
\ No newline at end of file

That said, solution 2 looks good to me, then you can support the optional arguments, old and new filenames. In that case you would wrap git_patch_from_blob_and_buffer no? Then the name could be Patch.from_blob_and_buffer so it matches libgit2's name (wonder why one is called _to_ and the other is called _and_)

@jdavid
Copy link
Member

jdavid commented Nov 28, 2017

Developing solution 2 a bit further, it could type check the arguments and wrap:

  • git_patch_from_blob_and_buffer
  • git_patch_from_blobs
  • git_patch_from_buffers

Then it could be called Patch.create_from, maybe.

@brandonio21
Copy link
Contributor Author

Thanks for the reply, @jdavid.

Unfortunately, the solution you highlighted with the empty blob does not work. It creates a patch that is an addition to an empty file, not the creation of a new file (patch should have new file and be --- /dev/null

In either case, glad that there is approval of the second "proper" solution. I'll work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants