view src/org/tmatesoft/hg/core/HgDiffCommand.java @ 631:8a5cdcb27b8f

AIOOBE in HgManifest.RevisionMapper. Provide more details about exception context. Create lock file atomically. Test concurrent pull-rebase and read
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 30 May 2013 15:24:17 +0200
parents 72c979555cb8
children 54e16ab771ec
line wrap: on
line source
/*
 * Copyright (c) 2013 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 static org.tmatesoft.hg.repo.HgRepository.NO_REVISION;
import static org.tmatesoft.hg.repo.HgRepository.TIP;

import org.tmatesoft.hg.internal.BlameHelper;
import org.tmatesoft.hg.internal.CsetParamKeeper;
import org.tmatesoft.hg.internal.Experimental;
import org.tmatesoft.hg.internal.FileHistory;
import org.tmatesoft.hg.internal.FileRevisionHistoryChunk;
import org.tmatesoft.hg.repo.HgDataFile;
import org.tmatesoft.hg.repo.HgRepository;
import org.tmatesoft.hg.repo.HgRuntimeException;
import org.tmatesoft.hg.util.CancelledException;
import org.tmatesoft.hg.util.Path;

/**
 * 'hg diff' counterpart, with similar, diff-based, functionality
 * 
 * @since 1.1
 * @author Artem Tikhomirov
 * @author TMate Software Ltd.
 */
@Experimental(reason="#execute* methods might get renamed")
public class HgDiffCommand extends HgAbstractCommand<HgDiffCommand> {

	private final HgRepository repo;
	private HgDataFile df;
	private final CsetParamKeeper clogRevIndexStart, clogRevIndexEnd;
	private HgIterateDirection iterateDirection = HgIterateDirection.NewToOld;

	public HgDiffCommand(HgRepository hgRepo) {
		repo = hgRepo;
		clogRevIndexStart = new CsetParamKeeper(hgRepo);
		clogRevIndexEnd = new CsetParamKeeper(hgRepo);
	}
	
	public HgDiffCommand file(Path file) {
		df = repo.getFileNode(file);
		return this;
	}

	/**
	 * Selects the file which history to blame, mandatory.
	 * 
	 * @param file repository file
	 * @return <code>this</code> for convenience
	 */
	public HgDiffCommand file(HgDataFile file) {
		df = file;
		return this;
	}

	/**
	 * Select range of file's history for {@link #executeDiff(HgBlameInspector)}
	 * and {@link #executeAnnotate(HgBlameInspector)}.
	 * <p>
	 * {@link #executeDiff(HgBlameInspector) diff} uses these as revisions to diff against each other, while 
	 * {@link #executeAnnotate(HgBlameInspector) annotate} walks the range. 
	 * 
	 * @param changelogRevIndexStart index of changelog revision, left range boundary
	 * @param changelogRevIndexEnd index of changelog revision, right range boundary
	 * @return <code>this</code> for convenience
	 * @throws HgBadArgumentException if failed to find any of supplied changeset 
	 */
	public HgDiffCommand range(int changelogRevIndexStart, int changelogRevIndexEnd) throws HgBadArgumentException {
		clogRevIndexStart.set(changelogRevIndexStart);
		clogRevIndexEnd.set(changelogRevIndexEnd);
		return this;
	}
	
	/**
	 * Selects revision for {@link #executeAnnotateSingleRevision(HgBlameInspector)}, the one 
	 * to diff against its parents. 
	 * 
	 * Besides, it is handy when range of interest spans up to the very beginning of the file history 
	 * (and thus is equivalent to <code>range(0, changelogRevIndex)</code>)
	 * 
	 * @param changelogRevIndex index of changelog revision
	 * @return <code>this</code> for convenience
	 * @throws HgBadArgumentException if failed to find supplied changeset 
	 */
	public HgDiffCommand changeset(int changelogRevIndex) throws HgBadArgumentException {
		clogRevIndexStart.set(0);
		clogRevIndexEnd.set(changelogRevIndex);
		return this;
	}

	/**
	 * Revision differences are reported in selected order when 
	 * annotating {@link #range(int, int) range} of changesets with
	 * {@link #executeAnnotate(HgBlameInspector)}.
	 * <p>
	 * This method doesn't affect {@link #executeAnnotateSingleRevision(HgBlameInspector)} and
	 * {@link #executeDiff(HgBlameInspector)}
	 * 
	 * @param order desired iteration order 
	 * @return <code>this</code> for convenience
	 */
	public HgDiffCommand order(HgIterateDirection order) {
		iterateDirection = order;
		return this;
	}

	// FIXME progress and cancellation
	
