creating a patch using git

creating a patch using git

by Ravishankar Somasundaram -
Number of replies: 17
Dear moodlers,

Recently i ran into this challenge of creating a patch file for my changes made between commits.

according to moodle docs on git patch "git diff commitid1 commitid2 > patch.txt" didnt do the trick.

As the changes i did in commits were

1. Introduced new images(.png or .gif files) in commits
2. Created directory structures and created new files inside them.

can somebody tell me what is the procedure to include and preserve these in my patch ?

Average of ratings: -
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Penny Leach -
Hi,

I think the correct syntax is git diff commitid1..commitid2 (note two dots)

Hope that helps
Average of ratings: Useful (1)
In reply to Penny Leach

Re: creating a patch using git

by Martín Langhoff -

If you have binary files (those PNGs), you want to add --binary:

git diff --binary commit1..commit2

But I tend to export them with

git format-patch --binary commit1..commit2

which gives you one patch per commit.

Average of ratings: Useful (1)
In reply to Martín Langhoff

Re: creating a patch using git

by Ravishankar Somasundaram -
Thank you Penny and Martin for replying,

I created the patch using --binary but not able to test it as it fails to apply,

this is what i did

git diff --binary d6d26242d560183db9326ef8cbee6c1ac22a1e20..e7c9d7be8511f74a8b8a0ace87b5fa78e223fb0a > /var/www/patch.txt
Goku@Namek:/var/www/retest$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)
Goku@Namek:/var/www/retest$ git checkout applypatch
Switched to branch 'applypatch'
Goku@Namek:/var/www/retest$ git status
# On branch applypatch
nothing to commit (working directory clean)
Goku@Namek:/var/www/retest$ sudo git apply /var/www/patch.txt
[sudo] password for Goku:
/var/www/patch.txt:377: trailing whitespace.
//Ravi => to enable adding roles in meta courses
/var/www/patch.txt:431: trailing whitespace.

/var/www/patch.txt:487: trailing whitespace.

/var/www/patch.txt:494: trailing whitespace.

/var/www/patch.txt:495: trailing whitespace.

error: patch failed: admin/roles/assign.php:1
error: admin/roles/assign.php: patch does not apply
error: patch failed: config.php:10
error: config.php: patch does not apply



In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Martín Langhoff -
"fails to apply" means that there are other changes in those files that conflict with the changes you are trying to apply...
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Shouldn't you reset your new branch to 'origin/master' before applying the patch?

Otherwise you'll be patching an already patched branch, won't you?

Saludos, Iñaki.

In reply to Iñaki Arenaza

Re: creating a patch using git

by Ravishankar Somasundaram -
In this case master has the applied patches and thew ne branch called "applypatch" has unapplied code
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Hubert Chathi -
Are they using the same repository? You may want to use "git merge" instead. Git is made to handle these things for you.
In reply to Hubert Chathi

Re: creating a patch using git

by Ravishankar Somasundaram -
Dear Hubert chathi,


Am not trying to implement changes from one branch to another, i created a seperate branch and checkout to old code to test the patch that was created in master branch with latest code.
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Hubert Chathi -
If it's a single commit (or a small number of commits) that you're testing, then "get cherry-pick" is useful.
In reply to Hubert Chathi

Re: creating a patch using git

by Ravishankar Somasundaram -
Dear Hubert chathi,

I am trying to create a patch and validate whether it is properly patching all my customizations so that i can upload the patch here for easier use for people.

please confirm whether cherry pick will suit this scenario
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Hubert Chathi -
If you want to test a patch for other users to use, then no, cherry-pick will not work. If that is what you are trying to do, then you need to either use patch directly ("patch < /var/www/patch.txt") or git apply.
In reply to Hubert Chathi

Re: creating a patch using git

by Ravishankar Somasundaram -
either use patch directly ("patch

is some text that you intended to post got missed in above reply hubert ?
In reply to Ravishankar Somasundaram

Re: creating a patch using git

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Actually, that is Moodle, which deletes everything after the first < character when cleaning input.

The point is that you need to use the command line patch tool, or another tool that can apply a patch.
In reply to Tim Hunt

Re: creating a patch using git

by Ravishankar Somasundaram -
Dear Tim,

i used git apply to apply patch that i have created and faced problems, have posted the output that i got in previous replies.

dont know how to proceed. many people feel that the display is not providing much of info for them to help.

but even i am not able to find any other information for its failure to post it here.

please share your ideas regarding this.
In reply to Hubert Chathi

Re: creating a patch using git

by Hubert Chathi -
ok, an alternate command, without the character that Moodle filters out (Tim how did you get it to show up in your post?): "cat /var/www/patch.txt | patch -p1"

I've never used git apply, but patch usually tells you what chunks are conflicting, and creates *.rej files that show the conflicting chunks.

Anyways, from the output you posted so far, it looks like the patch is trying to modify admin/roles/assign.php and config.php and is encountering conflicts in those files.
Average of ratings: Useful (1)
In reply to Hubert Chathi

Re: creating a patch using git

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You can type &lt;, if you are in safari, or another browser that does not use the HTML editor.
In reply to Hubert Chathi

Re: creating a patch using git

by Ravishankar Somasundaram -
Dear Hubert chathi,

thanks for that, really helped.

Face another challenge now, i used
git format-patch --binary commitid1..commitid5

but this patch file dosent seem to create my pix folder and contents of it when applied.