Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab45bd4d16adf599995c954e36c04741f6491c56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.core.internal.console;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import org.eclipse.osee.console.admin.Console;
import org.eclipse.osee.console.admin.ConsoleCommand;
import org.eclipse.osee.console.admin.ConsoleParameters;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.OrcsBranch;
import org.eclipse.osee.orcs.core.internal.branch.BranchUtil;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.search.BranchQuery;
import org.eclipse.osee.orcs.search.QueryFactory;

/**
 * @author Roberto E. Escobar
 */
public final class BranchPurgeCommand implements ConsoleCommand {

   private OrcsApi orcsApi;
   private static final String ERROR_STRING =
      "Branch %s[%s] is a %s branch and that option was not specified!  It will not be purged!\n";

   public void setOrcsApi(OrcsApi orcsApi) {
      this.orcsApi = orcsApi;
   }

   public OrcsApi getOrcsApi() {
      return orcsApi;
   }

   @Override
   public String getName() {
      return "branch_purge";
   }

   @Override
   public String getDescription() {
      return "Permenatly remove all branches matching the passed in criteria";
   }

   @Override
   public String getUsage() {
      // includeChildren excludes baseline branches
      StringBuilder sb = new StringBuilder();
      sb.append("Usage: branch_purge [-R] [-D] [-A] [-B] [-P] branchUuids=<BRANCH_UUID,..>\n");
      sb.append("Synopsis:\n");
      sb.append("\tCAUTION: This command will permanently remove branches from the datastore!\n");
      sb.append("\tThis command has no effect unless the [P] option is specified, otherwise it\n");
      sb.append("\tprints what would be purged had the [P] option been specified.  Each option turns\n");
      sb.append("\tfunctionality on.  Thus, the default behavior with no options is to list the \n");
      sb.append("\tarchived, deleted, non-baseline branches and not recurse branch children.\n");
      sb.append("Options:\n");
      sb.append("\tR: Recurse branch children\n");
      sb.append("\tA: Allow Un-Archived branches\n");
      sb.append("\tD: Allow Un-Deleted branches\n");
      sb.append("\tB: Allow Baseline branches\n");
      sb.append("\tP: Purge the branches\n");

      return sb.toString();
   }

   @Override
   public Callable<?> createCallable(Console console, ConsoleParameters params) {
      List<Long> branchUuids = new ArrayList<Long>();
      Arrays.asList(params.getArray("branchUuids"));

      if (branchUuids.isEmpty()) {
         console.writeln("No branch guids where specified");
      }

      Collection<String> options = params.getOptions();
      boolean recurse = options.contains("R");
      boolean unArchived = options.contains("A");
      boolean unDeleted = options.contains("D");
      boolean baseline = options.contains("B");
      boolean runPurge = options.contains("P");

      OrcsBranch orcsBranch = getOrcsApi().getBranchOps(null);
      return new PurgeBranchCallable(console, orcsBranch, getOrcsApi().getQueryFactory(null), branchUuids, recurse,
         unArchived, unDeleted, baseline, runPurge);
   }

   private static class PurgeBranchCallable extends CancellableCallable<List<IOseeBranch>> {

      private final Console console;
      private final OrcsBranch orcsBranch;
      private final List<Long> branchUuids;
      private final boolean recurse;
      private final boolean includeUnarchived;
      private final boolean includeUndeleted;
      private final boolean includeBaseline;
      private final boolean runPurge;
      private final QueryFactory queryFactory;

      public PurgeBranchCallable(Console console, OrcsBranch orcsBranch, QueryFactory queryFactory, List<Long> branchUuids, boolean recurse, boolean unArchived, boolean unDeleted, boolean baseline, boolean runPurge) {
         this.console = console;
         this.orcsBranch = orcsBranch;
         this.queryFactory = queryFactory;
         this.branchUuids = branchUuids;
         this.recurse = recurse;
         this.includeUnarchived = unArchived;
         this.includeUndeleted = unDeleted;
         this.includeBaseline = baseline;
         this.runPurge = runPurge;
      }

      private boolean filterBranch(BranchReadable branch) {
         if (!includeBaseline && branch.getBranchType() == BranchType.BASELINE) {
            console.writeln(ERROR_STRING, branch, branch.getGuid(), branch.getBranchType());
            return true;
         } else if (!includeUnarchived && branch.getArchiveState() == BranchArchivedState.UNARCHIVED) {
            console.writeln(ERROR_STRING, branch, branch.getGuid(), branch.getArchiveState());
            return true;
         } else if (!includeUndeleted && branch.getBranchState() != BranchState.DELETED) {
            console.writeln(ERROR_STRING, branch, branch.getGuid(), branch.getBranchState());
            return true;
         }
         return false;
      }