	/**
	 * Diff two revisions selected with {@link #range(int, int)} against each other.
	 * <p>mimics 'hg diff -r clogRevIndex1 -r clogRevIndex2'
	 * 
 	 * @throws HgCallbackTargetException propagated exception from the handler
	 * @throws CancelledException if execution of the command was cancelled
	 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
	 */
	public void executeDiff(HgBlameInspector insp) throws HgCallbackTargetException, CancelledException, HgException {
		checkFile();
		try {
			int fileRevIndex1 = fileRevIndex(df, clogRevIndexStart.get());
			int fileRevIndex2 = fileRevIndex(df, clogRevIndexEnd.get());
			BlameHelper bh = new BlameHelper(insp);
			bh.prepare(df, clogRevIndexStart.get(), clogRevIndexEnd.get());
			bh.diff(fileRevIndex1, clogRevIndexStart.get(), fileRevIndex2, clogRevIndexEnd.get());
		} catch (HgRuntimeException ex) {
			throw new HgLibraryFailureException(ex);
		}
	}

	/**
	 * Walk file history {@link #range(int, int) range} and report changes (diff) for each revision
	 * 
 	 * @throws HgCallbackTargetException propagated exception from the handler
	 * @throws CancelledException if execution of the command was cancelled
	 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
	 */
	public void executeAnnotate(HgBlameInspector insp) throws HgCallbackTargetException, CancelledException, HgException {
		checkFile();
		try {
			if (!df.exists()) {
				return;
			}
			BlameHelper bh = new BlameHelper(insp);
			FileHistory fileHistory = bh.prepare(df, clogRevIndexStart.get(), clogRevIndexEnd.get());
	
			int[] fileClogParentRevs = new int[2];
			int[] fileParentRevs = new int[2];
			for (FileRevisionHistoryChunk fhc : fileHistory.iterate(iterateDirection)) {
				for (int fri : fhc.fileRevisions(iterateDirection)) {
					int clogRevIndex = fhc.changeset(fri);
					// the way we built fileHistory ensures we won't walk past [changelogRevIndexStart..changelogRevIndexEnd]
					assert clogRevIndex >= clogRevIndexStart.get();
					assert clogRevIndex <= clogRevIndexEnd.get();
					fhc.fillFileParents(fri, fileParentRevs);
					fhc.fillCsetParents(fri, fileClogParentRevs);
					bh.annotateChange(fri, clogRevIndex, fileParentRevs, fileClogParentRevs);
				}
			}
		} catch (HgRuntimeException ex) {
			throw new HgLibraryFailureException(ex);
		}
	}

	/**
	 * Annotates changes of the file against its parent(s). 
	 * Unlike {@link #annotate(HgDataFile, int, Inspector, HgIterateDirection)}, doesn't
	 * walk file history, looks at the specified revision only. Handles both parents (if merge revision).
	 * 
 	 * @throws HgCallbackTargetException propagated exception from the handler
	 * @throws CancelledException if execution of the command was cancelled
	 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
	 */
	public void executeAnnotateSingleRevision(HgBlameInspector insp) throws HgCallbackTargetException, CancelledException, HgException {
		checkFile();
		try {
			int changelogRevisionIndex = clogRevIndexEnd.get();
			// TODO detect if file is text/binary (e.g. looking for chars < ' ' and not \t\r\n\f
			int fileRevIndex = fileRevIndex(df, changelogRevisionIndex);
			int[] fileRevParents = new int[2];
			df.parents(fileRevIndex, fileRevParents, null, null);
			if (changelogRevisionIndex == TIP) {
				changelogRevisionIndex = df.getChangesetRevisionIndex(fileRevIndex);
			}
			int[] fileClogParentRevs = new int[2];
			fileClogParentRevs[0] = fileRevParents[0] == NO_REVISION ? NO_REVISION : df.getChangesetRevisionIndex(fileRevParents[0]);
			fileClogParentRevs[1] = fileRevParents[1] == NO_REVISION ? NO_REVISION : df.getChangesetRevisionIndex(fileRevParents[1]);
			BlameHelper bh = new BlameHelper(insp);
			int clogIndexStart = fileClogParentRevs[0] == NO_REVISION ? (fileClogParentRevs[1] == NO_REVISION ? 0 : fileClogParentRevs[1]) : fileClogParentRevs[0];
			bh.prepare(df, clogIndexStart, changelogRevisionIndex);
			bh.annotateChange(fileRevIndex, changelogRevisionIndex, fileRevParents, fileClogParentRevs);
		} catch (HgRuntimeException ex) {
			throw new HgLibraryFailureException(ex);
		}
	}

	private void checkFile() {
		if (df == null) {
			throw new IllegalArgumentException("File is not set");
		}
	}

	private static int fileRevIndex(HgDataFile df, int csetRevIndex) throws HgRuntimeException {
		Nodeid fileRev = df.getRepo().getManifest().getFileRevision(csetRevIndex, df.getPath());
		return df.getRevisionIndex(fileRev);
	}
}