Improve commit diff.

This commit is contained in:
takezoe
2013-04-28 14:54:04 +09:00
parent a170daa48c
commit bf2692c9f8
2 changed files with 32 additions and 13 deletions

View File

@@ -146,13 +146,13 @@ class RepositoryViewerServlet extends ServletBase {
if(raw){ if(raw){
// Download // Download
contentType = "application/octet-stream" contentType = "application/octet-stream"
getContent(git, objectId) getContent(git, objectId, false)
} else { } else {
// Viewer // Viewer
val large = isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize) val large = isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
val viewer = if(isImage(path)) "image" else if(large) "large" else "text" val viewer = if(isImage(path)) "image" else if(large) "large" else "text"
val content = ContentInfo(viewer, if(viewer == "text") getContent(git, objectId).map(new String(_, "UTF-8")) else None) val content = ContentInfo(viewer, if(viewer == "text") getContent(git, objectId, false).map(new String(_, "UTF-8")) else None)
html.blob(branch, repositoryInfo, path.split("/").toList, content, new CommitInfo(rev)) html.blob(branch, repositoryInfo, path.split("/").toList, content, new CommitInfo(rev))
} }
@@ -194,8 +194,8 @@ class RepositoryViewerServlet extends ServletBase {
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff => git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
getContent(git, diff.getOldId.toObjectId).map(new String(_, "UTF-8")), getContent(git, diff.getOldId.toObjectId, false).map(new String(_, "UTF-8")),
getContent(git, diff.getNewId.toObjectId).map(new String(_, "UTF-8"))) getContent(git, diff.getNewId.toObjectId, false).map(new String(_, "UTF-8")))
} }
} else { } else {
// initial commit // initial commit
@@ -203,7 +203,8 @@ class RepositoryViewerServlet extends ServletBase {
walk.addTree(rev.getTree) walk.addTree(rev.getTree)
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]() val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
while(walk.next){ while(walk.next){
buffer.append(DiffInfo(ChangeType.ADD, null, walk.getPathString, None, getContent(git, walk.getObjectId(0)).map(new String(_, "UTF-8")))) buffer.append(DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
getContent(git, walk.getObjectId(0), false).map(new String(_, "UTF-8"))))
} }
buffer.toList buffer.toList
} }
@@ -213,6 +214,12 @@ class RepositoryViewerServlet extends ServletBase {
repositoryInfo, diffs) repositoryInfo, diffs)
} }
///////////////////////////////////////////////////////////////////////////////////////////////
//
// TODO Helper methods should be separated to object?
//
//////////////////////////////////////////////////////////////////////////////////////////////
/** /**
* Get the branch name from the commit id. * Get the branch name from the commit id.
*/ */
@@ -228,10 +235,16 @@ class RepositoryViewerServlet extends ServletBase {
* *
* @param git the Git object * @param git the Git object
* @param id the object id * @param id the object id
* @param large if true then returns None for the large file
* @return the object or None if object does not exist * @return the object or None if object does not exist
*/ */
def getContent(git: Git, id: ObjectId): Option[Array[Byte]] = try { def getContent(git: Git, id: ObjectId, large: Boolean): Option[Array[Byte]] = try {
val loader = git.getRepository.getObjectDatabase.open(id)
if(large == false && isLarge(loader.getSize)){
None
} else {
Some(git.getRepository.getObjectDatabase.open(id).getBytes) Some(git.getRepository.getObjectDatabase.open(id).getBytes)
}
} catch { } catch {
case e: MissingObjectException => None case e: MissingObjectException => None
} }

View File

@@ -39,11 +39,16 @@
</tr> </tr>
<tr> <tr>
<td> <td>
@if(diff.newContent != None || diff.oldContent != None){
<div id="diff-@i"></div> <div id="diff-@i"></div>
<textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea> <textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea>
<textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea> <textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea>
} else {
Too big file not shown
}
</td> </td>
</tr> </tr>
</table> </table>
} }
} }
@@ -95,15 +100,16 @@ function diffUsingJS(oldTextId, newTextId, outputId) {
baseTextLines: oldLines, baseTextLines: oldLines,
newTextLines: newLines, newTextLines: newLines,
opcodes: opcodes, opcodes: opcodes,
// set the display titles for each resource
//baseTextName: "Base Text",
//newTextName: "New Text",
contextSize: 4, contextSize: 4,
viewType: 1 viewType: 1
})); }));
} }
@diffs.zipWithIndex.map { case (diff, i) => $(function(){
@diffs.zipWithIndex.map { case (diff, i) =>
@if(diff.newContent != None || diff.oldContent != None){
diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i'); diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i');
} }
}
});
</script> </script>