Quickhacks: Git Swap: Swapping the two topmost commits

Maybe my workflow is broken, but I often have the problem that I need to swap the last two commits I did to my git repository. That is due to the fact that my last commit is usually what I’m really working on. I know I could use more branches, but still. When I’m cleaning up this commit, e.g. to get it past all the static (Findbugs, Checkstyle) checks bevore publishing it, I sometimes come along stuff that is broken or ugly but has nothing to do what I’m currently doing. Not beeing able to leave wrong code untouched I fix it and commit it away. But now the last commit is not what I’m “–amend“ing to anymore. What I always wanted to do is “git swap“, to just swap the commit before the last one to the top of my chain. I know that I can do a combination of git rebase –i or git reset, but I like stuff to be simple. I opted to write a script that rewrites the git-rebase-todo in order to make handling of conflicts more straight forward. So now I can do this:

$ mkdir git_swap_test
$ cd git_swap_test/
$ git init
Initialized empty Git repository in /tmp/git_swap_test/.git/
$ echo 'foo' > file1
$ git add file1
$ git commit -m 'First Commit'
[master (root-commit) 14b55a7] First Commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1
$ echo 'bar' > file2
$ git add file2
$ git commit -m 'Second Commit'
[master c9f97e4] Second Commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2
$ echo 'baz' > file3
$ git add file3
$ git commit -m 'Third Commit'
[master 40f98d8] Third Commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file3
$ git log --format=oneline
40f98d8bfbc4edaede576e18b7f4fa20dfde02bf Third Commit
c9f97e42e2ecd0821e3edf45c4dd599540972b46 Second Commit
14b55a753ea9ed291ec511d42effdf190e44ad09 First Commit
$ git swap
Reversing all commits up to HEAD~2
Successfully rebased and updated refs/heads/master.
$ git log --format=oneline
569fd69259bcf7ca8a4a25fe4c3e82e860e995d8 Second Commit
7414eee241877e0f0c0d4d317c4f06384bd27715 Third Commit
14b55a753ea9ed291ec511d42effdf190e44ad09 First Commit
$ git swap
Reversing all commits up to HEAD~2
Successfully rebased and updated refs/heads/master.
$ git log --format=oneline
300c358d4d6e2b26a4f06c41e438068a7d6b630e Third Commit
87ac4cf567bcab51a57c1599de8b5faee1b7cfb7 Second Commit
14b55a753ea9ed291ec511d42effdf190e44ad09 First Commit

What I did to do this is first write a small Shellscript that basically overrides GIT_EDITOR with my own Perl implementation:
https://gist.github.com/1325749.js?file=git-swapThis is what makes it possible to call “git swap“. Acutally, you could call it with any commitish, in order to swap up to there, but that might not be what you want.  It just defaults to the two topmost commits. The real work is done in the swapper.pl file:https://gist.github.com/1325749.js?file=swapper.pl
This reads the git-rebase-todo (which comes as the first command line parameter), reads all it’s lines and reverses the order. That is what git rebase -i uses to change the order of the commits. That’s it. Now I save at least 5 seconds per extra commit, because that is the time my Emacs needs to boot up ;).

More History

Today I found out that use.perl.org is shutting down, or at least it is not accepting any new posts anymore. As I had my first blog on use.perl.org I wanted to preserve the data. This was quite easy as I just had to enhance a little script that I found on perlmonks.org in order to  export the text of the use.perl.org entries to create an RSS file which I just imported here.  Thus my blog history now starts in September 2003, which is more than 8 years ago.

The code is here:

 

 

FrOSCon 2007: Call For Papers

The second Free and Open Source Conference “FrOSCon” takes place on August, 25th and 26th 2007 in Sankt Augustin, near Bonn, Germany. The conference is once again hosted by the faculty of computer science of the University of Applied Sciences Bonn-Rhein-Sieg in collaboration with the student body and the Linux/Unix User Group Sankt Augustin.

