Your Ad Here '

My Git workflow

I first attempted to use Git almost a year ago. I tried to use it as a kind of rsync’er between computers for my personal projects+data folder. It didn’t work too well: I think it was primarily built with the idea of versioning and syncing source files, not thousands of WAV sample files scattered around thousands of nested folders. So I left “Start using Git” in my to-do list and almost forgot about it.

Recently I was about to start a project where I didn’t know quite well what direction it was going to take. So I was there, experimenting and hacking code here and there while investigating where would it bring us, when I realised that I would like to have a snapshot of the code at that point, before continuing with the next part. The classic solution would have been to make a .zip of the code folder, or a copy of the folder in the above directory, and continue. But if you want to compare between versions, manually calling diff between file versions is cumbersome. I didn’t want to add the code to the subversion repository because it was by no means ready yet for anything. So I though: why don’t I use git for this?

First steps were a bit uncertain, but I quickly developed a simple yet effective command line technique. Yeah, no IDE plug-in whatsoever, I am just using a bash terminal. It works pretty much like this:

  • code code code
  • looks good! let’s store this…
  • git add file1.cpp file2.cpp ………. or git add . for adding all changed files to the commit
  • git commit (so that I can enter more than one line of commit message, in the editor that shows up) or git commit -m ‘commit message’ for one liners.

Sometimes, when I want to see what has changed, I use gitk. It is admittedly ugly but once you get it, it’s nicer than reading plain diff output in a terminal screen. Specially because you can navigate between changes, instead of having to manually invoke diff for each file you want to compare.

At a certain point I wanted to make a huge code change, so I made a new branch. Actually, I haven’t gone back to the other branch since then, but it’s good to know it’s there in case I want to see how the code is in that totally incompatible code and class-wise approach to the problem.

Probably one of the things I like more is that everything is lightning fast. Since the repository is stored locally, there is no more waiting for commits to finish or contacting the server for getting a diff. Even if your Subversion server is in your local network, it will never beat the experience of a localhost, direct disk access based repository. This way, one doesn’t get distracted with the check in/commit process; it becomes an almost automatic action so the mind can be focused in the real problem: the code!

If you’re an advanced git user, you might have already realised that I am only using a very reduced set of git features. Whether that’s sad or not, I leave it up to you; I just wanted to highlight the notion that although git is generally used (or was designed) for huge projects with lots of collaborators branching and merging intensively and all that, it is actually very good for producing what I call code sketches, and it doesn’t need to be used by several users with all the clone, pull, push and rest of single-worded verbs paraphernalia to enjoy its power.

Please don’t understand this post as a subversion sucks, use git square-headed litany, but rather as a friendly git could be quite useful, if you have some spare time you might consider having a look at it suggestion from a long-time Subversion user who knows Git is intimidating but worth the effort to learn :D

NetBeans’ “Unable to resolve identifier std” error

Tried to use some C++ STL includes to no avail. But it was odd, NetBeans was underlining my include’s with a red groovy line, and still managed to compile successfully (obvious, because gcc is the compiler, not NB).

So this:

#include <string>

and

std::string m_strTest;

were both underlined, and a “Unable to resolve identifier std” error appeared to the left, with the classic question mark with red background signal.

I was using NB 6.7; tried with NB 6.8 Beta and the problem persisted.

But turns out it was a very simple fix. Thanks to this post which suggested to use CTRL+ALT when hovering over include’s, I found out that several include paths such as /usr/lib/include/c++/4.3 weren’t found (they were marked in red, not surprisingly). So if netbeans couldn’t find those directories, it wasn’t able to determine whether std:: was a proper identifier or not.

Simple test:

sole@courgette:~$ ls -la /usr/include/c++
total 20
drwxr-xr-x  3 root root  4096 2009-11-01 11:47 .
drwxr-xr-x 65 root root 12288 2009-11-24 17:18 ..
drwxr-xr-x 10 root root  4096 2009-11-01 11:22 4.4
lrwxrwxrwx  1 root root     3 2009-11-01 11:22 4.4.1 -> 4.4

