[Gambas-user] IDE patch functionality

Tobias Boege taboege at ...626...
Sat Jan 19 23:22:58 CET 2013


On Sat, 19 Jan 2013, Beno?t Minisini wrote:
> Le 19/01/2013 15:40, Tobias Boege a ?crit :
> > Hi,
> >
> > I added the following items under the "Make" menu in the IDE (my working
> > copy):
> >
> > - "Generate patch..." to take an old source archive and generate a diff
> >    relative to the current project
> > - "Apply patch..." to apply a previously generated patch to the currently
> >    open project (the "old" one from above)
> > - "Revert patch..." the inverse operation of "Apply patch..."
> >
> > One may then have a software-1.0.0.tar.gz archive and generate a patch for
> > it to ascend to the latest version. I used my modified IDE to create a diff
> > between itself and the unmodified IDE source code archive, applied and
> > reverted it without errors.
> >
> > This may not be the usual Gambas development pattern but I found it
> > particularly useful the last days. If anyone is interested, I will add more
> > advanced error handling and perhaps commit it to the official repository if
> > Benoit is content with it.
> >
> > Regards,
> > Tobi
> >
> 
> I need to see it to give my opinion. Of course if there is no error 
> handling, I will not be happy. :-)
> 

OK. So here it is:

 1. Make a .tar.gz archive of your working copy IDE (rev#5511)
 2. Copy the attached patch into app/src/gambas3/
 3. $ patch -p1 <gambas3-rev5511-add-patch-functionality.patch
 4. Start the patched IDE and make it open itself
 5. Project -> Make -> Patches -> Generate patch... and select the source
    archive from above to generate a patch against it and save that patch
 6. Unpack the source archive to somewhere else
 7. With the patched IDE, open the unpatched one, open FMain.class and do
    Project -> Make -> Patches -> Apply patch..., then select the patch
 8. Watch it being applied
 9. Revert it
10. Change FMain.class so that the patch utility will reject the patch
11. Try to apply again
12. Watch how nothing is changed. If one file fails, the backups of all
    successfully patched files are reloaded again to not go into an
    inconsistent state of the project. You get the complaints from patch.
13. Remove your changes and apply, then commit ;-)

This is mostly how I created and tested the attached patch.

I could commit the changes myself but I wanted to show you how such a patch
will look like because I'm not sure about this issue: a generated patch will
contain some paths from the author's local filesystem. I don't know how to
solve this efficiently.

The best solution would be to copy the current project, similar to how it is
done with the old project, to the same temporary directory and make a diff
from the temporary directory. This way, one would have

--- old-source.tmp/.src/FMain.class
+++ new-source.tmp/.src/FMain.class

in the patch's header. I momentarily took the more efficient way which has
instead:

--- old-source.tmp/.src/FMain.class
+++ /home/tab/Desktop/gambas3/.src/FMain.class

and exposes the location of the newer files.

You certainly have some objections, don't you? :-)

Regards,
Tobi
-------------- next part --------------
diff -urNa old-source.tmp/.src/FMain.class /home/tab/Desktop/gambas3/.src/FMain.class
--- old-source.tmp/.src/FMain.class	2012-12-19 13:19:34.000000000 +0100
+++ /home/tab/Desktop/gambas3/.src/FMain.class	2013-01-19 22:48:54.343993842 +0100
@@ -2202,3 +2202,70 @@
   wrkProject.Picture = Project.GetBackground()
   
 End
+
+Public Sub mnuGeneratePatch_Click()
+
+  Dim sOld As String = Temp$("old-source")
+  ' Dim aIgnore As String[] = Project.GetIgnoreFiles()
+  Dim sIgnore As String = ""
+  Dim sCmd As String
+  Dim sPatch As String
+
+  ' If aIgnore.Count Then
+  '   sIgnore = "-x \"*" & aIgnore.Join("\" -x \"*") & "\""
+  ' Endif
+
+  Dialog.Title = ("Select the older source archive...")
+  Dialog.Filter = ["*.tar.gz", ("Gambas source archive")]
+  If Dialog.OpenFile(False) Then Return
+  Project.Save()
+  Project.CleanUp()
+
+  Shell Subst$("tar -zxPf &1 --xform='s_^[^/]\\+_&2_'", Shell$(Dialog.Path), sOld) Wait
+  sCmd = Subst$("cd &1/..; ", Shell$(sOld))
+  sCmd &= Subst$("diff -urNa &1 &2 &3", sIgnore, Shell$(File.Name(sOld)), Shell$(Project.Dir))
+  Shell sCmd To sPatch
+  Shell "rm -r " & Shell$(sOld)
+
+  Dialog.Title = "Save the patch..."
+  Dialog.AutoExt = True
+  Dialog.Filter = ["*.patch;*.diff", ("Diff/Patch files")]
+  If Dialog.SaveFile() Then Return
+  File.Save(Dialog.Path, sPatch)
+
+End
+
+Private Sub DoPatch(Optional sOpt As String)
+
+  Dim sCmd As String
+  Dim sBackup As String = Temp$("backup") & "/"
+  Dim sRes As String
+
+  Dialog.Title = ("Select patch...")
+  Dialog.Filter = ["*.patch;*.diff", ("Diff/Patch files")]
+  If Dialog.OpenFile(False) Then Return
+  Try Mkdir sBackup
+  sCmd = Subst$("cd &1; patch &2 -B &3 -r - ", Shell$(Project.Dir), sOpt, sBackup)
+  sCmd &= Subst$("-bsNp1 <&1", Shell$(Dialog.Path))
+  Shell sCmd To sRes
+  If Process.LastValue Then
+    Shell Subst$("cp -r $(find &1 -mindepth 1 -print) &2", sBackup, Shell$(Project.Dir)) Wait
+    Message.Error(("Patch didn't apply:\n") & sRes)
+  Else
+    Project.ReloadAll()
+  Endif
+  Shell "rm -r " & sBackup
+
+End
+
+Public Sub mnuApplyPatch_Click()
+
+  DoPatch()
+
+End
+
+Public Sub mnuRevertPatch_Click()
+
+  DoPatch("-R")
+
+End
diff -urNa old-source.tmp/.src/FMain.form /home/tab/Desktop/gambas3/.src/FMain.form
--- old-source.tmp/.src/FMain.form	2013-01-02 00:23:44.000000000 +0100
+++ /home/tab/Desktop/gambas3/.src/FMain.form	2013-01-19 22:48:56.890661102 +0100
@@ -98,6 +98,21 @@
         Picture = Picture["icon:/small/package"]
         Shortcut = "Ctrl+Alt+I"
       }
+      { mnuPatch Menu
+        Text = ("Patches...")
+        { mnuGeneratePatch Menu
+          Text = ("Generate patch...")
+          Picture = Picture["icon:/16/add"]
+        }
+        { mnuApplyPatch Menu
+          Text = ("Apply patch...")
+          Picture = Picture["icon:/16/redo"]
+        }
+        { mnuRevertPatch Menu
+          Text = ("Revert patch...")
+          Picture = Picture["icon:/16/undo"]
+        }
+      }
     }
     { Menu15 Menu
     }


More information about the User mailing list