In a Call for Papers, the organizers ask for submission of contributions. A broad variety of topics concerning free and open source software is desired.

Contributions can be submitted in English or German and should summarize the intended talk concisely and succinctly; talks should be no longer than 45 minutes in length. Deadline for submission is June, 4th 2007.

PerlPlanet

I hate to always criticise other people’s work, but Planet is a piece of crap. I tried it on my favourite blogs to fire up friends.thiesen.org and besides not working on Atom feeds as it should it completely died on another feed and had encoding issues. I tried to fix it, but it appears as if the feed reader library they are using is far too old (they are using 2.2 something, 4.0-pre is the curernt version) and it is Python and I can’t do Python.

PerlPlanet to the rescue. Because there was no other implementation of a Planet like thing and I thought it couldn’t be that hard, I wrote a replacement myself. PerlPlanet can do everything except the Hackergotchi feature while staying compatible with Planets own templates and configuration files. Turns out it is even 500 lines less, though you have to install quite a bunch of Modules, but you should be used to that by now.

Get it here.

P.S.: WordPress 2.0 Beta rocks…

JSON & JavaScript Speed

Was macht man wenn man clientseitig viele Daten verarbeiten möchte? Kein XML parsen, das ist total out, weil XML ja nicht native ist. Dafür gibt es jetzt JSON, die JavaScript Object Notation, die ist wie XML ohne den ganzen DTD schnickschnack und sowieso, man kann es einfach eval’en. Das geht dann auch für 3943 Einträge ala Autor, Titel, Ausgabe, Seiten. Damit man jetzt nicht seine ganzen XML Daten (oder sonstwie mehr oder minder strukturierte Daten) von hand umwandeln muss gibt es JSON für Perl aber auch eigentlich für alles andere. Hat mich auf jeden Fall begeistert, für Sachen die im Browser laufen sollen auf jeden Fall das richtige Datenmodell und richtig schön KISSig.

Wenn man dann allerdings knapp 4000 Einträge in einem Browser laufen lässt und vor allem daraus auch noch ein volles Listing in Tabellenform macht (also 4000 <tr><td> in den DOM Baum einbaut) hat man eher einen Browser Benchmark als sonstwas (das initiale Parsen ist kaum merklich, ich nehme mal stark an das die Browser rendern und so Sachen wie JavaScript Namespace aufbauen asynchron machen). And the winner is… Opera. Ja, ich muss zugeben ich war auch etwas überrascht. Zumindest auf Linux ist es ganz klar Opera der das Ding am schnellsten Angezeigt bekommt. Danach kommt Firefox (1.5 RC1) und dann Konqueror, wenn er nicht gerade nach 5 Sekunden Meckert das ein JavaScript das Anzeigen der Seite verhindert. Mein Windows Test gestern war nicht wirklich fair, da ich das Ding auf dem alten 600 Herzer meine Ma hab laufen lassen, da kam dann der IE erst nach 5 Minuten wieder. Naja, das Suchen geht überraschend performant, es geht hier wohl sehr viel Zeit in das Rendern.

Ferner hat mir das rumgespiele mit JavaScript und DOM mal wieder gezeigt was für tolle Sachen sich doch alles clienseitig basteln lassen und das sogar ohne AJAX. Ich könnte mich jetzt hier hinstellen und das ende von Perl, Ruby und PHP als Websprache vorhersagen, aber das wäre glaube ich etwas verfrüht. Auf jeden Fall ist es für den Benutzer viel freundlicher, wenn man nicht über tausend Seiten geleitet wird nur damit die ganzen Post Queries durchkommen und es ist auf jeden Fall schneller, den Client die Sachen rechnen zu lassen.

Zusammenfassung: JSON ist cool als Datenformat, Opera ist der schnellste JavaScript Auswerter/Renderer auf Linux und weg vom Server, zurück zum Client.

fuseftp: Implementing userspace filesystems with Perl

