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
72 @Override
73 protected void run() throws Exception {
74 if (list)
75 list();
76 else
77 throw new NotSupportedException(
78 "only --list option is currently supported");
79 }
80
81 private void list() throws IOException, ConfigInvalidException {
82 final FS fs = getRepository().getFS();
83 if (configFile != null) {
84 list(new FileBasedConfig(configFile, fs));
85 return;
86 }
87 if (system
88 || (isListAll() && StringUtils.isEmptyOrNull(SystemReader
89 .getInstance()
90 .getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))))
91 list(SystemReader.getInstance().openSystemConfig(null, fs));
92 if (global || isListAll())
93 list(SystemReader.getInstance().openUserConfig(null, fs));
94 if (local || isListAll())
95 list(new FileBasedConfig(fs.resolve(getRepository().getDirectory(),
96 Constants.CONFIG), fs));
97 }
98
99 private boolean isListAll() {
100 return !system && !global && !local && configFile == null;
101 }
102
103 private void list(StoredConfig config) throws IOException,
104 ConfigInvalidException {
105 config.load();
106 Set<String> sections = config.getSections();
107 for (String section : sections) {
108 Set<String> names = config.getNames(section);
109 for (String name : names) {
110 for (String value : config.getStringList(section, null, name))
111 outw.println(section + "." + name + "=" + value);
112 }
113 if (names.isEmpty()) {
114 for (String subsection : config.getSubsections(section)) {
115 names = config.getNames(section, subsection);
116 for (String name : names) {
117 for (String value : config.getStringList(section,
118 subsection, name))
119 outw.println(section + "." + subsection + "."
120 + name + "=" + value);
121 }
122 }
123 }
124 }
125 }
126 }