Recent

Author Topic: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows  (Read 9739 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Hi all,

Working with the FPC supplied-version of patch.exe on Windows I got tired of fiddling with paths etc when dealing with patches so I wrote patchwrangler: a GUI that loads a patch/diff file, fixes Unix=>DOS (and vice versa) directory separators and line endings, and can guess which -p<n> option to pass to patch.exe.

If a patch was created in a subdirectory, it can also guess which one. If there are multiple candidates, the GUI allows the user to select which directory to use.

Edit: GUI improvements thanks to Risk

Also command line functionality:
--batch: never show GUI; abort if patch cannot be applied
--root=<dir>: root directory under which the patch should be applied (somewhere - using the same heuristic search if necessary as in the GUI)
--patchexe=<file>: location of patch.exe (needed if not in path)
--patch=<file>: location of the patch/diff file
--revert, --reverse: use patch --revert option to apply patches backwards

Edit: Separate completely command line program pw.exe also available. Useful for use in batch programs etc.

Release available at this mercurial repository:
https://bitbucket.org/reiniero/patchwrangler/
source and executable can be downloaded from
https://bitbucket.org/reiniero/patchwrangler/downloads

To do:
- return result code when in --batch mode
- ??? wishes and feedback, let alone patches welcome ;)


Thanks,
BigChimp

Edit: updated with new release info
Edit2: updated with new command-line options
Edit3: added Linux x86 executable to downloads
« Last Edit: July 03, 2013, 11:29:45 am by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #1 on: June 08, 2013, 10:51:32 am »
Updated first post with new release info.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #2 on: June 17, 2013, 03:33:02 pm »
Updated first post with new release info; patchwrangler now parses file paths instead of brute forcing patch dry runs which should increase speed significantly when guessing in which subdirectory a patch should be applied.

Also split up gui and non-gui parts, so changing this in a totally command line tool or reusing the code in other projects will be easier.

Thanks to ludob for the contributed code!
« Last Edit: June 17, 2013, 03:39:32 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #3 on: June 18, 2013, 12:59:15 pm »
BigChimp, how did you test your Patch Wrangler?
For me it crashes immediately when selecting a patch. The reason is a wrong parameter signature in an event handler. I fixed it, the patch is below.
I think we also found a compiler bug. The compiler should give an error about wrong param signature. I have now FPC 2.7.1.

I also forked your Mercurial repo and wanted to commit my change there but I could not. I must learn more Mercurial usage. It refused a commit because user name was not defined and I don't know yet where to put it.
Are there any nice GUI tools for Mercurial, similar with Gitk and Git gui?

Patch Wrangler's GUI is rather primitive. There are no anchors, the controls don't react to form resize. Alignment is wrong. Texts are truncated. The log memo is small and not resizable, but it has plenty of messages.
Things must be done in right order. When I first selected the patch, then my lazarus repo, it actually found the file to be patched in a subdirectory. "Apply patch" reported success but nothing was there in my repo.
After selecting my lazarus repo there was a long wait but no HourGlass cursor.
_Almost_ works. :)

Juha

Code: [Select]
diff -r 476cfa0f1885 main.pas
--- a/main.pas Mon Jun 17 12:17:49 2013 +0200
+++ b/main.pas Tue Jun 18 13:37:35 2013 +0300
@@ -67,7 +67,7 @@
     procedure ctlPatchFileEditingDone(Sender: TObject);
     procedure ctlRootDirectoryEditingDone(Sender: TObject);
     procedure ctlSearchEditingDone(Sender: TObject);
-    procedure ctlPatchFileAcceptFileName(Sender: TObject; const Value: string);
+    procedure ctlPatchFileAcceptFileName(Sender: TObject; var Value: string);
     procedure FormActivate(Sender: TObject);
     procedure FormCreate(Sender: TObject);
     procedure FormDestroy(Sender: TObject);
@@ -137,7 +137,7 @@
 
 { TForm1 }
 
-procedure TForm1.ctlPatchFileAcceptFileName(Sender: TObject; const Value: string);
+procedure TForm1.ctlPatchFileAcceptFileName(Sender: TObject; var Value: string);
 begin
   FPatcher.PatchFile := Value;
   SynMemo1.Lines.Assign(FPatcher.PatchText);
@@ -159,7 +159,7 @@
 
 procedure TForm1.FormCreate(Sender: TObject);
 var
-  PatchLocation: string;
+  PatchLocation, s: string;
 begin
   FRanOnce:=false;
   FPatcher := TPatcher.Create;
@@ -207,7 +207,8 @@
   // If default value is correct, load SynMemo:
   if (FileExistsUtf8(FPatcher.PatchFile)) then
   begin
-    ctlPatchFileAcceptFileName(Sender, FPatcher.PatchFile);
+    s := FPatcher.PatchFile;
+    ctlPatchFileAcceptFileName(Sender, s);
   end;
 
   if Application.HasOption('b', 'batch') then
« Last Edit: June 18, 2013, 01:07:34 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #4 on: June 18, 2013, 03:02:44 pm »
Thanks for the feedback, Juha.

I'm definitely not a GUI guy so any improvements welcome. I've left in the logging as we were debugging - you're right, a lot of it can be deleted (or $IFDEF DEBUGged).

