Mercurial > jhg
comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Status.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
| author | Alexander Kitaev <kitaev@gmail.com> |
|---|---|
| date | Tue, 10 May 2011 10:52:53 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 212:edb2e2829352 | 213:6ec4af642ba8 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2010-2011 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.console; | |
| 18 | |
| 19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; | |
| 20 | |
| 21 import java.util.ArrayList; | |
| 22 import java.util.Collections; | |
| 23 import java.util.HashMap; | |
| 24 import java.util.LinkedList; | |
| 25 import java.util.List; | |
| 26 import java.util.Map; | |
| 27 import java.util.TreeMap; | |
| 28 | |
| 29 import org.tmatesoft.hg.core.HgRepoFacade; | |
| 30 import org.tmatesoft.hg.core.HgStatus; | |
| 31 import org.tmatesoft.hg.core.HgStatus.Kind; | |
| 32 import org.tmatesoft.hg.core.HgStatusCommand; | |
| 33 import org.tmatesoft.hg.util.Path; | |
| 34 | |
| 35 /** | |
| 36 * | |
| 37 * @author Artem Tikhomirov | |
| 38 * @author TMate Software Ltd. | |
| 39 */ | |
| 40 public class Status { | |
| 41 | |
| 42 public static void main(String[] args) throws Exception { | |
| 43 Options cmdLineOpts = Options.parse(args); | |
| 44 HgRepoFacade hgRepo = new HgRepoFacade(); | |
| 45 if (!hgRepo.init(cmdLineOpts.findRepository())) { | |
| 46 System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation()); | |
| 47 return; | |
| 48 } | |
| 49 // | |
| 50 HgStatusCommand cmd = hgRepo.createStatusCommand(); | |
| 51 if (cmdLineOpts.getBoolean("-A", "--all")) { | |
| 52 cmd.all(); | |
| 53 } else { | |
| 54 // default: mardu | |
| 55 cmd.modified(cmdLineOpts.getBoolean(true, "-m", "--modified")); | |
| 56 cmd.added(cmdLineOpts.getBoolean(true, "-a", "--added")); | |
| 57 cmd.removed(cmdLineOpts.getBoolean(true, "-r", "--removed")); | |
| 58 cmd.deleted(cmdLineOpts.getBoolean(true, "-d", "--deleted")); | |
| 59 cmd.unknown(cmdLineOpts.getBoolean(true, "-u", "--unknonwn")); | |
| 60 cmd.clean(cmdLineOpts.getBoolean("-c", "--clean")); | |
| 61 cmd.ignored(cmdLineOpts.getBoolean("-i", "--ignored")); | |
| 62 } | |
| 63 // cmd.subrepo(cmdLineOpts.getBoolean("-S", "--subrepos")) | |
| 64 final boolean noStatusPrefix = cmdLineOpts.getBoolean("-n", "--no-status"); | |
| 65 final boolean showCopies = cmdLineOpts.getBoolean("-C", "--copies"); | |
| 66 class StatusHandler implements HgStatusCommand.Handler { | |
| 67 | |
| 68 final Map<HgStatus.Kind, List<Path>> data = new TreeMap<HgStatus.Kind, List<Path>>(); | |
| 69 final Map<Path, Path> copies = showCopies ? new HashMap<Path,Path>() : null; | |
| 70 | |
| 71 public void handleStatus(HgStatus s) { | |
| 72 List<Path> l = data.get(s.getKind()); | |
| 73 if (l == null) { | |
| 74 l = new LinkedList<Path>(); | |
| 75 data.put(s.getKind(), l); | |
| 76 } | |
| 77 l.add(s.getPath()); | |
| 78 if (s.isCopy() && showCopies) { | |
| 79 copies.put(s.getPath(), s.getOriginalPath()); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 public void dump() { | |
| 84 sortAndPrint('M', data.get(Kind.Modified), null); | |
| 85 sortAndPrint('A', data.get(Kind.Added), copies); | |
| 86 sortAndPrint('R', data.get(Kind.Removed), null); | |
| 87 sortAndPrint('?', data.get(Kind.Unknown), null); | |
| 88 sortAndPrint('I', data.get(Kind.Ignored), null); | |
| 89 sortAndPrint('C', data.get(Kind.Clean), null); | |
| 90 sortAndPrint('!', data.get(Kind.Missing), null); | |
| 91 } | |
| 92 | |
| 93 private void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) { | |
| 94 if (ul == null) { | |
| 95 return; | |
| 96 } | |
| 97 ArrayList<Path> sortList = new ArrayList<Path>(ul); | |
| 98 Collections.sort(sortList); | |
| 99 for (Path s : sortList) { | |
| 100 if (!noStatusPrefix) { | |
| 101 System.out.print(prefix); | |
| 102 System.out.print(' '); | |
| 103 } | |
| 104 System.out.println(s); | |
| 105 if (copies != null && copies.containsKey(s)) { | |
| 106 System.out.println(" " + copies.get(s)); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 }; | |
| 111 | |
| 112 StatusHandler statusHandler = new StatusHandler(); | |
| 113 int changeRev = cmdLineOpts.getSingleInt(BAD_REVISION, "--change"); | |
| 114 if (changeRev != BAD_REVISION) { | |
| 115 cmd.change(changeRev); | |
| 116 } else { | |
| 117 List<String> revisions = cmdLineOpts.getList("--rev"); | |
| 118 int size = revisions.size(); | |
| 119 if (size > 1) { | |
| 120 cmd.base(Integer.parseInt(revisions.get(size - 2))).revision(Integer.parseInt(revisions.get(size - 1))); | |
| 121 } else if (size > 0) { | |
| 122 cmd.base(Integer.parseInt(revisions.get(0))); | |
| 123 } | |
| 124 } | |
| 125 cmd.execute(statusHandler); | |
| 126 statusHandler.dump(); | |
| 127 } | |
| 128 } |
