Streams of thought by...

This is the (tumble) log by Innerfusion, I'll post various things about code, design, politics, or whatever is interesting at the moment.

1/6/2010
3:21pm
Comments (View)

Quick convenience methods for String

I’m wondering why this isn’t in Core?

  # should be in Ruby Core
  class String
    # grab the first character
    def first
      self[0..0]
    end

    # grab the last character
    def last
      self[(self.length-1)..(self.length-1)]
    end
  end

Grab the gist

Posted in: ruby snippets
11/17/2009
3:51pm
Comments (View)

Get the current branch in a git repo

Ever need to get the current branch name in a ruby app?

b = `git branch`.split("\n").delete_if { |i| i.first != "*" }
b.first.gsub("* ","")

Or maybe you might want to put it into ~/.irbrc:

def branch
   b = `git branch`.split("\n").delete_if { |i| i.first != "*" }
   b.first.gsub("* ","")
end

Posted in: snippets ruby git
10/22/2009
11:40am
Comments (View)

Compiling and installing git on centos 5

I recently had to compile the latest version of git on CentOS 5, here’s what worked for me.

  ## Compiling and installing git 1.6.x on Centos5

    # if you need to get dependencies
    sudo yum install curl-devel 
    sudo yum install expat-devel 
    sudo yum install gettext-devel 
    sudo yum install openssl-devel 
    sudo yum install zlib-devel
    sudo yum install wget

    # you are using /usr/local/ right?
    cd /usr/local/src/
    wget http://kernel.org/pub/software/scm/git/git-1.6.5.1.tar.gz
    tar zxvf git-1.6.5.1.tar.gz
    cd git-1.6.5.1
    make configure
    ./configure --prefix=/usr/local
    sudo ./configure --prefix=/usr/local
    sudo make all
    sudo make install

Grab the gist

Posted in: centos git snippets
9/10/2009
12:30pm
Comments (View)

How git rm . should behave

So when you do:

  git add .

It automatically adds files to your index ready to be committed right? But when I did:

  git rm .

I assumed it would remove files/folders that you have removed from the filesystem, kinda like the inverse of how git add . should be. But it doesn’t, it gives you something that about not removing the directory you are currently working in. Here’s what it says in the git manual about git rm:

Remove files from the index, or from the working tree and the index. git-rm will not remove a file from just your working directory. (There is no option to remove a file only from the work tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When —cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

Notice they say use bin/rm to remove a file from the work tree and still keep it in the index… what? Ok, but for me I’ll usually just want to remove a file/directory from both the work tree and the index, it just puts more work on the developer to manually have to remove it from the git index when you have just removed it from the filesystem. Granted it’s a simple git rm whatever/file/you/deleted/ but once you multiply that by a bunch of files in directories you have removed, it becomes tedious. So what do we do?

Sed, Grep and Bash to the rescue!

By using the power of sed and grep you can now freely delete your files and directories as you wish without worrying about manually git rm-ing everything. I decided to write a quick bash function to give me that behavior, drop this in your ~/.bash_profile or ~/.bashrc and you’ll be good to go:

# removes all deleted files / directories 
# kinda like what git rm . should do 
git_rm_all (){
  git st | grep deleted | sed -e 's/deleted: *//' | sed 's/# *//' | xargs git rm
}

Fork the gist

Posted in: git code bash snippets
9/9/2009
10:51am
Comments (View)

Running rsync with sudo

Rsync can probably be the best deployment tool (among other things) to use if you consider how fast and efficient it is. Lately however, I’ve run into issues on needing to rsync into directories that required sudo privileges, luckily google gave me a bit of help when figuring out how to do it.

Here is a version that simply sends sudo to the server and prepends rsync with sudo:

# run rsync with sudo
stty -echo; ssh you@hostname 'sudo -v'; stty echo
rsync -avz --rsync-path='sudo rsync' ~/location/on/your/computer you@hostname:~/location/on/server

You’ll obviously want to replace you@hostname and the location paths.

Fork the gist

Posted in: snippets rsync linux deployment
9/1/2009
10:18am
Comments (View)

Jquery window close prompt

UPDATE: Download this from github!

I can’t believe it took me as long as it did to figure this out in jquery,this proves I need more practice. It does one simple thing: prompts you when the window is closed.

$(document).ready(function() {
  // unbind all anchors from causing the window.beforeunload event
  $('a').each(function(index) {
    $(this).click(function() {
      $(window).unbind('beforeunload');
    });
  });
  
});

$(window).bind("beforeunload", function(e){
  msg = "Are you sure that you want to close?";
  return e.returnValue = msg; 
});

Fork the gist

Posted in: jquery snippets code
8/31/2009
4:00am
Comments (View)

Less is more

I’m a big fan of the less command, which allows you to read files similar to vim, except without the editing capability. I use it a ton to quickly scan through text files and even programmatically push text through it. When I use script/console, I sometimes find it handy to use less as a quick way to view a bunch of content or even a nice way to output arrays. Simply add the following method to ~/.irbrc :

    # hook for the system's less command, allows you to pass in arrays too!
    # EX : 
    #     less "A bunch of content"
    #     less %w(1 2 3) 
    #     less [1,2,3]
    def less(content)
      content = content.join("\n") if content.is_a?(Array)
      system('echo "' << content << '"|less')
    end

fork the gist

Posted in: snippets code ruby rails
8/5/2009
1:55pm
Comments (View)

Git and irbrc sitting in a tree…

Using git and rails together? Need some git love in your console? Here’s a list of command git commands that I often use, abstracted into a method, just throw this in ~/.irbrc :

  # git hook
  def git(*args)
    if args.length == 1
      # EX : git :st 
      #      git "push origin master"
      cmd = "git #{args.first.to_s}"
    elsif args.length > 1
      # EX : git :st, :origin, :master
      #      git :push, :origin
      cmd = args.first.to_s
      args.delete_at(0)
      arg = args.join(" ")
      cmd = "git #{cmd} #{arg}"
    elsif args.blank?
      cmd = "git [*cmd] or git.[st|ci|push|config]"
    end
    
    cmd.instance_eval do
      
      # git.st
      def st
        system("git st")
      end
      
      # git.ci 'msg'
      def ci(msg)
        system("git add .;git commit -m '#{msg}'")
      end
      
      # git.push 'args' 
      def push(args=nil)
        args.nil? ? system("git push") : system("git push #{args}")
      end
      
      # git.url
      def url
        system 'cat .git/config | grep url'
      end
      
      # git.config
      def config
        system 'vi .git/config'
      end
      
    end
    
    args.blank? ? cmd : system(cmd)
  end

Now you can do things like:

  # git status
  >> git.st 
  >> git :st
 
  # git push 'msg'
  >> git.push 'msg'
  >> git :push 'msg'

  # you can even modify the config in vi from the console
  >> git.config

Go ahead and fork the gist at : http://gist.github.com/162857

Posted in: git rails ruby snippets
6/24/2009
1:22am
Comments (View)

Attaching files with ActionMailer

I was having issues attaching files in emails with ActionMailer, I couldn’t find any reference to any of the options that you can pass to the attachment method for ActionMailer, so I am posting this snippet for my own reference:

View the snippet

Posted in: rails ruby snippets