ahh so it’s not 4.3 but 4.4!

So I went to Tools -> Options -> C/C++ -> Code Assistance and replaced the occurrences of 4.3 with 4.4 where appropriate. All worked perfectly from that point onwards.

It’s strange; I don’t remember having updated GCC recently (apart from the update to Karmic Koala). In any case I hadn’t been using NetBeans with C++ and std recently either, so maybe the problem was there waiting for me to stumble upon it :-)

Linking with ffmpeg’s libav

Every single tutorial linked from ffmpeg’s documentation suggests using simple library linking switches when compiling against libav, for example:

gcc -o main.o main.c -lavcodec -lavformat

but for whatever the reason, that didn’t work in my case. I was just getting a bunch of undefined references to the functions that I was using in my code. Horrible.

After browsing and searching for hours I eventually stumbled upon a post in libav mailing list* that suggested the use of pkg-config, so I typed this in:

gcc -o main.o main.c `pkg-config --cflags --libs libavformat libswscale`

and voilà, my program was compiled with libavformat and libswscale in it!

It is pretty similar to SDL’s sdl-config, and thankfully you can even use both at the same time:

gcc -o main.o main.c `pkg-config --cflags --libs libavformat libswscale` `sdl-config --cflags --libs`

The only thing I have noticed is that this is statically linking and therefore entirely including those libraries into the executable, so I’m ending with an executable which is 40 Megabytes in size. Not that I am worried about statically linking, but I wonder if it’s possible to only include the functions it’s really using or needs.

Bonus: libav with C++

By the way, now that we are on helping people so that they don’t waste an entire afternoon looking for info, if you want to use libav in a C++ program, be sure to surround the includes with the traditional extern C block, so that C++ doesn’t mangle what it doesn’t have to mangle. For example:

#ifdef __cplusplus
extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
}
#endif

The sublibraries you include depend entirely on what you want to do in your project. I.e. you could include libavcodec or not, libswscale or not, and so on.

*By the way, let me use this opportunity to bash against mailing lists as the main support asset in a project. Nowadays they are probably the worst idea ever, right after newsgroups, of course. I tried posting a message to the libav list and it was rejected, days after, because “non members cannot post to the list”. Hooray for accessibility, proper sorting of topics, searchability and etc.

Delicatessen v2

Let me introduce Delicatessen v2, the new version of my WordPress plug-in for finding out who’s bookmarked your posts in delicious.com

It has taken a bit too long to produce this version, but it at last behaves like a nice netizen, and won’t spam delicious.com with too many queries in a very little time, therefore you won’t get banned ;)

Delicatessen

More info and downloads in the Delicatessen page.

Too many open files

I am doing something that requires manipulating a lot of files, and I fell in the classical too many open files error trap.

A way of finding out which files are being used by a process is to type

ps -ax

in a terminal, then identify the guilty process and its PID. Let’s imagine its PID is 9090. Now to list every one of its open files you just run this (again, in the terminal):

lsof -p 9090

Or you can get a raw estimate by piping that through wc and getting the number of lines in the return value of lsof:

lsof -p 9090 | wc -l

That will return a number, like for example “33″.

It’s interesting to know that the output from lsof does not only show files in the windows way of referring to a file as a folder or data written in the disk, but returns files in the UNIX way, i.e., everything is a file, including pipes, sockets, files-files, etc.

I have changed my code meanwhile to be a bit more austere in regards to the number of open files, but this is an interesting tool nevertheless. If you run it for example with Firefox, you can even see which font files Firefox is using:

...
firefox 3847 sole  mem    REG                8,1   224692   272248 /usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf
firefox 3847 sole  mem    REG                8,1   622020     6209 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf
...

Or which plug-ins has it loaded:

...
firefox 3847 sole  mem    REG                8,1   101536    13774 /usr/lib/mozilla/plugins/libtotem-cone-plugin.so
firefox 3847 sole  mem    REG                8,1   117960      741 /var/lib/flashplugin-installer/npwrapper.libflashplayer.so
...

etc etc :)