kitaev@213: /* kitaev@213: * Copyright (c) 2010-2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.console; kitaev@213: kitaev@213: import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; kitaev@213: kitaev@213: import java.util.ArrayList; kitaev@213: import java.util.Collections; kitaev@213: import java.util.HashMap; kitaev@213: import java.util.LinkedList; kitaev@213: import java.util.List; kitaev@213: import java.util.Map; kitaev@213: import java.util.TreeMap; kitaev@213: kitaev@213: import org.tmatesoft.hg.core.HgRepoFacade; kitaev@213: import org.tmatesoft.hg.core.HgStatus; kitaev@213: import org.tmatesoft.hg.core.HgStatus.Kind; kitaev@213: import org.tmatesoft.hg.core.HgStatusCommand; kitaev@213: import org.tmatesoft.hg.util.Path; kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class Status { kitaev@213: kitaev@213: public static void main(String[] args) throws Exception { kitaev@213: Options cmdLineOpts = Options.parse(args); kitaev@213: HgRepoFacade hgRepo = new HgRepoFacade(); kitaev@213: if (!hgRepo.init(cmdLineOpts.findRepository())) { kitaev@213: System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation()); kitaev@213: return; kitaev@213: } kitaev@213: // kitaev@213: HgStatusCommand cmd = hgRepo.createStatusCommand(); kitaev@213: if (cmdLineOpts.getBoolean("-A", "--all")) { kitaev@213: cmd.all(); kitaev@213: } else { kitaev@213: // default: mardu kitaev@213: cmd.modified(cmdLineOpts.getBoolean(true, "-m", "--modified")); kitaev@213: cmd.added(cmdLineOpts.getBoolean(true, "-a", "--added")); kitaev@213: cmd.removed(cmdLineOpts.getBoolean(true, "-r", "--removed")); kitaev@213: cmd.deleted(cmdLineOpts.getBoolean(true, "-d", "--deleted")); kitaev@213: cmd.unknown(cmdLineOpts.getBoolean(true, "-u", "--unknonwn")); kitaev@213: cmd.clean(cmdLineOpts.getBoolean("-c", "--clean")); kitaev@213: cmd.ignored(cmdLineOpts.getBoolean("-i", "--ignored")); kitaev@213: } kitaev@213: // cmd.subrepo(cmdLineOpts.getBoolean("-S", "--subrepos")) kitaev@213: final boolean noStatusPrefix = cmdLineOpts.getBoolean("-n", "--no-status"); kitaev@213: final boolean showCopies = cmdLineOpts.getBoolean("-C", "--copies"); kitaev@213: class StatusHandler implements HgStatusCommand.Handler { kitaev@213: kitaev@213: final Map> data = new TreeMap>(); kitaev@213: final Map copies = showCopies ? new HashMap() : null; kitaev@213: kitaev@213: public void handleStatus(HgStatus s) { kitaev@213: List l = data.get(s.getKind()); kitaev@213: if (l == null) { kitaev@213: l = new LinkedList(); kitaev@213: data.put(s.getKind(), l); kitaev@213: } kitaev@213: l.add(s.getPath()); kitaev@213: if (s.isCopy() && showCopies) { kitaev@213: copies.put(s.getPath(), s.getOriginalPath()); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: public void dump() { kitaev@213: sortAndPrint('M', data.get(Kind.Modified), null); kitaev@213: sortAndPrint('A', data.get(Kind.Added), copies); kitaev@213: sortAndPrint('R', data.get(Kind.Removed), null); kitaev@213: sortAndPrint('?', data.get(Kind.Unknown), null); kitaev@213: sortAndPrint('I', data.get(Kind.Ignored), null); kitaev@213: sortAndPrint('C', data.get(Kind.Clean), null); kitaev@213: sortAndPrint('!', data.get(Kind.Missing), null); kitaev@213: } kitaev@213: kitaev@213: private void sortAndPrint(char prefix, List ul, Map copies) { kitaev@213: if (ul == null) { kitaev@213: return; kitaev@213: } kitaev@213: ArrayList sortList = new ArrayList(ul); kitaev@213: Collections.sort(sortList); kitaev@213: for (Path s : sortList) { kitaev@213: if (!noStatusPrefix) { kitaev@213: System.out.print(prefix); kitaev@213: System.out.print(' '); kitaev@213: } kitaev@213: System.out.println(s); kitaev@213: if (copies != null && copies.containsKey(s)) { kitaev@213: System.out.println(" " + copies.get(s)); kitaev@213: } kitaev@213: } kitaev@213: } kitaev@213: }; kitaev@213: kitaev@213: StatusHandler statusHandler = new StatusHandler(); kitaev@213: int changeRev = cmdLineOpts.getSingleInt(BAD_REVISION, "--change"); kitaev@213: if (changeRev != BAD_REVISION) { kitaev@213: cmd.change(changeRev); kitaev@213: } else { kitaev@213: List revisions = cmdLineOpts.getList("--rev"); kitaev@213: int size = revisions.size(); kitaev@213: if (size > 1) { kitaev@213: cmd.base(Integer.parseInt(revisions.get(size - 2))).revision(Integer.parseInt(revisions.get(size - 1))); kitaev@213: } else if (size > 0) { kitaev@213: cmd.base(Integer.parseInt(revisions.get(0))); kitaev@213: } kitaev@213: } kitaev@213: cmd.execute(statusHandler); kitaev@213: statusHandler.dump(); kitaev@213: } kitaev@213: }