Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for File Creation #47

Closed
Pumuckline83 opened this issue Aug 16, 2019 · 8 comments
Closed

Support for File Creation #47

Pumuckline83 opened this issue Aug 16, 2019 · 8 comments
Assignees

Comments

@Pumuckline83
Copy link

Hi there, I ran into a problem when I tried to create a unified diff for the creation of a new file, which seams not to be supported by your library right now.

As far as I can see, this feature is very easy to implement and could help others as well.

It requires just a small change in the UnifiedDiffUtils.processDeltas() method, some changes in the method signatures to get the info about the new File creation into the methods and maybe another method to keep calls to old method signature running with default values.

Actually it just those few lines in the main logic that could cover file creation:

if (createNewFile) {
            origStart = 0; // the 0 here will trigger the creation of the new file
 } else {
            origStart = curDelta.getSource().getPosition() + 1 - contextSize;
            // NOTE: +1 to overcome the 0-offset Position
            if (origStart < 1) {
                origStart = 1;
            }
 }

Cheers, Eva

@wumpz
Copy link
Collaborator

wumpz commented Nov 19, 2019

Could you give me some example diff file, which I could include as a testcase for this behaviour?

@Pumuckline83
Copy link
Author

My scenario: I need a patch file that covers file creation.

The provided method com.github.difflib.Diffutils#diff(List original, List revised) can not handle null input for original. Still you can use this method to get a valid set of patches. You just have to use an empty list as input parameter, because the changes per line are exactly the same for both cases (Meaning in both cases is something like "There was nothing before, but now there is."). So this works fine.

But when you call com.github.difflib.UnifiedDiffUtils#processDeltas, you can not build the unified diff, because you can not tell the method if it should include the file creation, or not, because it always tries to "overcome the 0-offset Position", even if the 0-offset is exactly, what you need.

Example:

1. Current result
Patch execution fails in some tools, if file is not existing (We tested back in august with git command line, Eclipse and IDEA Tools. I don't remember which one actually failed. Sorry. :)).

--- 
+++ test.txt
@@ -1,0 +1,2 @@
+Line1
+Line2

2. Expected (additional) result
Like this, the missing file is created when the patch is applied. Worked on git command line, Eclipse and IDEA.

--- 
+++ test.txt
@@ -0,0 +1,2 @@
+Line1
+Line2

@wumpz wumpz self-assigned this Dec 19, 2019
@wumpz
Copy link
Collaborator

wumpz commented Dec 19, 2019

I will look into it for both versions of the unified diff processor. Do you use the Multifile variant?

@wumpz wumpz pinned this issue Dec 19, 2019
@wumpz
Copy link
Collaborator

wumpz commented Dec 25, 2019

generateUnifiedDiff was changed in that way, that originalFileName == null switches to newfile mode.

@wumpz wumpz closed this as completed in 7dec3fe Dec 25, 2019
@wumpz wumpz unpinned this issue Dec 25, 2019
@Pumuckline83
Copy link
Author

Nice. Thanks a lot! 👍

@wumpz
Copy link
Collaborator

wumpz commented Jan 7, 2020

You are welcome. Did you check the new Unified diff implementation?

@Pumuckline83
Copy link
Author

Yes. It works as expected. :)

@Pumuckline83
Copy link
Author

Argh, I actually found a problem. :)

You should extend your test case to check the first lines of the result as well:

    @Test
    public void testNewFileCreation() throws DiffException {
        List<String> original = new ArrayList<>();
        List<String> revised = new ArrayList<>();

        revised.add("line1");
        revised.add("line2");

        Patch<String> patch = DiffUtils.diff(original, revised);
        List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised",
                original, patch, 10);

        assertEquals("---  ", udiff.get(0));
        assertEquals("+++ revised", udiff.get(1));
        assertEquals("@@ -0,0 +1,2 @@", udiff.get(2));
    }

You will see, that "null" is printed as the files originalName now. :)

@wumpz wumpz reopened this Jan 14, 2020
@wumpz wumpz closed this as completed in ae23408 Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants