 I did a video a little bit ago on DWM which is one of those suckless programs where in order to modify it you actually have to change the source code with patch and diff and stuff like that. Now I don't specifically want to talk about suckless programs in this video but you should probably watch it if you're a suckless, interested if you're suckless curious. But I really just want to do a general video on patch and diff because a lot of people don't know about these programs despite the fact that they're on every Unix, Linux, they're very useful but a lot of people don't often use them just because I don't know maybe they seem a little weird or something like that. So what are patch and diff and what are they actually used for? So here's my hypothetical example. I'm going to cd into this directory and I have a bunch of stuff from my master's program. You can actually tell this is old stuff. I had it back when I was a normie because there were like space names in the folders and capital letters and stuff like that. Anyway, so as an example I'm going to open up my master's thesis. Now this is a really big document. It's over a thousand lines and this of course is law tech but you could hypothetically have any kind of source code from any program markup language or just any kind of text document. So here's what we want to do. Let's say hypothetically I am revising this, I'm making changes, I'm actively writing this and at the same time I want to have someone come in and you know I guess make edits, correct the mistakes or something like that. So that's what I want to do. Now as an example, let's say I send this off to someone and they're going to use diff and patch. How are they going to benefit from using diff and patch? Okay, so here's an example. Let's say we go here and add in some extra text. So here is a new paragraph, blah, blah, blah, stuff like that. Just add in some kind of text. I'm going to add in some stuff over there. I'm going to go to the bottom, we'll say this is a new old paragraph, stuff like that or maybe all capitalized stuff or something like that. I always almost forget the VIM shortcut for capitalized but I remembered it. So we've made some changes to our file and now what I'm going to do is I'm going to save it not as thesis.tech but I'm going to save it as thesis underscore new.tech. So that's going to be the change that this hypothetical editor has made to my document. So here's the problem normally is if he just sent that back to me, maybe at the same time I'm making a bunch of changes to that document and now I have to manually go through and find out what the changes are and intersperse them and copy and paste a whole bunch of stuff but we could just use diff and patch. So first off, you may know that there's this program named diff and one of the other things that it does by default is you can give it two files and what it does is it prints out the lines that are different between them. So these green lines right here, the lines that are pointing to the right I guess, they are part of our second file but not our first. So these three lines were added in our second file but they're not existent in the first. These lines here are also added in. You'll notice that this line I changed this word on on what the diff generates So it shows you what it looks like in one file and it shows you what it looks like in the other file. So it shows you that distinction. So diff shows you all the differences between files. Now one nice thing about diff is that you can actually make files to use patch with. So specifically now this enough actually is very nice. It's very convenient. Okay now I can see all the changes between these. Hypothetically you could go through and you know copy and paste this it would at least be easier than going through the whole document. But one nice thing about diff is that you can generate a diff file. Give diff the U option and that will generate, well let's actually look what this does first off. It first off gives you the file names. It also gives you the approximate location of these changes in terms of line number. And it also gives you the lines that are around those lines. So hypothetically you can see exactly where these changes are taking place. So the interesting thing is we can output all of this. We can say let's output all this to thesis.diff. So now we have this file thesis.diff. So the nice thing about this is we can use it with patch to automatically change our file. So what we can do now is I can say patch thesis.diff and it's going to say patching file thesis.tech. Now of course it knew to use the file thesis.tech because again in thesis.diff it tells you where exactly that is. But what exactly did that do? So long story short it made the changes to make it like the other revised file. So I can open up thesis.tech and I can see I forget exactly what I said paragraph or something. So you can see that these lines that we added in have been added in. So I think the other one was maybe contemplate. So these changes have now taken place. You can also let's say we want to reverse that change. I can run patch with the capital R option and if you've already applied a patch this will undo the patch. Now let's show you where the goodness is. Now right now we're just changing files. Again the interesting use case is when someone is making changes to the file while I'm concurrently at the same time making different changes. Changes that might even be really close to each other. So let's open up this file again and I think it was around this line. So I've unpatched the file so now it's how it originally was and let's say I go to this place where the diff file wants to add stuff and I add in another paragraph. I'll say something like this is a paragraph that I added on my own machine. So now we have that stuff in there. Now let's try running our patch again. And if we do that you'll see that it still worked. Hunk 1 succeeded, Hunk 2 succeeded, Hunks are just like the modifications. That's what that means. The interesting thing is that Hunk 1 is the one that we change stuff around and it says it succeeded with Fuzz 2, offset of two lines. Now what that means is, let's actually look at what it means. Where was that defect? Yeah, you'll see that so there's this paragraph that we had added on our own machine and the diff file added in this. And the reason, keep in mind we changed the environment that it was looking for but it realizes okay it's close enough to the line number. It's close enough to this additional text. I'm going to put it in. I assume that that's what they want. So that's one of the nice things about patch. It's actually pretty smart. Now let's make a change. Let me actually undo that patch once again. Let's make a change that will make it too difficult for patch to automatically decide where to put something. So let me go back in there to that same line that I keep searching for. Let's say I comment out. Let's say I change these lines down here and that change is going to throw off patch totally because it'll see that this line is similar. It's going to see that we're looking at a similar line number. But this is probably a little too much for patch. So let's just try that out. So I'm going to save this. Now if we run patch, what's going to happen is it says 1 out of 2 hunks failed, saving rejects to thesis tech reg. Now what does that mean? I'll tell you what it doesn't mean. It doesn't mean, I had an error. Let me go online and search for it so I can figure out what happened. Don't do that. Because it actually gives you the answer right here. Now what happened is patch wasn't smart enough or wasn't exactly sure how to change one of these hunks. So it saves the hunk that it didn't change to thesis.tech.redge. So you can open this up and see yourself if you want that, oh, these are the lines that failed. So if you have, let's say again, you're patching a suckless program. And because you've patched so many patches, it's changed so many of the environments that eventually patches get to the point where they have errors. Well, you just open up your rejects file and you see, okay, this is what's going on. I'll open up thesis.tech myself if I want. And I will add in this line that patch didn't quite know how to add. So I'll add it in here, do whatever, change whatever you want. And that'll be pretty much all you have to do. Actually, I'm going to undo this because I might do some more changes to this. But that's what you're supposed to do. Now additionally, in addition to the rejects file, now, of course, if you have a bunch of your hunks failing and a bunch of your hunks succeeding, sometimes it gets a little messy. So one nice thing that patch does is it actually gives you a, in addition to a reject file, gives you an or a file, an orage file. I don't know how you want to pronounce it. But in this one, it gives you what that document looked like before you applied any patches if you ran into an error. So this is what it, if you totally mess things up, if you're applying patches and patches and everything gets too confusing and you can't really run patch R because you've changed too much, you can usually go into the orage file or the orage file and get. So again, the nice thing about patching diff is you can use them on any kind of files. They work, of course, similar to git. I think actually git might use, I'm not sure how git works, but I think it might actually use patch and diff sort of as a back end partially. They definitely can generate diff files and stuff like that. But patch and diff is a nice way. You don't need git or anything fat. You don't need to start a git repository. If you're just making simple changes to a file, or first, actually I use this every once in a while because I'll make, I'll be testing something on my .files on another computer, the .files that people use publicly for larbs. And I might make a whole bunch of changes. Well, I can make a diff file instead of uploading, re-uploading all files or starting a new git repo and or connecting to my git repo. I can just make a diff file and send it to myself or something like that. And that gives me pretty much all I want. Now you, of course, should double check diff and patches command, you know, mans, just to make sure you know what's going on, make sure you know all the options. I've only sort of scratched the surface here, but that should give you an idea of the kind of things you can use them for. And they definitely are useful. So that's about it. And I will see you guys next time.