Update 2011: Well, I haven’t done anything with Perl in the last couple of years, but to update this Page: FuseFTP is now up on Github, you can fork it there.

Well, as I mentioned earlier, 2.6.14 has this nifty new FUSE feature, allowing the implementation of a file system in userspace. Unfortunately, there was no implementation of a FTP filesystem. Fortunately, there are Perl bindings for FUSE :-).

One can implement a userspace filesystem using the whole power of Perl, and that makes it quite easy. I implemented a basic ftp filesystem this morning, which uses Fuse.pm and Net::FTP to allow read only access to ftp servers. It works quite well and was really easy, so this might be the beginning of a couple of nice userspace filesystems written in Perl. I could actually create MegasusFS….

BTW, you can download fuseftp from my Github Account.

Usage is quite easy, you can simply:

marcus@hal2000:~: mkdir kernel
marcus@hal2000:~: fuseftp kernel/ ftp.kernel.org &
Successfully logged into ftp.kernel.org
[1] 7150
marcus@hal2000:~: cd kernel/
marcus@hal2000:~/kernel: ls
for_mirrors_only  lost+found  pub  usr  welcome.msg
marcus@hal2000:~/kernel: cd ..
marcus@hal2000:~: fusermount -u kernel/
[1]+  Done                    fuseftp kernel/ ftp.kernel.org

Net::SILC::Client

Once upon a time there was a Linux user group which switched it’s chatting system to SILC. Unfortunately there was no Chatbot available for SILC and the user group had to live without. Suddently, in the beginning of ’05, somebody had the time to write a bot, called Megasus, which runs inside the Irssi client, which is used by SILC as well. From now on the chat was enriched by nice information provided by a Bot. But Megasus had a problem, (actually the Irssi client when you call fork() inside of it) and it was leaking memory like hell, until the bad Adminstrators where forced to put memory and runtime limits on the machine. This lead to Megasus dying a horrible death every week and really annoyed the author. But then…

Well, I have been looking for real Perl bindings for the SILC Client API for quite a while. The problem is that there are none. Apparantly, nobody took it’s time to write something which would allow something like:

use Net::SILC::Client;
use Chatbot::Eliza;

my $bot = new Chatbot::Eliza( );

sub on_channel_message {
    my ($self, $channel, $nick, $message) = @_;

    if ($message =~ $self->nickname) {
        my $answer = $bot->transform($message);

        $self->channel_message($channel, 
                               $nick . ': '  . $answer );
    }
}

my $silc = Net::SILC::Client->new (channels => [ 'silc' ],
                                   nickname => 'NSC');

$silc->set_callback('channel_message', &on_channel_message);

$silc->connect;

A few days ago, I stumbled over SILC.pm a Sourceforge Project which attempted to create a Perl wrapper for the SILC API. They hadn’t released any code and as the project was founded in 2002 I contacted the authors and asked if they had already done some work, because I started believing that I would have to do this myself. They where really friendly and they had code, but only wrapping some of SILCs Math functions and not really what I needed. So I started writing from scratch.

As of today, you can run the above example and it does what it has to, plus there are a couple of other events that you can handle directly from Perl. As this is my first Perl/C/PerlXS Module I believe that it is leaking memory like hell as well as there are a couple of other problems (vanishing scalars between two function calls is one of them). But it supports all important events (you can set callbacks as this whole system is event driven), lets you issue commands and send private and channel messages. What I need to do to make it a possible frontend for Megasus is that I need an other Event System (for things like fetching RSS feeds) and some other minor fixes, but the major part is done. The Event System is a little bit tricky, as I tried to use Event.pm and running the SILC Main Loop with it which crashed horibly and I’m still not really good in debugging this stuff. And of course, it is lacking documentation (there are two examples which show the main methods as well as some information about the events, but this is not really good). It’s still far away from something I’d release to the CPAN, but If you want to have a look, it’s all in the CVS on Sourceforge.