1 /* 2 * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> 3 * Copyright (C) 2009, Google Inc. 4 * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org> 5 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg.lists@dewire.com> 6 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> 7 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> 8 * and other copyright owners as documented in the project's IP log. 9 * 10 * This program and the accompanying materials are made available 11 * under the terms of the Eclipse Distribution License v1.0 which 12 * accompanies this distribution, is reproduced below, and is 13 * available at http://www.eclipse.org/org/documents/edl-v10.php 14 * 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or 18 * without modification, are permitted provided that the following 19 * conditions are met: 20 * 21 * - Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 24 * - Redistributions in binary form must reproduce the above 25 * copyright notice, this list of conditions and the following 26 * disclaimer in the documentation and/or other materials provided 27 * with the distribution. 28 * 29 * - Neither the name of the Eclipse Foundation, Inc. nor the 30 * names of its contributors may be used to endorse or promote 31 * products derived from this software without specific prior 32 * written permission. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 35 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 36 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 39 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 43 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 47 */ 48 49 package org.eclipse.jgit.pgm; 50 51 import java.text.MessageFormat; 52 import java.util.List; 53 54 import org.eclipse.jgit.api.Git; 55 import org.eclipse.jgit.api.ListTagCommand; 56 import org.eclipse.jgit.api.TagCommand; 57 import org.eclipse.jgit.api.errors.RefAlreadyExistsException; 58 import org.eclipse.jgit.lib.ObjectId; 59 import org.eclipse.jgit.lib.Ref; 60 import org.eclipse.jgit.lib.Repository; 61 import org.eclipse.jgit.pgm.internal.CLIText; 62 import org.eclipse.jgit.revwalk.RevWalk; 63 import org.kohsuke.args4j.Argument; 64 import org.kohsuke.args4j.Option; 65 66 @Command(common = true, usage = "usage_CreateATag") 67 class Tag extends TextBuiltin { 68 @Option(name = "-f", usage = "usage_forceReplacingAnExistingTag") 69 private boolean force; 70 71 @Option(name = "-d", usage = "usage_tagDelete") 72 private boolean delete; 73 74 @Option(name = "-m", metaVar = "metaVar_message", usage = "usage_tagMessage") 75 private String message = ""; //$NON-NLS-1$ 76 77 @Argument(index = 0, metaVar = "metaVar_name") 78 private String tagName; 79 80 @Argument(index = 1, metaVar = "metaVar_object") 81 private ObjectId object; 82 83 /** {@inheritDoc} */ 84 @Override 85 protected void run() throws Exception { 86 try (Git git = new Git(db)) { 87 if (tagName != null) { 88 if (delete) { 89 List<String> deletedTags = git.tagDelete().setTags(tagName) 90 .call(); 91 if (deletedTags.isEmpty()) { 92 throw die(MessageFormat 93 .format(CLIText.get().tagNotFound, tagName)); 94 } 95 } else { 96 TagCommand command = git.tag().setForceUpdate(force) 97 .setMessage(message).setName(tagName); 98 99 if (object != null) { 100 try (RevWalk walk = new RevWalk(db)) { 101 command.setObjectId(walk.parseAny(object)); 102 } 103 } 104 try { 105 command.call(); 106 } catch (RefAlreadyExistsException e) { 107 throw die(MessageFormat.format( 108 CLIText.get().tagAlreadyExists, tagName)); 109 } 110 } 111 } else { 112 ListTagCommand command = git.tagList(); 113 List<Ref> list = command.call(); 114 for (Ref ref : list) { 115 outw.println(Repository.shortenRefName(ref.getName())); 116 } 117 } 118 } 119 } 120 }