src/com/vectrace/MercurialEclipse/commands/HgTagClient.java
author Jerome Negre <jerome+hg@jnegre.org>
Thu Mar 27 21:38:33 2008 +0100 (15 months ago)
changeset 180 ac8797d619e4
parent 177a6a4d9cf2982
permissions -rw-r--r--
1st version of "Tag as Version..."
        1 package com.vectrace.MercurialEclipse.commands;
        2 
        3 import java.util.regex.Matcher;
        4 import java.util.regex.Pattern;
        5 
        6 import org.eclipse.core.resources.IProject;
        7 import org.eclipse.core.resources.IResource;
        8 
        9 import com.vectrace.MercurialEclipse.exception.HgException;
       10 import com.vectrace.MercurialEclipse.model.Tag;
       11 
       12 public class HgTagClient {
       13 
       14 	private static final Pattern GET_TAGS_PATTERN = Pattern
       15 			.compile("^(.+[^ ]) +([0-9]+):([a-f0-9]+)( local)?$");
       16 	
       17 	public static Tag[] getTags(IProject project) throws HgException {
       18 		HgCommand command = new HgCommand("tags", project, false);
       19 		command.addOptions("-v");
       20 		String[] lines = command.executeToString().split("\n");
       21 		int length = lines.length;
       22 		Tag[] tags = new Tag[length];
       23 		for(int i=0; i<length; i++) {
       24 			Matcher m = GET_TAGS_PATTERN.matcher(lines[i]);
       25 			if (m.matches()) {
       26 				Tag tag = new Tag(
       27 						m.group(1),
       28 						Integer.parseInt(m.group(2)),
       29 						m.group(3),
       30 						m.group(4)!=null
       31 						);
       32 				tags[i] = tag;
       33 			} else {
       34 				throw new HgException("Parse exception: '"+lines[i]+"'");
       35 			}
       36 		}
       37 		return tags;
       38 	}
       39 	
       40 	/**
       41 	 * 
       42 	 * @param resource
       43 	 * @param name
       44 	 * @param user if null, uses the default user
       45 	 * @param local
       46 	 * @throws HgException
       47 	 */
       48 	public static void addTag(IResource resource, String name, String rev, String user, boolean local, boolean force) throws HgException {
       49 		HgCommand command = new HgCommand("tag", resource.getProject(), false);
       50 		if(local) {
       51 			command.addOptions("-l");
       52 		}
       53 		if(force) {
       54 			command.addOptions("-f");
       55 		}
       56 		if(rev != null) {
       57 			command.addOptions("-r", rev);
       58 		}
       59 		command.addUserName(user);
       60 		command.addOptions(name);
       61 		command.executeToBytes();
       62 	}
       63 	
       64 }