Tuesday, 22 October 2013

Map Caps Lock to Esc in Xfce

To make Caps Lock another Esc in Xfce (e.g., Xubuntu) simply create a script like:

xmodmap -e "clear Lock"
xmodmap -e "keysym Caps_Lock = Escape"

and run it. You can add this script to the session startup to have this behavior "persist".


(Last test on Xfce 4.10, Xubuntu 13.10)

Thursday, 17 October 2013

Modifying a file in a previous Git commit

Let's say we have project with a Git master branch and a topic branch named master-awesomo; in this topic branch we have the following commits:

Commit #1: some foo 
modified foo.js

Commit #2: foobar
modified bar.py
modified foo.js

Commit #3: only bar
modified bar.py

(where Commit #3 is the latest)

The issue is that we realized that there is an issue with bar.py introduced in Commit #2; the issue is not that big to introduce a new commit, and we want to keep the changes done to foo.js. Here's what we can do:

Start an interactive rebase:

# make sure we are in the correct branch
git checkout master-awesomo 

# actually start the rebase (interactive)
git rebase master -i

Depending on Git's configuration (usually ~/.gitconfig) an editor will start up, looking something like this:

pick SHA(Commit #1) some foo
pick SHA(Commit #2) foobar
pick SHA(Commit #3) only bar

Underneath this we can find some further explanations; what we want to do is change the second line to:

edit SHA(Commit #2) foobar

save, and close the editor. A message will be displayed:

Stopped at SHA(Commit #2)... foobar

This is what we want; we are now at the second commit and we can re-edit bar.py. First, we need to unstage it:

git reset HEAD^ bar.py

Now, we can go ahead and modify the file. After that, we need to amend the commit:

# notice that we don't do a standard commit, but we amend the
# existing one, as we are rebasing 
git commit --amend

Great, all that remains is to finish the rebase:

git rebase --continue

And we are done. Some notes:

  1. During the rebase, we might get some conflicts at Commit #3 (as we modified the same file in Commit #2) and we'll have to fix them before finishing the rebase.
  2. Assuming that bar.py was added, and not modified in Commit #2, we will need to re-add it when amending the commit during the rebase.
  3. If we are going to push master-awesomo to a remote location, we will have to --force it.
  4. If we realize that we did something wrong, we can at anytime abort the rebase with:
     git rebase --abort

     This will reset everything to its initial state (original Commit #3 and sub-tree).

Wednesday, 16 October 2013

No-brainer tips for navigating large software projects in Unix/ Linux

Here are the two commands without which I could not live when working on large projects:

grep -rn 'some_text' . [--exclude=*.some_extension] [--include=*.some_extensions] [--color=auto]

Searches for some_text in all the files in the current directory (recursively) and displays the file name and the line number where the text was found (-n). Optionally, it can exclude/ include certain file patterns, and colorize the output.

find . -name '*some_text*' [-ls]

Searches for all files having some_text in their name (see man find for other options); -ls will output the files' details like, well, ls does.

Bonus: tail -fn 10 will continuously output the last ten (or how many you want) lines of a file, and is useful for watching log files.

Saturday, 12 October 2013

Display ticket descriptions in TracWiki

One way to display a link to a ticket in the TracWiki (or ant other Trac location) is to simply type #1337, where 1337 is an existing ticket; this will automatically create a link to the ticket. If you also want to include the title along with the link, simply use [[TicketQuery(id=1337)]].

Friday, 11 October 2013

Allowing multiple users to write to directory in Linux

Let's say you have in your filesystem a directory /AlicesAdventures owned by Alice, and you would like to also allow Bob to read/ write to it. You can do the following:

# create a new user group
groupadd AlicesAdventuresUsers

# add Alice to the new group
usermod -a -G AlicesAdventuresUsers Alice

# same for Bob
usermod -a -G AlicesAdventuresUsers Bob

# pass ownership of /AlicesAdventures to AlicesAdventuresUsers
# (recursive)
chgrp -R AlicesAdventuresUsers /AlicesAdventures

# make sure that all newly created files in /AlicesAdventures
# will belong to AlicesAdventuresUsers
chmod g+s /AlicesAdventures

Sure, you could also make /AlicesAdventures writeable by everyone, but do you really trust Chuck? :-)


N.b.: You may need to run the above commands as root.

Thursday, 10 October 2013

Using mock (fake) date/times in Unix/Linux

Recently I had to test an application for a strange date/ time calculation bug related to leap years. The issue with this bug was that it manifested only on the 29th of February, so you had to set the time of the system accordingly. This is where the libfaketime comes into play. After installation (most distributions have it included in their standard repositories) you can use it like this:

faketime '2012-02-29 13:33:37' some_script