James Moger
2012-10-22 eba89539a29deba954035056437279088c3e047b
commit | author | age
126dde 1 /*
JM 2  * Copyright 2012 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit;
17
18 import java.io.File;
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.LinkedHashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
27 import org.eclipse.jgit.storage.file.FileBasedConfig;
28 import org.eclipse.jgit.storage.file.FileRepository;
29 import org.eclipse.jgit.util.FS;
30
31 import com.beust.jcommander.JCommander;
32 import com.beust.jcommander.Parameter;
33 import com.beust.jcommander.ParameterException;
34 import com.beust.jcommander.Parameters;
35 import com.gitblit.utils.ArrayUtils;
36 import com.gitblit.utils.JGitUtils;
37 import com.gitblit.utils.StringUtils;
38
39 /**
40  * Utility class to add an indexBranch setting to matching repositories.
41  * 
42  * @author James Moger
43  * 
44  */
45 public class AddIndexedBranch {
46
47     public static void main(String... args) {
48         Params params = new Params();
49         JCommander jc = new JCommander(params);
50         try {
51             jc.parse(args);
52         } catch (ParameterException t) {
53             System.err.println(t.getMessage());
54             jc.usage();
55             return;
56         }
57         
58         // create a lowercase set of excluded repositories
59         Set<String> exclusions = new TreeSet<String>();
60         for (String exclude : params.exclusions) {
61             exclusions.add(exclude.toLowerCase());
62         }
63         
64         // determine available repositories
65         File folder = new File(params.folder);
0adceb 66         List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
126dde 67         
JM 68         int modCount = 0;
69         int skipCount = 0;
70         for (String repo : repoList) {
71             boolean skip = false;
72             for (String exclusion : exclusions) {
73                 if (StringUtils.fuzzyMatch(repo, exclusion)) {
74                     skip = true;
75                     break;
76                 }
77             }
78             
79             if (skip) {
80                 System.out.println("skipping " + repo);
81                 skipCount++;
82                 continue;
83             }
84
85             System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));
86             try {
87                 // load repository config
88                 File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);
89                 FileRepository repository = new FileRepository(gitDir);
90                 FileBasedConfig config = repository.getConfig();
91                 config.load();
92                 
93                 Set<String> indexedBranches = new LinkedHashSet<String>();
94                 indexedBranches.add(Constants.DEFAULT_BRANCH);
95                 
96                 String [] branches = config.getStringList("gitblit", null, "indexBranch");
97                 if (!ArrayUtils.isEmpty(branches)) {
98                     for (String branch : branches) {
99                         indexedBranches.add(branch);
100                     }
101                 }
102                 config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));
103                 config.save();
104                 modCount++;
105             } catch (Exception e) {
106                 System.err.println(repo);
107                 e.printStackTrace();
108             }
109         }
110         
111         System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));
112     }
113
114     
115
116     /**
117      * JCommander Parameters class for AddIndexedBranch.
118      */
119     @Parameters(separators = " ")
120     private static class Params {
121
122         @Parameter(names = { "--repositoriesFolder" }, description = "The root repositories folder ", required = true)
123         public String folder;
124
125         @Parameter(names = { "--branch" }, description = "The branch to index", required = true)
126         public String branch = "default";
127
128         @Parameter(names = { "--skip" }, description = "Skip the named repository (simple fizzy matching is supported)", required = false)
129         public List<String> exclusions = new ArrayList<String>();
130     }
131 }