view src/org/tmatesoft/hg/internal/GeneratePatchInspector.java @ 709:497e697636fc

Report merged lines as changed block if possible, not as a sequence of added/deleted blocks. To facilitate access to merge parent lines AddBlock got mergeLineAt() method that reports index of the line in the second parent (if any), while insertedAt() has been changed to report index in the first parent always
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 21 Aug 2013 16:23:27 +0200
parents 7839ff0bfd78
children
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.internal;

import org.tmatesoft.hg.internal.diff.DiffHelper;
import org.tmatesoft.hg.internal.diff.DiffHelper.DeltaInspector;
import org.tmatesoft.hg.internal.diff.DiffHelper.LineSequence;

class GeneratePatchInspector extends DeltaInspector<LineSequence> {
	private final Patch deltaCollector;
	
	GeneratePatchInspector(Patch p) {
		assert p != null;
		deltaCollector = p;
	}
	
	public static Patch delta(byte[] prev, byte[] content) {
		Patch rv = new Patch();
		DiffHelper<LineSequence> pg = new DiffHelper<LineSequence>();
		pg.init(new LineSequence(prev).splitByNewlines(), new LineSequence(content).splitByNewlines());
		pg.findMatchingBlocks(new GeneratePatchInspector(rv));
		return rv;
	}

	@Override
	protected void changed(int s1From, int s1To, int s2From, int s2To) {
		int from = seq1.chunk(s1From).getOffset();
		int to = seq1.chunk(s1To).getOffset();
		byte[] data = seq2.data(s2From, s2To);
		deltaCollector.add(from, to, data);
	}
	
	@Override
	protected void deleted(int s2DeletionPoint, int s1From, int s1To) {
		int from = seq1.chunk(s1From).getOffset();
		int to = seq1.chunk(s1To).getOffset();
		deltaCollector.add(from, to, new byte[0]);
	}
	
	@Override
	protected void added(int s1InsertPoint, int s2From, int s2To) {
		int insPoint = seq1.chunk(s1InsertPoint).getOffset();
		byte[] data = seq2.data(s2From, s2To);
		deltaCollector.add(insPoint, insPoint, data);
	}
}