      private Collection<BranchReadable> getBranchesToPurge() throws OseeCoreException {
         Set<BranchReadable> specifiedBranches = new HashSet<BranchReadable>();
         for (Long uuid : branchUuids) {
            if (uuid > 0) {
               console.write("UUID listed %s is not a valid UUID", uuid);
            } else {
               BranchReadable cached = queryFactory.branchQuery().andUuids(uuid).getResults().getExactlyOne();
               if (cached != null) {
                  specifiedBranches.add(cached);
               }
            }
         }

         Collection<BranchReadable> branchesToPurge =
            recurse ? getChildBranchesToPurge(specifiedBranches) : specifiedBranches;

         Iterator<BranchReadable> iter = branchesToPurge.iterator();
         while (iter.hasNext()) {
            if (filterBranch(iter.next())) {
               iter.remove();
            }
         }
         return branchesToPurge;
      }

      private Collection<BranchReadable> getChildBranchesToPurge(Iterable<BranchReadable> branches) throws OseeCoreException {

         BranchQuery branchQuery = queryFactory.branchQuery();
         branchQuery.includeArchived();
         branchQuery.includeDeleted();

         if (includeBaseline) {
            branchQuery.andIsOfType(BranchType.WORKING, BranchType.MERGE, BranchType.PORT, BranchType.BASELINE);
         } else {
            branchQuery.andIsOfType(BranchType.WORKING, BranchType.MERGE, BranchType.PORT);
         }

         if (includeUndeleted) {
            branchQuery.andStateIs(BranchState.CREATED, BranchState.MODIFIED, BranchState.COMMITTED,
               BranchState.REBASELINED, BranchState.DELETED, BranchState.REBASELINE_IN_PROGRESS,
               BranchState.COMMIT_IN_PROGRESS, BranchState.CREATION_IN_PROGRESS, BranchState.DELETE_IN_PROGRESS,
               BranchState.PURGE_IN_PROGRESS, BranchState.PURGED);
         } else {
            branchQuery.andStateIs(BranchState.DELETED);
         }

         Set<BranchReadable> results = new HashSet<BranchReadable>();
         for (BranchReadable parent : branches) {
            for (BranchReadable branch : branchQuery.andIsChildOf(parent).getResults()) {
               if (includeUnarchived || branch.getArchiveState() == BranchArchivedState.ARCHIVED) {
                  results.add(branch);
               }
            }
         }

         if (recurse) {
            results.addAll(getChildBranchesToPurge(new ArrayList<BranchReadable>(results)));
         }
         return results;
      }

      @Override
      public List<IOseeBranch> call() throws Exception {
         Collection<BranchReadable> branchesToPurge = getBranchesToPurge();
         branchesToPurge.addAll(getMergeBranches(branchesToPurge));

         Conditions.checkNotNull(branchesToPurge, "branchesToPurge");
         if (branchesToPurge.isEmpty()) {
            console.writeln("no branches matched specified criteria");
         } else {
            List<BranchReadable> orderedBranches = BranchUtil.orderByParentReadable(queryFactory, branchesToPurge);

            for (IOseeBranch toPurge : orderedBranches) {
               console.writeln("Branch [%s] guid [%s] will be purged!", toPurge.getName(), toPurge.getGuid());
            }

            List<IOseeBranch> purged = new LinkedList<IOseeBranch>();
            if (runPurge) {
               int size = orderedBranches.size();
               int count = 0;
               for (IOseeBranch aBranch : orderedBranches) {
                  console.writeln("Purging Branch [%s of %s]: [%s]", ++count, size, aBranch);
                  Callable<List<IOseeBranch>> callable = orcsBranch.purgeBranch(aBranch, false);
                  purged.addAll(callable.call());
               }
            }
            return purged;
         }
         return null;
      }

      private List<BranchReadable> getMergeBranches(Collection<BranchReadable> branches) throws OseeCoreException {
         List<BranchReadable> mergeBranches = new ArrayList<BranchReadable>();
         for (BranchReadable branch : branches) {
            for (BranchReadable child : queryFactory.branchQuery().andIsChildOf(branch).andIsOfType(BranchType.MERGE).getResults()) {
               mergeBranches.add(child);
            }
         }
         return mergeBranches;
      }
   }
}

Back to the top