Your hg username goes into a hg config file .hg.conf or something (can't remember the name now).
I use TortoiseHG on Windows - saves a lot of typing ;)

I compiled on windows the program with fpc 2.6.x (and I think the latest time I did it with stable Laz 1.0.10; I've also compiled with Laz trunk+fpc 2.7.1 - an older revision) - no problems there with any event handler.

Basically tested with the default test file in a directory c:\development which contains both an x86 and an x64 Laz install. It correctly identifies that there are two possible directories and lets you choose.

Are you testing this on Linux or Windows?

Once again, thanks for the feedback!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #5 on: June 18, 2013, 03:44:29 pm »
Are you testing this on Linux or Windows?

I am testing on 64-bit Linux. The compiler is also 64-bit but it should not matter in this case. A "var" parameter is passed differently from a "const" parameter. It requires an extra indirection (= a pointer) in the generated binary code level. I wonder how it could work for you.
And indeed, debugger was showing carbage in the parameter because there was a pointer instead of a string. Stepping into the code lead to a crash.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #6 on: June 18, 2013, 03:50:07 pm »
Well, AFAIR, that code was autogenerated by Lazarus ??!?!
Edit: Mmm, looking at it I think I probably modified it. Didn't know you weren't allowed to do that ;)

(Am in the middle of a Windows upgrade so can't look at the code right now).

...Edit: while endless upgrades roll by I've updated code... ;)
Let me know if you find anything else...
« Last Edit: June 18, 2013, 04:13:16 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #7 on: June 18, 2013, 04:11:27 pm »
Well, AFAIR, that code was autogenerated by Lazarus ??!?!
(Am in the middle of a Windows upgrade so can't look at the code right now).

Then Lazarus has (yet another) bug as well.
But no hurry...
I will go tomorrow morning to Dubrovnik for Lazarus meeting. Let's look at Patch Wrangler again after a week or so.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #8 on: July 02, 2013, 04:14:40 pm »
@Juha, no, the bug was between keyboard and chair ;)

I'd appreciate some new testing on Linux though; I've fixed some issues with relative paths not being expanded to absolute paths and the command line pw seems to work ok.
However, on my Debian unstable test machines, a call to Sysutils.GetTempFileName in patchmanagement.pas around line 369 gives an empty filename.

Very strange; a small test program does not have this flaw.

Probably too tired and messed up something simple... comments/insights welcome as usual.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #9 on: July 03, 2013, 10:31:59 am »
However, on my Debian unstable test machines, a call to Sysutils.GetTempFileName in patchmanagement.pas around line 369 gives an empty filename.

No it doesn't.
I fixed the problem in my BitBucket fork and sent you a pull request. Let't test this distributed development model finally. It has been easy so far, now only if you can merge easily then it works. :)

https://bitbucket.org/reiniero/patchwrangler/pull-request/1/fix-a-crash-when-debug-was-not-defined/diff
« Last Edit: July 03, 2013, 10:41:37 am by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #10 on: July 03, 2013, 11:01:20 am »
Thanks a lot for that Juha! Blast.... hadn't realized the {$IFDEF} could mess up program flow.

That'll teach me to finally just blindly use begin..end in all if then else constructs.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #11 on: July 03, 2013, 11:26:29 am »
Ok, with recent changes patchwrangler seems to work fine on Windows as well as Linux.

The only thing is that the FPC/Laz supplied patch.exe on Windows insists on creating backup files (.orig files) which make it more difficult to apply the patch in reverse... you'd have to remove the .orig files first. Ah well, the important functionality is there.

Testers welcome ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #12 on: July 03, 2013, 11:48:54 am »
Ok, with recent changes patchwrangler seems to work fine on Windows as well as Linux.

How did you merge my commit? Now I get merge conflicts when I push "Sync now" in BitBucket page.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #13 on: July 03, 2013, 11:50:59 am »
I rejected it and manually made the changes as it seemed to cause a merge conflict at my end.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3887
  • I like bugs.
Re: [UPDATED] Patch wrangler: deal with FPC's patch.exe on Windows
« Reply #14 on: July 04, 2013, 11:58:43 am »
I rejected it and manually made the changes as it seemed to cause a merge conflict at my end.

Interesting. There was no way to fix the merge conflict in my fork. The synchronization with your original repo happens in BitBucket server and there is no button for solving merge conflicts or anything.
I could remove my local commit with "hg rollback" but it does not help because the commit was in the server.
I deleted and recreated my fork, changed the main window layout, committed, pushed, and added another pull request for you.
Please accect it. It is a "safe" change, does not break anything. According to BitBucket documentation my fork is merged automatically then.

I am not only nitpicking but I want to learn and use this development model. I think it could be the easiest way to share contributions to any project if used properly. At least from my side thing go smoothly. I even found a TortoiseHg for Linux, made with PyQt. Cool! There is no TortoiseGit for Linux.

I think the first pull request created a rare situation. You essentially accepted my change and made a commit, but clicked a Reject button.
It resulted in a merge conflict. If you truly rejected my change (no commit) there would be no conflict.
Then what happens with a truly rejected commit? It would still stay in my forked repo which may not not be desired either. I think all commits should be done in a separate branch, at least if there is danger for rejection. Branches can be ignored later or deleted.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

 

TinyPortal © 2005-2018