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

Allow more flexibility in applying individual patches #20

Open
GoogleCodeExporter opened this issue Apr 18, 2015 · 2 comments
Open

Allow more flexibility in applying individual patches #20

GoogleCodeExporter opened this issue Apr 18, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

I'm using patch in an app that needs to accept patch files and apply them 
safely contained within a certain directory. Namely, it needs to vet each file 
that is to be modified and fail if a patch attempts to modify any file outside 
the base directory that the patch is to be applied to - this could be due to 
symlinks, relative or absolute paths.

To enable this, I've split up the `apply` function into two parts:

patchset.file_patches()
 - returns a generator of (filename, patch) pairs - note that this is the filename that will be modified (so it may be either the source or target)

patchset.apply_patch(filename, i, p):
 - applies an inidivual patch to the given filename. I've left the variable names as-is for the sake of a minimal patch, but `fileno` and `patch` may be better names.

This makes `patchset.apply` a very simple piece of code that just hooks these 
two functions together. My app can now do the same thing, but insert some 
file-checking code before each patch gets applied. Previously it would have to 
duplicate the logic in `apply`, which is quite a huge undertaking.

Other bits:

 - I did have to move the `errors` count into a field on PatchSet, because there's no longer any shared scope between `apply` and `pply_patch`.

 - fixed `xstrip` function which never actually returned anything but `None`.

 - Added "normalize_filenames" params to `parse` (and init) function defaulting to True, to optionally prevent path normalization (which just confuses the matter for my application).


`patch` is the actual patch - I'm also attaching `patch_whitespace_indifferent` 
just for your information (since the change alters a lot of whitespace).

Original issue reported on code.google.com by [email protected] on 2 Jul 2012 at 10:02

Attachments:

@GoogleCodeExporter
Copy link
Author

Could it make sense to have some unit tests for all these changes?

Original comment by [email protected] on 21 Aug 2012 at 7:22

@GoogleCodeExporter
Copy link
Author

Perhaps - the main point is that it breaks nothing, but it would indeed be a 
good idea to ensure that the things it enables work as intended. Unfortunately 
I'm no longer planning to integrate python-patch, so it's unlikely I'll get 
around to this myself.

Original comment by [email protected] on 5 Sep 2012 at 4:54

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

No branches or pull requests

1 participant