RSS

Search Engine

Monday, November 22, 2010

Mercurial usage with the command line

3.1. Overview

The following will be a short Eclipse Java development cycle using the command line interface of mercurial.

You will first create a new Mercurial repository, create a new Java project and commit the changes to your mercurial repository.

The command for mercurial is "hg".

3.2. Prepare

Check that you are using Mercurial version 1.3 or higher:

    
hg --version

Create and switch to your desired work directory .

Initialize a new mercurial repository with:

    

hg clone https://yourproject.googlecode.com/hg/ name_of_directory_for_the_clone

Tip

If you have an existing project you could clone, e.g. create a copy of, your repository via the following command.

      

hg clone https://yourproject.googlecode.com/hg/ name_of_directory_for_the_clone

Eclipse creates a lot of metadata with regards to the current status of the Eclipse workbench. Also Eclipse creates compiled version in the directory bin. This data should not be placed in the mercurial repository. Create the file ".hgignore" in the same directory as the hg project.

    
# switch to regexp syntax.
syntax: regexp
^\.metadata
^de.vogella.mercurial.firstproject/bin$

3.3. Create Java Project

Start Eclipse and use the new directory as workspace. Create a new Java project "de.vogella.mercurial.firstproject". Create the following Java class.

    
package de.vogella.mercurial.firstproject;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello Mercurial");
}

}

3.4. Add Project to Mercurial and commit

Add the project to Mercurial.

    
hg commit -A -m 'Commit with automatic addremove'


Commit.

    
hg commit -m "Create new project"

If you have checked out the Mercurial project from another site you can push your changes to the repository.

    
hg push

Check the history via:

    
hg log

3.5. Making changes

Rename your Java class to "TestNewName" and create a new class "Test2".

        
package de.vogella.mercurial.firstproject;

public class Test2 {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello Mercurial");
System.out.println("This is a new class");
}

}


Add the the new files to Mercurial.

    
hg commit -A -m 'Commit with automatic addremove'


A rename in Java removes the old file. Mercurial will report these files as missing (hg status). You can use the following command to automatically add the new files and remove the deleted.

Add the the new files to Mercurial.

    
hg addremove

You can now commit. The -A flag in the commit command would also automatically add and remove new files.

    
hg add .

3.6. See changes

To see the changes use the status command. diff we show the differences.

    
hg log
hg diff

3.7. Revert

You can revert the changes via "hg revert".

    
hg revert de.vogella.mercurial.firstproject/src/de/vogella/mercurial/firstproject/Test.java

0 comments:

Post a Comment