1
2
3
4
5
6
7
8
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
30
31
32
33
34
35
36
37 public class DeleteTagCommand extends GitCommand<List<String>> {
38
39 private final Set<String> tags = new HashSet<>();
40
41
42
43
44
45
46
47 protected DeleteTagCommand(Repository repo) {
48 super(repo);
49 }
50
51
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
97
98
99
100
101
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 }