From 7b0b4110ed4f63efed25af2f03bf01d2715c5fbf Mon Sep 17 00:00:00 2001 From: Jeff Schumacher Date: Wed, 30 Jun 2010 16:21:49 -0700 Subject: [PATCH] Refactored code out of FileHeader to facilitate rename detection Refactored a superclass out of FileHeader called DiffEntry that holds the more general data from FileHeader that is useful in rename detection (old/new Ids, modes, names, as well as changeType and score). FileHeader is now a DiffEntry that adds Hunks, parsing abilities, etc. Change-Id: I8398728cd218f8c6e98f7a4a7f2f342391d865e4 --- .../src/org/eclipse/jgit/diff/DiffEntry.java | 174 ++++++++++++++++++ .../org/eclipse/jgit/patch/FileHeader.java | 125 +------------ 2 files changed, 176 insertions(+), 123 deletions(-) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java new file mode 100644 index 000000000..ac3ee9864 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2008-2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.diff; + +import org.eclipse.jgit.lib.AbbreviatedObjectId; +import org.eclipse.jgit.lib.FileMode; + +/** A value class representing a change to a file */ +public class DiffEntry { + + /** General type of change a single file-level patch describes. */ + public static enum ChangeType { + /** Add a new file to the project */ + ADD, + + /** Modify an existing file in the project (content and/or mode) */ + MODIFY, + + /** Delete an existing file from the project */ + DELETE, + + /** Rename an existing file to a new location */ + RENAME, + + /** Copy an existing file to a new location, keeping the original */ + COPY; + } + + /** File name of the old (pre-image). */ + protected String oldName; + + /** File name of the new (post-image). */ + protected String newName; + + /** Old mode of the file, if described by the patch, else null. */ + protected FileMode oldMode; + + /** New mode of the file, if described by the patch, else null. */ + protected FileMode newMode; + + /** General type of change indicated by the patch. */ + protected ChangeType changeType; + + /** Similarity score if {@link #changeType} is a copy or rename. */ + protected int score; + + /** ObjectId listed on the index line for the old (pre-image) */ + protected AbbreviatedObjectId oldId; + + /** ObjectId listed on the index line for the new (post-image) */ + protected AbbreviatedObjectId newId; + + /** + * Get the old name associated with this file. + *

+ * The meaning of the old name can differ depending on the semantic meaning + * of this patch: + *

+ * + * @return old name for this file. + */ + public String getOldName() { + return oldName; + } + + /** + * Get the new name associated with this file. + *

+ * The meaning of the new name can differ depending on the semantic meaning + * of this patch: + *

+ * + * @return new name for this file. + */ + public String getNewName() { + return newName; + } + + /** @return the old file mode, if described in the patch */ + public FileMode getOldMode() { + return oldMode; + } + + /** @return the new file mode, if described in the patch */ + public FileMode getNewMode() { + return newMode; + } + + /** @return the type of change this patch makes on {@link #getNewName()} */ + public ChangeType getChangeType() { + return changeType; + } + + /** + * @return similarity score between {@link #getOldName()} and + * {@link #getNewName()} if {@link #getChangeType()} is + * {@link ChangeType#COPY} or {@link ChangeType#RENAME}. + */ + public int getScore() { + return score; + } + + /** + * Get the old object id from the index. + * + * @return the object id; null if there is no index line + */ + public AbbreviatedObjectId getOldId() { + return oldId; + } + + /** + * Get the new object id from the index. + * + * @return the object id; null if there is no index line + */ + public AbbreviatedObjectId getNewId() { + return newId; + } + +} \ No newline at end of file diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/patch/FileHeader.java b/org.eclipse.jgit/src/org/eclipse/jgit/patch/FileHeader.java index 25dc72af2..35c2ee303 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/patch/FileHeader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/patch/FileHeader.java @@ -60,6 +60,7 @@ import java.util.List; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.EditList; import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.Constants; @@ -69,7 +70,7 @@ import org.eclipse.jgit.util.TemporaryBuffer; /** Patch header describing an action for a single file path. */ -public class FileHeader { +public class FileHeader extends DiffEntry { /** Magical file name used for file adds or deletes. */ public static final String DEV_NULL = "/dev/null"; @@ -103,24 +104,6 @@ public class FileHeader { static final byte[] NEW_NAME = encodeASCII("+++ "); - /** General type of change a single file-level patch describes. */ - public static enum ChangeType { - /** Add a new file to the project */ - ADD, - - /** Modify an existing file in the project (content and/or mode) */ - MODIFY, - - /** Delete an existing file from the project */ - DELETE, - - /** Rename an existing file to a new location */ - RENAME, - - /** Copy an existing file to a new location, keeping the original */ - COPY; - } - /** Type of patch used by this file. */ public static enum PatchType { /** A traditional unified diff style patch of a text file. */ @@ -142,30 +125,6 @@ public static enum PatchType { /** Position 1 past the end of this file within {@link #buf}. */ int endOffset; - /** File name of the old (pre-image). */ - private String oldName; - - /** File name of the new (post-image). */ - private String newName; - - /** Old mode of the file, if described by the patch, else null. */ - private FileMode oldMode; - - /** New mode of the file, if described by the patch, else null. */ - protected FileMode newMode; - - /** General type of change indicated by the patch. */ - protected ChangeType changeType; - - /** Similarity score if {@link #changeType} is a copy or rename. */ - private int score; - - /** ObjectId listed on the index line for the old (pre-image) */ - private AbbreviatedObjectId oldId; - - /** ObjectId listed on the index line for the new (post-image) */ - protected AbbreviatedObjectId newId; - /** Type of patch used to modify this file */ PatchType patchType; @@ -312,86 +271,6 @@ private String[] extractFileLines(final Charset[] csGuess) { } } - /** - * Get the old name associated with this file. - *

- * The meaning of the old name can differ depending on the semantic meaning - * of this patch: - *

- * - * @return old name for this file. - */ - public String getOldName() { - return oldName; - } - - /** - * Get the new name associated with this file. - *

- * The meaning of the new name can differ depending on the semantic meaning - * of this patch: - *

- * - * @return new name for this file. - */ - public String getNewName() { - return newName; - } - - /** @return the old file mode, if described in the patch */ - public FileMode getOldMode() { - return oldMode; - } - - /** @return the new file mode, if described in the patch */ - public FileMode getNewMode() { - return newMode; - } - - /** @return the type of change this patch makes on {@link #getNewName()} */ - public ChangeType getChangeType() { - return changeType; - } - - /** - * @return similarity score between {@link #getOldName()} and - * {@link #getNewName()} if {@link #getChangeType()} is - * {@link ChangeType#COPY} or {@link ChangeType#RENAME}. - */ - public int getScore() { - return score; - } - - /** - * Get the old object id from the index. - * - * @return the object id; null if there is no index line - */ - public AbbreviatedObjectId getOldId() { - return oldId; - } - - /** - * Get the new object id from the index. - * - * @return the object id; null if there is no index line - */ - public AbbreviatedObjectId getNewId() { - return newId; - } - /** @return style of patch used to modify this file */ public PatchType getPatchType() { return patchType;