View Javadoc
1   /*
2    * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import java.io.IOException;
13  import java.text.MessageFormat;
14  import java.util.ArrayList;
15  import java.util.Arrays;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Set;
19  
20  import org.eclipse.jgit.api.errors.GitAPIException;
21  import org.eclipse.jgit.api.errors.JGitInternalException;
22  import org.eclipse.jgit.internal.JGitText;
23  import org.eclipse.jgit.lib.Ref;
24  import org.eclipse.jgit.lib.RefUpdate;
25  import org.eclipse.jgit.lib.RefUpdate.Result;
26  import org.eclipse.jgit.lib.Repository;
27  
28  /**
29   * Used to delete one or several tags.
30   *
31   * The result of {@link #call()} is a list with the (full) names of the deleted
32   * tags.
33   *
34   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
35   *      >Git documentation about Tag</a>
36   */
37  public class DeleteTagCommand extends GitCommand<List<String>> {
38  
39  	private final Set<String> tags = new HashSet<>();
40  
41  	/**
42  	 * Constructor for DeleteTagCommand
43  	 *
44  	 * @param repo
45  	 *            the {@link org.eclipse.jgit.lib.Repository}
46  	 */
47  	protected DeleteTagCommand(Repository repo) {
48  		super(repo);
49  	}
50  
51  	/** {@inheritDoc} */
52  	@Override
53  	public List<String> call() throws GitAPIException {
54  		checkCallable();
55  		List<String> result = new ArrayList<>();
56  		if (tags.isEmpty())
57  			return result;
58  		try {
59  			setCallable(false);
60  			for (String tagName : tags) {
61  				if (tagName == null)
62  					continue;
63  				Ref currentRef = repo.findRef(tagName);
64  				if (currentRef == null)
65  					continue;
66  				String fullName = currentRef.getName();
67  				RefUpdate update = repo.updateRef(fullName);
68  				update.setForceUpdate(true);
69  				Result deleteResult = update.delete();
70  
71  				boolean ok = true;
72  				switch (deleteResult) {
73  				case IO_FAILURE:
74  				case LOCK_FAILURE:
75  				case REJECTED:
76  					ok = false;
77  					break;
78  				default:
79  					break;
80  				}
81  
82  				if (ok) {
83  					result.add(fullName);
84  				} else
85  					throw new JGitInternalException(MessageFormat.format(
86  							JGitText.get().deleteTagUnexpectedResult,
87  							deleteResult.name()));
88  			}
89  			return result;
90  		} catch (IOException ioe) {
91  			throw new JGitInternalException(ioe.getMessage(), ioe);
92  		}
93  	}
94  
95  	/**
96  	 * Set names of the tags to delete
97  	 *
98  	 * @param tags
99  	 *            the names of the tags to delete; if not set, this will do
100 	 *            nothing; invalid tag names will simply be ignored
101 	 * @return this instance
102 	 */
103 	public DeleteTagCommand setTags(String... tags) {
104 		checkCallable();
105 		this.tags.clear();
106 		this.tags.addAll(Arrays.asList(tags));
107 		return this;
108 	}
109 }