I need to apply some code changes to a source code file I don't have access to. I write code but rely on someone else to test it for me because my old computer has trouble with VMs. Most of the changes are fixing small syntax errors, but I think there were some code instertions, some newlines, etc. I've never used diff in this way before. And I've never used patch ever. Is this possible?

Dani AI

Generated

For : it is practical to produce a diff/patch that someone else can apply, but it only works reliably when the patch is built against the same base version the tester has. Small edits, whitespace fixes, or nearby line shifts are often handled automatically; large, unrelated edits will produce rejects that need manual merging.

Building on points from and , a simple, robust workflow:

# locally, produce a unified patch (unified diffs are easiest to apply)
diff -u original.c edited.c > fix.patch

# send fix.patch and include a base identifier (package version, commit id or sha1sum)
sha1sum original.c

# tester: preview and then apply (adjust -pN if patch contains path prefixes)
patch --dry-run -p0 < fix.patch
patch -p0 < fix.patch

If hunks fail, patch will leave .rej files and usually a file.orig. Inspect .rej to see what changed and apply those bits by hand, or use an interactive merge tool. If the tester can provide the exact base file (or if both sides use Git), a three-way merge is much easier: commit the change locally and either push a branch or create a formatted patch with history (git format-patch) so the tester can merge with Git’s tooling.

Practical cautions: include the base file identifier (commit hash or checksum) with the patch, request a dry run first, and note line-ending/whitespace differences (CRLF vs LF can cause spurious rejects). If applying a patch repeatedly fails, sending the complete modified file (or a branch/commit) is the least error-prone option.

Recommended Answers

All 4 Replies

diff is used to find the differences between two files. So you will need a copy of the file before the changes were made and a copy of the file after the changes were made.

in other words no. Thats OK. I noticed diff worked by identifying line numbers where files differed I hoped that if perhaps the files had the same number of lines, changes could still be made. But I also noticed there are different versions of a diff file.

There has to be two versions of the file so that diff can tell you the differences. How would you expect diff to work with only 1 file??? The two files don't have to have the same number of lines, at least not with the diff pogram I've used in MS-Windows.

Depending upon how you use it, diff can generate files that the patch command can use to update a source file by merging the deltas between the two versions. You need to read the man pages for diff and patch in detail in order to understand how that works, and whether it is applicable for your needs. This technique is commonly used when distribution source code patches for Linux system software, so there is abundant information on the web that can help. Remember, Google (or DuckDuckGo) is your friend! :-)

In any case, if you get the original version of a source file and edit it, you can use diff to generate the deltas that the patch command can be used to create a new (and hopefully fixed) source file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.