How to do rollbacks in GIT (in other words - oops, I shouldn't have done that!)

This is divided into two main sections. How to rollback before the code has been committed, and how to rollback after the code has been committed.

BEFORE you have committed the code

You have already staged a change using “git add”. You don’t want it staged anymore:

git reset

$ git reset HEAD filename.txt

You haven’t staged changes yet, but want to rollback your changes to filename.txt:

git checkout

$ git checkout -- filename.txt

OK, why the two dashes??? Well, if you think about it, the ‘checkout’ command is usually used to switch branches. The two dashes tells it otherwise.

AFTER you have committed the code

You have committed code you shouldn’t have. Oh dear. You have some choices:

git revert

‘git revert’ will create a new commit which undoes a specifc commit that you pass to it. The usual use is to undo the last commit:

$ git revert HEAD

If you need to undo a commit that happened before the last commit you can pass it a caret (Bugs Bunny) or a specific SHA1 of the commit you want to undo:

$ git revert HEAD^

or:

$ git revert 16829a6f

If you rollback to a commit other than the last one, and there are merge conflicts created by undoing the change, you will have to deal with those at the time of your new commit (this revert one).

git commit —amend

‘git commit —amend’ is another command that allows you to change your last commit. For example, if I forgot to stage a file for the last commit and I need to edit the commit message:

$ git commit -m 'initial commit'
$ git add filename.txt
$ git commit --amend

It will open your default text editor and allow you to edit the comment. The previous commit is overridden by adding my new ‘filename.txt’ and now has my new commit message.

git reset —hard

This will reset the files, and the commits. Pass in the tag, branch, or commit SHA1:

$ git reset --hard (tag/branch/commit id)

Create a new branch and checkout your chosen commit into that

First, rename your master branch:

$ git branch -m mynewbranch

Check out your good commit (the one you want to use):

$ git checkout f1e4ab2b22

Make this commit you just checked out your new master branch:

$ git checkout -b master

That’s it. You still have your ‘mynewbranch’ branch, so if you want to get rid of it now, do this:

$ git branch -D mynewbranch

Note: You will probably need to run:

$ git push -f origin master

to get master to look like it did at the specified commit because of the non-fastforwards.

Filed under  //   git  

Removing CVS directories from Subversion

It’s time for a cleanout. A couple of servers here have Drupal 7 installed from CVS. As CVS is no longer used by Drupal, it’s time to clean those unneeded directories out of SVN with a mass SVN delete command

svn delete `find . -type d | grep 'CVS$'`

The command in the backticks finds everything of type directory, and then it’s passed (piped) to grep which filters the results. The $ at the end of the CVS tells us to only show root CVS directories, nothing inside them.

Filed under  //   cvs   drupal   drupal 7   subversion  

Eclipse IDE - Improving the memory hog

I stopped using Eclipse IDE quite a while back for a couple of reasons. Firstly, I wanted to explore what other IDE’s were available, and secondly I thought it was a bit of a memory hog. There is nothing worse than having to restart your IDE every couple of hours when you are deep in fixing code.

I downloaded Helios the other day from the PDT Project site which has PHP ready for use. After hunting around, I came across something I thought I’d try, and it made quite a lot of difference in the speed.

Open up the eclipse.ini file inside the eclipse folder (might be best to make a copy of it just in case!). Remove the -Xms40m and -Xmx384m lines from the bottom. Now put this in at the bottom:

-Xms128m
-Xmx512m
-XX:MaxPermSize=120m
-XX:MaxGCPauseMillis=10
-XX:MaxHeapFreeRatio=70
-XX:+UseConcMarkSweepGC
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing

Now start Eclipse (or shut it down and restart if you already had it open). Notice any difference?

So.., you may be asking, what does all of this do? The notes below are taken from this garbage collection (GC) article.

Heap size

-Xms256m
-Xmx512m

The more the better…

Perm size

IntelliJ likes a lot of memory for caching references and class meta information. That is why we need to make sure that cache is not purged. Cache is stored in ‘Old Generation’ space which size is determined by the following option

-XX:MaxPermSize=200m

General rule is to observe memory indicator after using find symbol option with include non project files selected. If it doesn’t move you have chosen enough memory.

Maximum Pause

-XX:MaxGCPauseMillis=10

We ask GC to pause the application for no more then 10 milliseconds.

Proportion of free space

-XX:MaxHeapFreeRatio=70

We ask GC to kick off when more than 30% of memory is occupied, so when we start compilation GC will not interfere because it will be enough memory free.

Concurrent Mark-Sweep (CMS) Collector

-XX:+UseConcMarkSweepGC

We want GC to run in parallel with other threads without freezing the application. This option doesn’t stop GC from freezing the application but it reduces it significantly.

Incremental Mode

-XX:+CMSIncrementalPacing

We don’t wont to freeze the application so we do GC incrementally with breaks so the application can take a breath. We use Pacing so GC can learn how to use CPU based on application CPU usage.

Filed under  //   code   development   eclipse   ide   memory   php  

Improving performance for Drupal development on localhost

I usually use virtual machines for development if I am on Windows. On this machine I have several VM's with different flavours of Linux for that purpose. However, I also have a WAMP installation which I use if I want to muck with a test Drupal site or just try out some stuff.

As a lot of people out there know, WAMP and XAMPP can really suck at performance. I remember in the past waiting 40 seconds for a page load on a Drupal site which was pretty clean, which of course is enough to make you want to rip your own hair out. After firing WAMP up today for the first time in months, I thought I'd have a hunt around to see if there were any solutions to this problem. Here are two things I found that made quite a difference on my system.

1. Adjust the realpath_cache_size in php.ini, and set it to a reasonable level. I set mine to 24 meg which is probably a bit too high, some people are saying that even 2 meg gives them improvements. In WAMP you can edit the php.ini file by clicking on the WAMP icon in your toolbar, scrolling up to the PHP section, and clicking on php.ini. Find the line that starts with ;realpath_cache_size, and uncomment it out, so it should now read:

realpath_cache_size=24M

2. Disable the IPv6 line in your Windows hosts file (note this will only work on Vista and 7, not XP). You will need to do this as adminstrator, so you may need to right click on the hosts file and choose 'open as administrator'. The file is located at C:\Windows\System32\drivers\etc
Change the line:

::1             localhost

to

#::1             localhost

and save the file. Basically all you are doing is commenting the line out with a hash (#) so it's not used.
See if that makes any difference. I noticed it made an improvement to my overall system, not just WAMP. If you see no difference, then you can always re-edit the hosts file and remove the hash to enable the line again.

 

Filed under  //   development   drupal   localhost   performance   php   server  

Theming Drupal blocks dependent on the region

The other day I was working with the menu_block module (http://drupal.org/project/menu_block), and I was trying to theme the block dependent on the region it was assigned to, to get sets of different looking menus for different page types. The hooks that are in menu_block don’t use any region info, just block ID, block name, and whether you are actually using the menu block module. So, here is an easy way to pass the block region into other dependant hooks:

Firstly, override the theme_block function, and create a global variable called $_current_region. It’s also a good idea to set the variable to null just before closing the function. Here is the standard theme_blocks function overridden in template.php:

function phptemplate_blocks($region) {
    // pass the current block region so we can use it in other hooks later
    global $_current_region;
    $_current_region = $region;

    // start of standard theme_block hook
    $output = '';

    if ($list = block_list($region)) {
        foreach ($list as $key => $block) {
            $output .= theme('block', $block);
        }
    }

    // Add any content assigned to this region through drupal_set_content() calls.
    $output .= drupal_get_content($region);

    // set region to NULL seen as we have now passed it on
    $_current_region = NULL;

    return $output;
}

So now, we can make that variable available in other functions, for example theme_menu_tree, and theme_menu_item, and you can style the menus dependant on the region. Here is theme_menu_tree:

function phptemplate_menu_tree($tree) {
    global $_current_region;

    switch ($_current_region) {
        case 'front_menu':
            return $tree;
            break;
        case 'content_page_menu':
            return '<ul class="subnavigation">' . $tree . '</ul>';
            break;
    }
}

Filed under  //   blocks   drupal   theming  

Create directory and change into it automatically

More quick command line geekery.... Create a command in .bashrc that lets you create directories and change into them immediately:

mkcd () {
  mkdir -p "$*"
  cd "$*"
}


so to use it:

mkcd directory/another/yet_another

Filed under  //   command line   linux   ubuntu  

How to open JPEG images in Camera Raw in Photoshop

Have you ever wanted to open a jpeg image in Camera Raw in Photoshop? I do it sometimes as I like working with the camera raw controls, they offer some different options to the usual set of tools in the standard Photoshop tools pallette. Be warned though, you probably won't get the same range of colour and tone control as you would do with a RAW image. You can however, make sure you set the Depth to 16 Bits/Channel in the Workflow Options inside the Camera Raw window (to get to the Workflow Options, click the link underneath the photo between the "Save Image..." and "Open Image" buttons) to get more depth.

So, here is how you do it:

1. Go to the Photoshop file menu, and click on "Open As..."
2. Browse for the photo you want to open, and highlight it (click on it once with the mouse).
3. In the "Open As" dropdown box down the bottom (underneath the "File name" box), select "Camera Raw".
4. Click "Open".

Filed under  //   camera raw   hdr   jpeg   photography   photoshop   raw  

Change permissions on all directories except SVN with the command line?

OK, a quick one. Want to set permissions on all directories except .svn ones?

So, working from your current directory down, let's say you want to make all directories writeable, and leave the svn ones alone:

find . -type d -not -name ".svn" | xargs chmod 0777

Or, if you need to exclude several directory types, you could arrange it a little bit differently:

find . -type d -print | grep -v '\(\.svn\|modules\)' | xargs chmod 0777

Filed under  //   command line   linux   ubuntu  

Victoria Memorial

Vicm

Victoria Memorial, outside Buckingham Palace. Lucky the sky was filled with Cirrus clouds that day, it adds to the effect.

Flickr page here:
http://bit.ly/9CTrTY

Filed under  //   D700   flickr   hdr   photography   photomatix   photoshop  

Downgrading to PHP 5.2 from PHP 5.3 on Ubuntu 10.04

On the Ubuntu 10.04 Lucid Lynx release, by default PHP 5.3 is installed. Great if you are using it, but it's not for everyone. Definitely not for me being a Drupal developer, as for the moment, it causes problems.

This method worked for me (however I am not responsible if it doesn't for you). All you need to do it put this lot in a shell file and run it. Remember to chmod +x file for it to run.

 

php_packages=`dpkg -l | grep php | awk '{print $2}'`
sudo apt-get remove $php_packages
sed s/lucid/karmic/g /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/karmic.list
sudo mkdir -p /etc/apt/preferences.d/
for package in $php_packages;
do echo "Package: $package
Pin: release a=karmic
Pin-Priority: 991
" | sudo tee -a /etc/apt/preferences.d/php
done
sudo apt-get update
sudo apt-get install $php_packages
sudo /etc/init.d/apache2 restart

Filed under  //   command line   downgrade   linux   php   ubuntu