tikhomirov@127: /*
tikhomirov@628: * Copyright (c) 2011-2013 TMate Software Ltd
tikhomirov@127: *
tikhomirov@127: * This program is free software; you can redistribute it and/or modify
tikhomirov@127: * it under the terms of the GNU General Public License as published by
tikhomirov@127: * the Free Software Foundation; version 2 of the License.
tikhomirov@127: *
tikhomirov@127: * This program is distributed in the hope that it will be useful,
tikhomirov@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@127: * GNU General Public License for more details.
tikhomirov@127: *
tikhomirov@127: * For information on how to redistribute this software under
tikhomirov@127: * the terms of a license other than GNU General Public License
tikhomirov@128: * contact TMate Software at support@hg4j.com
tikhomirov@127: */
tikhomirov@127: package org.tmatesoft.hg.core;
tikhomirov@127:
tikhomirov@247: import java.io.File;
tikhomirov@128: import java.util.Date;
tikhomirov@128:
tikhomirov@128: import org.tmatesoft.hg.internal.ChangelogHelper;
tikhomirov@154: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
tikhomirov@628: import org.tmatesoft.hg.repo.HgRuntimeException;
tikhomirov@133: import org.tmatesoft.hg.util.Path;
tikhomirov@128:
tikhomirov@131: /**
tikhomirov@131: * Repository file status and extra handy information.
tikhomirov@131: *
tikhomirov@131: * @author Artem Tikhomirov
tikhomirov@131: * @author TMate Software Ltd.
tikhomirov@131: */
tikhomirov@127: public class HgStatus {
tikhomirov@127:
tikhomirov@127: public enum Kind {
tikhomirov@143: Modified, Added, Removed, Missing, Unknown, Clean, Ignored
tikhomirov@143: // I'd refrain from changing order of these constants, just in case someone (erroneously, of course ;), uses .ordinal()
tikhomirov@127: };
tikhomirov@127:
tikhomirov@127: private final HgStatus.Kind kind;
tikhomirov@127: private final Path path;
tikhomirov@127: private final Path origin;
tikhomirov@128: private final ChangelogHelper logHelper;
tikhomirov@127:
tikhomirov@128: HgStatus(HgStatus.Kind kind, Path path, ChangelogHelper changelogHelper) {
tikhomirov@128: this(kind, path, null, changelogHelper);
tikhomirov@127: }
tikhomirov@127:
tikhomirov@128: HgStatus(HgStatus.Kind kind, Path path, Path copyOrigin, ChangelogHelper changelogHelper) {
tikhomirov@127: this.kind = kind;
tikhomirov@127: this.path = path;
tikhomirov@127: origin = copyOrigin;
tikhomirov@128: logHelper = changelogHelper;
tikhomirov@127: }
tikhomirov@127:
tikhomirov@127: public HgStatus.Kind getKind() {
tikhomirov@127: return kind;
tikhomirov@127: }
tikhomirov@127:
tikhomirov@127: public Path getPath() {
tikhomirov@127: return path;
tikhomirov@127: }
tikhomirov@127:
tikhomirov@127: public Path getOriginalPath() {
tikhomirov@127: return origin;
tikhomirov@127: }
tikhomirov@127:
tikhomirov@127: public boolean isCopy() {
tikhomirov@127: return origin != null;
tikhomirov@127: }
tikhomirov@128:
tikhomirov@128: /**
tikhomirov@128: * @return null
if author for the change can't be deduced (e.g. for clean files it's senseless)
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception
tikhomirov@128: */
tikhomirov@628: public String getModificationAuthor() throws HgRuntimeException {
tikhomirov@154: RawChangeset cset = logHelper.findLatestChangeWith(path);
tikhomirov@128: if (cset == null) {
tikhomirov@128: if (kind == Kind.Modified || kind == Kind.Added || kind == Kind.Removed /*&& RightBoundary is TIP*/) {
tikhomirov@140: // perhaps, also for Kind.Missing?
tikhomirov@128: return logHelper.getNextCommitUsername();
tikhomirov@128: }
tikhomirov@128: } else {
tikhomirov@128: return cset.user();
tikhomirov@128: }
tikhomirov@128: return null;
tikhomirov@128: }
tikhomirov@128:
tikhomirov@628: /**
tikhomirov@628: * @return date when the file was last modified, never null
. Either date of changeset the file was modified at
tikhomirov@628: * or timestamp of local file, if present
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception
tikhomirov@628: */
tikhomirov@628: public Date getModificationDate() throws HgRuntimeException {
tikhomirov@154: RawChangeset cset = logHelper.findLatestChangeWith(path);
tikhomirov@128: if (cset == null) {
tikhomirov@247: File localFile = new File(logHelper.getRepo().getWorkingDir(), path.toString());
tikhomirov@247: if (localFile.canRead()) {
tikhomirov@247: return new Date(localFile.lastModified());
tikhomirov@247: }
tikhomirov@628: // TODO post-1.1 find out what to do in this case, perhaps, throw an exception?
tikhomirov@628: // perhaps check dirstate and for timestamp
tikhomirov@418: return new Date(); // what's correct?
tikhomirov@128: } else {
tikhomirov@128: return cset.date();
tikhomirov@128: }
tikhomirov@128: }
tikhomirov@127: }