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

Format catenated unified diffs #399

Closed
make-github-pseudonymous-again opened this issue Mar 6, 2021 · 5 comments
Closed

Format catenated unified diffs #399

make-github-pseudonymous-again opened this issue Mar 6, 2021 · 5 comments

Comments

@make-github-pseudonymous-again
Copy link

What is the proper way to catenate unified diffs? When I use the following python loop

for path in paths:
	file_a = os.path.join(dir_a, path)
	with open(file_a) as fd:
		sa = fd.read().splitlines(keepends=True)

	file_b = os.path.join(dir_b, path)
	with open(file_b) as fd:
		sb = fd.read().splitlines(keepends=True)

	lines = unified_diff(
		sa,
		sb,
		fromfile=file_a,
		tofile=file_b
	)

	sys.stdout.writelines(lines)

then pipe it to diff-so-fancy, the first --- file_a +++ file_b header is correctly formatted, but the others are formatted as diff lines -- file_a ++ file_b (removing the first -/+ symbol) and the line annotations always refer to the first file.

@make-github-pseudonymous-again make-github-pseudonymous-again changed the title Diff catenated unified diffs Format catenated unified diffs Mar 6, 2021
@OJFord
Copy link
Member

OJFord commented Mar 6, 2021

You're missing the header lines like:

diff --git a/foo b/bar
[optional line e.g. 'deleted file mode ..']
index 0f24aeb..fe5efa 100644

or similar.

@scottchiefbaker
Copy link
Contributor

Any updates on this? Can we close this issue?

@make-github-pseudonymous-again
Copy link
Author

You're missing the header lines like:

diff --git a/foo b/bar
[optional line e.g. 'deleted file mode ..']
index 0f24aeb..fe5efa 100644

or similar.

I sort of understand how to construct the first line, but what about the other two lines? Is there some utility function that can produce that header?

@OJFord
Copy link
Member

OJFord commented May 28, 2021

Well the middle one is optional; the third is just the git SHAs that the diff is between, and the file mode, which d-s-f will strip (and not meaningfully use) so you could put whatever, probably just best to keep it real-ish so you don't rely too much on exactly how the parsing happens to work today (i.e. this is not minimal, the content of the third line doesn't seem to matter today):

$ echo -e 'diff --git a/foo b/foo\nindex dead..beef 100644\n--- a/foo\n+++b/foo' | diff-so-fancy
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
modified: foo
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
$

@make-github-pseudonymous-again
Copy link
Author

Thanks for the help! Here is how I fixed my script following your advice:

for path in paths:
    file_a = os.path.join(dir_a, path)
    with open(file_a) as fd:
        sa = fd.read().splitlines(keepends=True)

    file_b = os.path.join(dir_b, path)
    with open(file_b) as fd:
        sb = fd.read().splitlines(keepends=True)

    lines = unified_diff(
        sa,
        sb,
        fromfile=file_a,
        tofile=file_b
    )

    try:
        first_line = next(lines)
        sys.stdout.writelines([
            'diff --git a/{} b/{}\n'.format(file_a, file_b),
            'index dead..beef 100644\n',
            first_line
        ])
        sys.stdout.writelines(lines)
    except StopIteration:
        pass

@scottchiefbaker Feel free to close this issue.

make-github-pseudonymous-again added a commit to make-github-pseudonymous-again/sak that referenced this issue May 28, 2021
@OJFord OJFord closed this as completed May 28, 2021
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

3 participants