JGit: Semantics of diff hunks versus git commandline [message #1795792] |
Fri, 28 September 2018 16:46 |
Dexter Haslem Messages: 2 Registered: September 2018 |
Junior Member |
|
|
I've been exploring a particular commit that ends up with different modified hunks than the git executable `git diff -p`. The patch it creates ends up correct, but the lines it moves vary. This is interesting to me because the vast majority of other commits I diff with JGit (set to myers) match git executable diffs around 90% of the time, so I was wondering if there are some knobs to tweak on the semantics of which lines it adds/removes in this scenario.
Here is a comparison of the diffs from DiffFormatter and git commandline -
JGit DiffFormatter diff:
diff --git a/foo.sh b/foo.sh
index d7d320e..3639f73 100644
--- a/foo.sh
+++ b/foo.sh
@@ -14,8 +14,15 @@
echo `pip install -r src/foo_lib/requirements.pip`
echo `pip install -r src/foo2/requirements.pip`
echo
+echo "Updating Django models/data..."
+echo `python src/foo1/manage.py syncdb`
+echo
+echo "Updating NPM packages..."
+cd $WORKDIR/www/jsapp
+echo `make`
+echo `make install`
+echo
echo "Running Grunt to build JS modules..."
-cd $WORKDIR/www/jsapp
echo " -- executing: grunt build (from $WORKDIR/www/jsapp/)"
echo `grunt build`
echo
For comparison here is what `git diff` of it looks like:
diff --git a/build_foo.sh b/build_foo.sh
index caca979..5ca34f7 100644
--- a/build_foo.sh
+++ b/build_foo.sh
@@ -14,8 +14,15 @@ echo `pip install -r src/metadb/requirements.pip`
echo `pip install -r src/foo_lib/requirements.pip`
echo `pip install -r src/foo2/requirements.pip`
echo
-echo "Running Grunt to build JS modules..."
+echo "Updating Django models/data..."
+echo `python src/foo1/manage.py syncdb`
+echo
+echo "Updating NPM packages..."
cd $WORKDIR/www/jsapp
+echo `make`
+echo `make install`
+echo
+echo "Running Grunt to build JS modules..."
echo " -- executing: grunt build (from $WORKDIR/www/jsapp/)"
echo `grunt build`
echo
Notice it leaves the "cd $WORKDIR..." line.
Below is a standalone unit test that creates a repo and commits and prints the diff above.
@Test
public void jgitDiffFormatterTest() throws IOException, GitAPIException
{
File localPath = File.createTempFile("test-case-repo", "");
if (!localPath.delete())
{
throw new IOException("Could not delete temporary file " + localPath);
}
try
{
try (Git git = Git.init().setDirectory(localPath).call())
{
ClassLoader testClassReloader = this.getClass().getClassLoader();
// add file initial commit
final String testFileName = "foo.sh";
File dstFile = new File(localPath, testFileName);
File srcFile = new File(testClassReloader.getResource("resources/jgit-test-cases/testfile1.sh").getFile());
assertTrue(srcFile.exists());
FileUtils.copyFile(srcFile, dstFile);
git.add().addFilepattern(testFileName).call();
git.commit().setMessage("add test file").call();
// change file to create diff
File srcFileUpdated = new File(testClassReloader.getResource("resources/jgit-test-cases/testfile2.sh").getFile());
assertTrue(srcFileUpdated.exists());
FileUtils.copyFile(srcFileUpdated, dstFile);
git.add().addFilepattern(testFileName).call();
git.commit().setMessage("updated test file").call();
Repository repo = git.getRepository();
// now do the equiv of `git diff -p HEAD~1 HEAD` to diff the only two commits
// and print the diff patch
ObjectId oldHead = repo.resolve("HEAD^^{tree}");
ObjectId head = repo.resolve("HEAD^{tree}");
try (ObjectReader reader = repo.newObjectReader())
{
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
// finally get the list of changed files
List<DiffEntry> diffs = git.diff()
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();
// pass through formatter, we dont bother with full blown rename detector here on one
// simple file example
try (OutputStream tmpOutput = new ByteArrayOutputStream(2048))
{
DiffFormatter formatter = new DiffFormatter(tmpOutput);
formatter.setRepository(repo);
formatter.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
formatter.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(SupportedAlgorithm.MYERS));
formatter.setDetectRenames(true);
formatter.getRenameDetector().setRenameScore(50);
formatter.format(diffs);
String diffPatchString = tmpOutput.toString();
System.out.println("** printing commit DIFF:");
System.out.println(diffPatchString);
}
}
}
}
finally
{
FileUtils.deleteDirectory(localPath);
}
}
Appreciate any feedback.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03349 seconds