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 package org.eclipse.jgit.pgm;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.Set;
43
44 import org.eclipse.jgit.errors.ConfigInvalidException;
45 import org.eclipse.jgit.errors.NotSupportedException;
46 import org.eclipse.jgit.lib.Constants;
47 import org.eclipse.jgit.lib.StoredConfig;
48 import org.eclipse.jgit.storage.file.FileBasedConfig;
49 import org.eclipse.jgit.util.FS;
50 import org.eclipse.jgit.util.StringUtils;
51 import org.eclipse.jgit.util.SystemReader;
52 import org.kohsuke.args4j.Option;
53
54 @Command(common = true, usage = "usage_getAndSetOptions")
55 class Config extends TextBuiltin {
56 @Option(name = "--system", usage = "usage_configSystem")
57 private boolean system;
58
59 @Option(name = "--global", usage = "usage_configGlobal")
60 private boolean global;
61
62 @Option(name = "--local", usage = "usage_configLocal")
63 private boolean local;
64
65 @Option(name = "--list", aliases = { "-l" }, usage = "usage_configList")
66 private boolean list;
67
68 @Option(name = "--file", aliases = { "-f" }, metaVar = "metaVar_file", usage = "usage_configFile")
69 private File configFile;
70
71 @Override
72 protected void run() throws Exception {
73 if (list)
74 list();
75 else
76 throw new NotSupportedException(
77 "only --list option is currently supported");
78 }
79
80 private void list() throws IOException, ConfigInvalidException {
81 final FS fs = getRepository().getFS();
82 if (configFile != null) {
83 list(new FileBasedConfig(configFile, fs));
84 return;
85 }
86 if (system
87 || (isListAll() && StringUtils.isEmptyOrNull(SystemReader
88 .getInstance()
89 .getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))))
90 list(SystemReader.getInstance().openSystemConfig(null, fs));
91 if (global || isListAll())
92 list(SystemReader.getInstance().openUserConfig(null, fs));
93 if (local || isListAll())
94 list(new FileBasedConfig(fs.resolve(getRepository().getDirectory(),
95 Constants.CONFIG), fs));
96 }
97
98 private boolean isListAll() {
99 return !system && !global && !local && configFile == null;
100 }
101
102 private void list(StoredConfig config) throws IOException,
103 ConfigInvalidException {
104 config.load();
105 Set<String> sections = config.getSections();
106 for (String section : sections) {
107 Set<String> names = config.getNames(section);
108 for (String name : names) {
109 for (String value : config.getStringList(section, null, name))
110 outw.println(section + "." + name + "=" + value);
111 }
112 if (names.isEmpty()) {
113 for (String subsection : config.getSubsections(section)) {
114 names = config.getNames(section, subsection);
115 for (String name : names) {
116 for (String value : config.getStringList(section,
117 subsection, name))
118 outw.println(section + "." + subsection + "."
119 + name + "=" + value);
120 }
121 }
122 }
123 }
124 }
125 }