comparison src/org/tmatesoft/hg/core/HgUpdateConfigCommand.java @ 378:9fb990c8a724

Investigate approaches to alter Mercurial configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 Feb 2012 04:15:17 +0100
parents
children 82336b7c54f4
comparison
equal deleted inserted replaced
377:86f049e6bcae 378:9fb990c8a724
1 /*
2 * Copyright (c) 2012 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.core;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.tmatesoft.hg.internal.ConfigFile;
27 import org.tmatesoft.hg.internal.Experimental;
28 import org.tmatesoft.hg.internal.Internals;
29 import org.tmatesoft.hg.repo.HgRepository;
30
31 /**
32 * WORK IN PROGRESS, DO NOT USE
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 @Experimental(reason="Investigating approaches to alter Hg configuration files")
38 public final class HgUpdateConfigCommand extends HgAbstractCommand<HgUpdateConfigCommand> {
39
40 private final HgRepository repo;
41 private final File configFile;
42
43 private Map<String,List<String>> toRemove;
44 private Map<String,Map<String,String>> toSet;
45
46 private HgUpdateConfigCommand(HgRepository hgRepo, File configurationFile) {
47 repo = hgRepo;
48 configFile = configurationFile;
49 }
50
51 public static HgUpdateConfigCommand forRepository(HgRepository hgRepo) {
52 return new HgUpdateConfigCommand(hgRepo, new File(".hg/hgrc"));
53 }
54
55 public static HgUpdateConfigCommand forUser(HgRepository hgRepo) {
56 return new HgUpdateConfigCommand(null, Internals.getUserConfigurationFileToWrite());
57 }
58
59 public static HgUpdateConfigCommand forInstallation() {
60 return new HgUpdateConfigCommand(null, Internals.getInstallationConfigurationFileToWrite());
61 }
62
63 /**
64 * Remove a property altogether
65 * @return <code>this</code> for convenience
66 */
67 public HgUpdateConfigCommand remove(String section, String key) {
68 if (toRemove == null) {
69 toRemove = new LinkedHashMap<String, List<String>>();
70 }
71 List<String> s = toRemove.get(section);
72 if (s == null) {
73 toRemove.put(section, s = new ArrayList<String>(5));
74 }
75 s.add(key);
76 if (toSet != null && toSet.containsKey(section)) {
77 toSet.get(section).remove(key);
78 }
79 return this;
80 }
81
82 /**
83 * Delete single attribute in a multi-valued property
84 * @return <code>this</code> for convenience
85 */
86 public HgUpdateConfigCommand remove(String section, String key, String value) {
87 throw new UnsupportedOperationException();
88 }
89
90 /**
91 * Set single-valued properties or update multi-valued with a single value
92 * @return <code>this</code> for convenience
93 */
94 public HgUpdateConfigCommand put(String section, String key, String value) {
95 if (toSet == null) {
96 toSet = new LinkedHashMap<String, Map<String,String>>();
97 }
98 Map<String,String> s = toSet.get(section);
99 if (s == null) {
100 toSet.put(section, s = new LinkedHashMap<String, String>());
101 }
102 s.put(key, value);
103 return this;
104 }
105
106 /**
107 * Multi-valued properties
108 * @return <code>this</code> for convenience
109 */
110 public HgUpdateConfigCommand add(String section, String key, String value) {
111 throw new UnsupportedOperationException();
112 }
113
114 public void execute() throws HgException {
115 try {
116 ConfigFile cfg = new ConfigFile();
117 cfg.addLocation(configFile);
118 if (toRemove != null) {
119 for (Map.Entry<String,List<String>> s : toRemove.entrySet()) {
120 for (String e : s.getValue()) {
121 cfg.putString(s.getKey(), e, null);
122 }
123 }
124 }
125 if (toSet != null) {
126 for (Map.Entry<String,Map<String,String>> s : toSet.entrySet()) {
127 for (Map.Entry<String, String> e : s.getValue().entrySet()) {
128 cfg.putString(s.getKey(), e.getKey(), e.getValue());
129 }
130 }
131 }
132 cfg.writeTo(configFile);
133 } catch (IOException ex) {
134 throw new HgInvalidFileException("Failed to update configuration file", ex, configFile);
135 }
136 }
137
138
139 public static void main(String[] args) throws Exception {
140 HgUpdateConfigCommand cmd = HgUpdateConfigCommand.forUser(null);
141 cmd.remove("test1", "sample1");
142 cmd.put("test2", "sample2", "value2");
143 cmd.put("ui", "user-name", "Another User <email@domain.com>");
144 cmd.execute();
145 }
146 }