Mercurial > jhg
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tmatesoft/hg/core/HgUpdateConfigCommand.java Thu Feb 09 04:15:17 2012 +0100 @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2012 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.core; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.tmatesoft.hg.internal.ConfigFile; +import org.tmatesoft.hg.internal.Experimental; +import org.tmatesoft.hg.internal.Internals; +import org.tmatesoft.hg.repo.HgRepository; + +/** + * WORK IN PROGRESS, DO NOT USE + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +@Experimental(reason="Investigating approaches to alter Hg configuration files") +public final class HgUpdateConfigCommand extends HgAbstractCommand<HgUpdateConfigCommand> { + + private final HgRepository repo; + private final File configFile; + + private Map<String,List<String>> toRemove; + private Map<String,Map<String,String>> toSet; + + private HgUpdateConfigCommand(HgRepository hgRepo, File configurationFile) { + repo = hgRepo; + configFile = configurationFile; + } + + public static HgUpdateConfigCommand forRepository(HgRepository hgRepo) { + return new HgUpdateConfigCommand(hgRepo, new File(".hg/hgrc")); + } + + public static HgUpdateConfigCommand forUser(HgRepository hgRepo) { + return new HgUpdateConfigCommand(null, Internals.getUserConfigurationFileToWrite()); + } + + public static HgUpdateConfigCommand forInstallation() { + return new HgUpdateConfigCommand(null, Internals.getInstallationConfigurationFileToWrite()); + } + + /** + * Remove a property altogether + * @return <code>this</code> for convenience + */ + public HgUpdateConfigCommand remove(String section, String key) { + if (toRemove == null) { + toRemove = new LinkedHashMap<String, List<String>>(); + } + List<String> s = toRemove.get(section); + if (s == null) { + toRemove.put(section, s = new ArrayList<String>(5)); + } + s.add(key); + if (toSet != null && toSet.containsKey(section)) { + toSet.get(section).remove(key); + } + return this; + } + + /** + * Delete single attribute in a multi-valued property + * @return <code>this</code> for convenience + */ + public HgUpdateConfigCommand remove(String section, String key, String value) { + throw new UnsupportedOperationException(); + } + + /** + * Set single-valued properties or update multi-valued with a single value + * @return <code>this</code> for convenience + */ + public HgUpdateConfigCommand put(String section, String key, String value) { + if (toSet == null) { + toSet = new LinkedHashMap<String, Map<String,String>>(); + } + Map<String,String> s = toSet.get(section); + if (s == null) { + toSet.put(section, s = new LinkedHashMap<String, String>()); + } + s.put(key, value); + return this; + } + + /** + * Multi-valued properties + * @return <code>this</code> for convenience + */ + public HgUpdateConfigCommand add(String section, String key, String value) { + throw new UnsupportedOperationException(); + } + + public void execute() throws HgException { + try { + ConfigFile cfg = new ConfigFile(); + cfg.addLocation(configFile); + if (toRemove != null) { + for (Map.Entry<String,List<String>> s : toRemove.entrySet()) { + for (String e : s.getValue()) { + cfg.putString(s.getKey(), e, null); + } + } + } + if (toSet != null) { + for (Map.Entry<String,Map<String,String>> s : toSet.entrySet()) { + for (Map.Entry<String, String> e : s.getValue().entrySet()) { + cfg.putString(s.getKey(), e.getKey(), e.getValue()); + } + } + } + cfg.writeTo(configFile); + } catch (IOException ex) { + throw new HgInvalidFileException("Failed to update configuration file", ex, configFile); + } + } + + + public static void main(String[] args) throws Exception { + HgUpdateConfigCommand cmd = HgUpdateConfigCommand.forUser(null); + cmd.remove("test1", "sample1"); + cmd.put("test2", "sample2", "value2"); + cmd.put("ui", "user-name", "Another User <email@domain.com>"); + cmd.execute(); + } +}