login to your remote server using cd and rvmrc

Here is a fun trick which you can use, to simulate that a remote server is on one of your directories.

Create a folder call ~/m/example.com and in that folder create a file called .rvmrc with the following command

1 ssh awesomeuser@example.com -p 2302 -i ~/.ssh/awesomekey_rsa
2 #or whatever ssh command you use

Now whenever you run cd ~/m/example.com you will automagically be logged into your remote computer.

Obviously, this is a fun trick. All this can be done in a much nicer way using the ~/.ssh/config file.

Here are a few resources to make your ssh config more useful, allowing you to login to servers using a command like ssh myserver

I wonder what other stuff could be done using rvmrc.

View Comments »

make evil monkey nag you back to work

Update: fixed the cron entry

I’ve read a very interesting article about “Why programmers work at night”. One of the points the author talks about is “how we get engrossed in twitter/hacker news/reddit”. I’ve felt the same. I think one of the reasons why we(programmers/developers) spend a lot of our time on twitter/hacker news/reddit is, because, we don’t have any idea of the time. Time just flies by. So, I created a small ruby script which nags you to get back to work :)

~/.scripts/nagger

 1 #!/usr/bin/env ruby
 2 require 'time'
 3 
 4 exit if File.exists?("/tmp/stop-nagging")
 5 #see what I did here ;)
 6 
 7 #run the below command to find your display
 8 #env | grep DISPLAY
 9 ENV['DISPLAY'] = ':0.0'
10 
11 last_line = `tail -2 ~/.gtimelog/timelog.txt`.lines.map{|x| x.chomp}.reject{|x| x.empty?}.reverse.first
12 minutes = ((Time.now - Time.parse(last_line[11, 5])) / 60).round
13 evil_monkey = File.expand_path File.join(File.dirname(__FILE__), 'evil-monkey.gif')
14 
15 if minutes > 30
16   `notify-send -i '#{evil_monkey}' "It's been #{minutes} minutes since your last log"`
17 end

cron entry

1 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c '/home/minhajuddin/.scripts/nagger'

Evil monkey nagging me to get back to work

Hope it helps you get back to work too :). By the way, I use the awesome gtimelog app to log my time.

View Comments »

script to do a global search and replace in a git repository

There are many instances where I had to replace some variable name in all my files. I use a small script to do this, Hope it helps you too.

 1 #!/bin/bash
 2 #~/.scripts/git-sub
 3 #Author: Khaja Minhajuddin <minhajuddin@cosmicvent.com>
 4 #script which does a global search and replace in the git repository
 5 #it takes two arguments
 6 #e.g. git sub OLD NEW
 7 
 8 old=$1
 9 new=$2
10 
11 for file in $(git grep $old | cut -d':'  -f 1 | uniq)
12 do
13   echo "replacing '$old' with '$new' in '$file'"
14   sed -i -e "s/$old/$new/g" $file
15 done

Just remember to add it to a directory which is in the $PATH. I have it in my ~/.scripts directory which is included in the $PATH. Name it git-sub and give it executable permissions using chmod +x ~/.scripts/git-sub. Now, you can just call git sub old_var new_var on terminal and it will do a global search and replace of all the files in the repository.

View Comments »

elegance of functional programming

Functional programming allows you to write concise and elegant code. Mainstream languages like Ruby and C# support a lot of functional programming paradigms, and learning them makes you a better programmer. Below is a small example which demonstrates that:

 1 #6 lines of ugly code
 2 i = 0
 3 tasks = list.tasks
 4 while(i < tasks.length - 2)
 5   tasks[i].priority.should >= tasks[i + 1].priority
 6   i += 1
 7 end
 8 
 9 
10 #3 lines of elegant functional code
11 list.tasks.each_cons(2).each do |t1, t2|
12   t1.priority.should >= t2.priority
13 end
View Comments »

gc your git repositories automatically with a cron task

I have a lot of git code repositories, and I usually gc (garbage collect) them manually by running the git gc command every now and then. Tasks like these are prime candidates for automating with cron. Below is a cron entry and the script which gcs my repositories. Hope you guys find it useful.

the script

 1 #!/bin/bash
 2 #author: Khaja Minhajuddin
 3 #email: minhajuddin.k@gmail.com
 4 #path /home/minhajuddin/.cron/reboot.sh
 5 #description: script which is executed everytime computer starts
 6 
 7 #git gc repos
 8 REPO_DIRS=$(cat <<EOS
 9 $HOME/repos
10 $HOME/repos/core
11 EOS
12 )
13 
14 for repo_dir in $REPO_DIRS
15 do
16   echo "checking for git repos in $repo_dir"
17   for repo in $(ls $repo_dir)
18   do
19     cd $repo_dir/$repo
20     if [[ -d .git ]]
21     then
22       echo "garbage collecting $repo"
23       git gc
24     fi
25   done
26 done

the crontab entry

1 $ crontab -e
2 #add the line below into the editor and save it
3 @reboot   $HOME/.cron/reboot.sh

Bonus tip: If you have a gitosis server, put the following script at ~git/.cron/reboot.sh and perform the above step for your git user.

the gitosis git user script

1 #!/bin/bash
2 
3 for repo in $(ls ~/repositories)
4 do
5   cd ~/repositories/$repo
6   echo "garbage collecting $repo"
7   git gc
8 done
View Comments »