tikhomirov@64: /*
tikhomirov@64: * Copyright (c) 2011 TMate Software Ltd
tikhomirov@64: *
tikhomirov@64: * This program is free software; you can redistribute it and/or modify
tikhomirov@64: * it under the terms of the GNU General Public License as published by
tikhomirov@64: * the Free Software Foundation; version 2 of the License.
tikhomirov@64: *
tikhomirov@64: * This program is distributed in the hope that it will be useful,
tikhomirov@64: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@64: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@64: * GNU General Public License for more details.
tikhomirov@64: *
tikhomirov@64: * For information on how to redistribute this software under
tikhomirov@64: * the terms of a license other than GNU General Public License
tikhomirov@102: * contact TMate Software at support@hg4j.com
tikhomirov@64: */
tikhomirov@64: package org.tmatesoft.hg.core;
tikhomirov@64:
tikhomirov@74: import static org.tmatesoft.hg.repo.HgRepository.TIP;
tikhomirov@64:
tikhomirov@157: import java.io.IOException;
tikhomirov@64: import java.util.Calendar;
tikhomirov@64: import java.util.Collections;
tikhomirov@64: import java.util.ConcurrentModificationException;
tikhomirov@64: import java.util.LinkedList;
tikhomirov@64: import java.util.List;
tikhomirov@64: import java.util.Set;
tikhomirov@64: import java.util.TreeSet;
tikhomirov@64:
tikhomirov@154: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
tikhomirov@129: import org.tmatesoft.hg.repo.HgChangelog;
tikhomirov@80: import org.tmatesoft.hg.repo.HgDataFile;
tikhomirov@74: import org.tmatesoft.hg.repo.HgRepository;
tikhomirov@157: import org.tmatesoft.hg.util.ByteChannel;
tikhomirov@157: import org.tmatesoft.hg.util.CancelledException;
tikhomirov@133: import org.tmatesoft.hg.util.Path;
tikhomirov@64:
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@131: * Access to changelog, 'hg log' command counterpart.
tikhomirov@131: *
tikhomirov@64: *
tikhomirov@131: * Usage:
tikhomirov@70: * new LogCommand().limit(20).branch("maintenance-2.1").user("me").execute(new MyHandler());
tikhomirov@64: *
tikhomirov@131: * Not thread-safe (each thread has to use own {@link HgLogCommand} instance).
tikhomirov@64: *
tikhomirov@64: * @author Artem Tikhomirov
tikhomirov@64: * @author TMate Software Ltd.
tikhomirov@64: */
tikhomirov@131: public class HgLogCommand implements HgChangelog.Inspector {
tikhomirov@64:
tikhomirov@64: private final HgRepository repo;
tikhomirov@64: private Set users;
tikhomirov@64: private Set branches;
tikhomirov@64: private int limit = 0, count = 0;
tikhomirov@64: private int startRev = 0, endRev = TIP;
tikhomirov@64: private Calendar date;
tikhomirov@77: private Path file;
tikhomirov@80: private boolean followHistory; // makes sense only when file != null
tikhomirov@193: private ChangesetTransformer csetTransform;
tikhomirov@195: private HgChangelog.ParentWalker parentHelper;
tikhomirov@80:
tikhomirov@131: public HgLogCommand(HgRepository hgRepo) {
tikhomirov@107: repo = hgRepo;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@148: * Limit search to specified user. Multiple user names may be specified. Once set, user names can't be
tikhomirov@148: * cleared, use new command instance in such cases.
tikhomirov@64: * @param user - full or partial name of the user, case-insensitive, non-null.
tikhomirov@64: * @return this
instance for convenience
tikhomirov@148: * @throws IllegalArgumentException when argument is null
tikhomirov@64: */
tikhomirov@131: public HgLogCommand user(String user) {
tikhomirov@64: if (user == null) {
tikhomirov@64: throw new IllegalArgumentException();
tikhomirov@64: }
tikhomirov@64: if (users == null) {
tikhomirov@64: users = new TreeSet();
tikhomirov@64: }
tikhomirov@64: users.add(user.toLowerCase());
tikhomirov@64: return this;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@64: * Limit search to specified branch. Multiple branch specification possible (changeset from any of these
tikhomirov@148: * would be included in result). If unspecified, all branches are considered. There's no way to clean branch selection
tikhomirov@148: * once set, create fresh new command instead.
tikhomirov@64: * @param branch - branch name, case-sensitive, non-null.
tikhomirov@64: * @return this
instance for convenience
tikhomirov@148: * @throws IllegalArgumentException when branch argument is null
tikhomirov@64: */
tikhomirov@131: public HgLogCommand branch(String branch) {
tikhomirov@64: if (branch == null) {
tikhomirov@64: throw new IllegalArgumentException();
tikhomirov@64: }
tikhomirov@64: if (branches == null) {
tikhomirov@64: branches = new TreeSet();
tikhomirov@64: }
tikhomirov@64: branches.add(branch);
tikhomirov@64: return this;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: // limit search to specific date
tikhomirov@64: // multiple?
tikhomirov@131: public HgLogCommand date(Calendar date) {
tikhomirov@64: this.date = date;
tikhomirov@64: // FIXME implement
tikhomirov@64: // isSet(field) - false => don't use in detection of 'same date'
tikhomirov@64: throw HgRepository.notImplemented();
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@64: *
tikhomirov@64: * @param num - number of changeset to produce. Pass 0 to clear the limit.
tikhomirov@64: * @return this
instance for convenience
tikhomirov@64: */
tikhomirov@131: public HgLogCommand limit(int num) {
tikhomirov@64: limit = num;
tikhomirov@64: return this;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@64: * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive.
tikhomirov@64: * Revision may be specified with {@link HgRepository#TIP}
tikhomirov@148: * @param rev1 - local revision number
tikhomirov@148: * @param rev2 - local revision number
tikhomirov@64: * @return this
instance for convenience
tikhomirov@64: */
tikhomirov@131: public HgLogCommand range(int rev1, int rev2) {
tikhomirov@64: if (rev1 != TIP && rev2 != TIP) {
tikhomirov@64: startRev = rev2 < rev1 ? rev2 : rev1;
tikhomirov@64: endRev = startRev == rev2 ? rev1 : rev2;
tikhomirov@64: } else if (rev1 == TIP && rev2 != TIP) {
tikhomirov@64: startRev = rev2;
tikhomirov@64: endRev = rev1;
tikhomirov@64: } else {
tikhomirov@64: startRev = rev1;
tikhomirov@64: endRev = rev2;
tikhomirov@64: }
tikhomirov@64: return this;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@77: /**
tikhomirov@77: * Visit history of a given file only.
tikhomirov@77: * @param file path relative to repository root. Pass null
to reset.
tikhomirov@80: * @param followCopyRename true to report changesets of the original file(-s), if copy/rename ever occured to the file.
tikhomirov@77: */
tikhomirov@131: public HgLogCommand file(Path file, boolean followCopyRename) {
tikhomirov@77: // multiple? Bad idea, would need to include extra method into Handler to tell start of next file
tikhomirov@77: this.file = file;
tikhomirov@80: followHistory = followCopyRename;
tikhomirov@77: return this;
tikhomirov@64: }
tikhomirov@142:
tikhomirov@142: /**
tikhomirov@142: * Handy analog of {@link #file(Path, boolean)} when clients' paths come from filesystem and need conversion to repository's
tikhomirov@142: */
tikhomirov@142: public HgLogCommand file(String file, boolean followCopyRename) {
tikhomirov@142: return file(Path.create(repo.getToRepoPathHelper().rewrite(file)), followCopyRename);
tikhomirov@142: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@154: * Similar to {@link #execute(org.tmatesoft.hg.repo.RawChangeset.Inspector)}, collects and return result as a list.
tikhomirov@64: */
tikhomirov@157: public List execute() throws HgException {
tikhomirov@64: CollectHandler collector = new CollectHandler();
tikhomirov@64: execute(collector);
tikhomirov@64: return collector.getChanges();
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: /**
tikhomirov@64: *
tikhomirov@64: * @param inspector
tikhomirov@64: * @throws IllegalArgumentException when inspector argument is null
tikhomirov@64: * @throws ConcurrentModificationException if this log command instance is already running
tikhomirov@64: */
tikhomirov@157: public void execute(Handler handler) throws HgException {
tikhomirov@64: if (handler == null) {
tikhomirov@64: throw new IllegalArgumentException();
tikhomirov@64: }
tikhomirov@193: if (csetTransform != null) {
tikhomirov@64: throw new ConcurrentModificationException();
tikhomirov@64: }
tikhomirov@64: try {
tikhomirov@64: count = 0;
tikhomirov@195: HgChangelog.ParentWalker pw = parentHelper; // leave it uninitialized unless we iterate whole repo
tikhomirov@195: if (file == null) {
tikhomirov@195: pw = getParentHelper();
tikhomirov@195: }
tikhomirov@193: // ChangesetTransfrom creates a blank PathPool, and #file(String, boolean) above
tikhomirov@193: // may utilize it as well. CommandContext? How about StatusCollector there as well?
tikhomirov@195: csetTransform = new ChangesetTransformer(repo, handler, pw);
tikhomirov@77: if (file == null) {
tikhomirov@77: repo.getChangelog().range(startRev, endRev, this);
tikhomirov@77: } else {
tikhomirov@80: HgDataFile fileNode = repo.getFileNode(file);
tikhomirov@80: fileNode.history(startRev, endRev, this);
tikhomirov@126: if (fileNode.isCopy()) {
tikhomirov@80: // even if we do not follow history, report file rename
tikhomirov@80: do {
tikhomirov@126: if (handler instanceof FileHistoryHandler) {
tikhomirov@126: FileRevision src = new FileRevision(repo, fileNode.getCopySourceRevision(), fileNode.getCopySourceName());
tikhomirov@126: FileRevision dst = new FileRevision(repo, fileNode.getRevision(0), fileNode.getPath());
tikhomirov@126: ((FileHistoryHandler) handler).copy(src, dst);
tikhomirov@126: }
tikhomirov@80: if (limit > 0 && count >= limit) {
tikhomirov@80: // if limit reach, follow is useless.
tikhomirov@80: break;
tikhomirov@80: }
tikhomirov@80: if (followHistory) {
tikhomirov@126: fileNode = repo.getFileNode(fileNode.getCopySourceName());
tikhomirov@80: fileNode.history(this);
tikhomirov@80: }
tikhomirov@80: } while (followHistory && fileNode.isCopy());
tikhomirov@80: }
tikhomirov@77: }
tikhomirov@64: } finally {
tikhomirov@193: csetTransform = null;
tikhomirov@64: }
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: //
tikhomirov@64:
tikhomirov@154: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
tikhomirov@64: if (limit > 0 && count >= limit) {
tikhomirov@64: return;
tikhomirov@64: }
tikhomirov@64: if (branches != null && !branches.contains(cset.branch())) {
tikhomirov@64: return;
tikhomirov@64: }
tikhomirov@64: if (users != null) {
tikhomirov@64: String csetUser = cset.user().toLowerCase();
tikhomirov@64: boolean found = false;
tikhomirov@64: for (String u : users) {
tikhomirov@64: if (csetUser.indexOf(u) != -1) {
tikhomirov@64: found = true;
tikhomirov@64: break;
tikhomirov@64: }
tikhomirov@64: }
tikhomirov@64: if (!found) {
tikhomirov@64: return;
tikhomirov@64: }
tikhomirov@64: }
tikhomirov@64: if (date != null) {
tikhomirov@64: // FIXME
tikhomirov@64: }
tikhomirov@64: count++;
tikhomirov@193: csetTransform.next(revisionNumber, nodeid, cset);
tikhomirov@64: }
tikhomirov@195:
tikhomirov@195: private HgChangelog.ParentWalker getParentHelper() {
tikhomirov@195: if (parentHelper == null) {
tikhomirov@195: parentHelper = repo.getChangelog().new ParentWalker();
tikhomirov@195: parentHelper.init();
tikhomirov@195: }
tikhomirov@195: return parentHelper;
tikhomirov@195: }
tikhomirov@195:
tikhomirov@64:
tikhomirov@64: public interface Handler {
tikhomirov@64: /**
tikhomirov@129: * @param changeset not necessarily a distinct instance each time, {@link HgChangeset#clone() clone()} if need a copy.
tikhomirov@64: */
tikhomirov@129: void next(HgChangeset changeset);
tikhomirov@64: }
tikhomirov@64:
tikhomirov@80: /**
tikhomirov@131: * When {@link HgLogCommand} is executed against file, handler passed to {@link HgLogCommand#execute(Handler)} may optionally
tikhomirov@80: * implement this interface to get information about file renames. Method {@link #copy(FileRevision, FileRevision)} would
tikhomirov@129: * get invoked prior any changeset of the original file (if file history being followed) is reported via {@link #next(HgChangeset)}.
tikhomirov@80: *
tikhomirov@131: * For {@link HgLogCommand#file(Path, boolean)} with renamed file path and follow argument set to false,
tikhomirov@80: * {@link #copy(FileRevision, FileRevision)} would be invoked for the first copy/rename in the history of the file, but not
tikhomirov@80: * followed by any changesets.
tikhomirov@80: *
tikhomirov@80: * @author Artem Tikhomirov
tikhomirov@80: * @author TMate Software Ltd.
tikhomirov@80: */
tikhomirov@80: public interface FileHistoryHandler extends Handler {
tikhomirov@80: // XXX perhaps, should distinguish copy from rename? And what about merged revisions and following them?
tikhomirov@80: void copy(FileRevision from, FileRevision to);
tikhomirov@80: }
tikhomirov@80:
tikhomirov@64: public static class CollectHandler implements Handler {
tikhomirov@129: private final List result = new LinkedList();
tikhomirov@64:
tikhomirov@129: public List getChanges() {
tikhomirov@64: return Collections.unmodifiableList(result);
tikhomirov@64: }
tikhomirov@64:
tikhomirov@129: public void next(HgChangeset changeset) {
tikhomirov@64: result.add(changeset.clone());
tikhomirov@64: }
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: public static final class FileRevision {
tikhomirov@64: private final HgRepository repo;
tikhomirov@64: private final Nodeid revision;
tikhomirov@64: private final Path path;
tikhomirov@64:
tikhomirov@80: /*package-local*/FileRevision(HgRepository hgRepo, Nodeid rev, Path p) {
tikhomirov@64: if (hgRepo == null || rev == null || p == null) {
tikhomirov@148: // since it's package local, it is our code to blame for non validated arguments
tikhomirov@148: throw new HgBadStateException();
tikhomirov@64: }
tikhomirov@64: repo = hgRepo;
tikhomirov@64: revision = rev;
tikhomirov@64: path = p;
tikhomirov@64: }
tikhomirov@64:
tikhomirov@64: public Path getPath() {
tikhomirov@64: return path;
tikhomirov@64: }
tikhomirov@64: public Nodeid getRevision() {
tikhomirov@64: return revision;
tikhomirov@64: }
tikhomirov@157: public void putContentTo(ByteChannel sink) throws HgDataStreamException, IOException, CancelledException {
tikhomirov@157: HgDataFile fn = repo.getFileNode(path);
tikhomirov@157: int localRevision = fn.getLocalRevision(revision);
tikhomirov@157: fn.contentWithFilters(localRevision, sink);
tikhomirov@64: }
tikhomirov@64: }
tikhomirov@64: }