Auto-generated transcript Good morning guys, welcome back to the DriveSyncer project. In the previous video, we fixed a couple of bugs. I'm not gonna go through them right now. But in the previous video, we fixed a couple of small bugs throughout the project. Stuff like making sure that we check the folder name in the project exists function over here. Stuff like adding this special case in the progress bar rendering, where if total files are equal to zero, then we actually, then we don't crash the program basically. in this video we're going to start building the git ignore parser that i've been hinting at in the previous video and so this is the logic that we're going to follow so let me zoom in as well just to make sure you guys can see this the first step is obviously to read the git ignore file yeah obviously and this is a two-step process the first step is to ignore all of the invalid lines lines that are just empty lines to start with a hash symbol this means a comment in the git ignore file so stuff like where's my git ignore file let me show you guys an example if i have an empty line over here and then i have another um file over here right i want to be able to ignore these empty lines also if i have a comment like this is a sensitive file this is just a comment right and so there's no need to parse this either so i want to ignore all of these invalid patterns and just make sure we're only parsing the valid patterns that we have and as as we find a valid pattern we're going to append it to the ignore list over here this is going to be a list of strings and we're going to process all of these patterns and figure out which files to ignore which files to allow and once we're done reading the git ignore file we're going to start the folder upload so in our code this means that we're going to in here in the upload folder function we're going to start the walk der function and we're going to walk through the entire project folder upload all the files right that's what this that's what this box is supposed to mean and then we're going to run a function called should ignore on each path right so every single time we get a path in this walk there function over here we're going to check whether we should ignore it or we or if we should allow it right once that's done we're going to match each possible pattern and in order to find out whether we should ignore a file or allow it to be uploaded we need to match every single possible pattern against that file so a quick note over here these rules can be mixed together so we can't just use a single switch statement to match them right multiple rules can be combined in the same line i can have stuff over here that combines multiple patterns stuff like like i showed you in the previous example right in the previous video sorry we can have a double star over here to match all folders and subfolders we can then have a folder name inside over here like the pi cache folder in python projects and we can have another pattern over here like a wild card that says ignore all of the files inside the PI cache folder so this is combining like two or three different patterns of a gated nor file and it's combining them all in the same line right so we want to be able to parse all of these patterns in the same line so we can't use something like we can't use something like a switch statement over here let me let me show you an example right here I'm going to remove this in just a bit but we can't say something like switch pattern and then case one case two we can't do stuff like that right because multiple patterns can be on the same line and then here we have all the patterns we can have a pattern like slash foo which means we need to match this path from the root directory we can have just foo which means this can be at any depth meaning this folder or this file foo can be in any folder or subfolder in our project and we'll still ignore it we can have double star slash foo and that's that's the same thing as this empty foo over here it's just going to ignore foo the folder or the file in every single um folder or subfolder and finally we also have foo slash double star and this means that foo is a directory not a file and we're going to ignore everything inside that directory so simple enough and by the way there a couple more patterns Over here which I didn really draw out but there something like you can you can have something like a double star in between the path. You can also have it at the end, like we have over here and at the end of beginning, like we have over here. So yeah, we're going to be implementing all of these patterns in the should ignore function. And yeah, that is the entire process or like an overview of the process. So now that you understand all of that let's move on to the code and let's start actually implementing this. So I'm going to create a new file over here call it ignore.go there we go and this will be package API obviously and we're going to start by creating a new struct over here I'm going to call it git ignore. Now this is going to have a list of ignore patterns this is a new struct that we need to implement that we need to write and before I do that this is also going to have a folder that'll be i guess models dot folder a pointer to that now this ignore pattern struct is going to be it's going to have a pattern which is going to be a string it'll have a couple booleans first of all negate that'll be a boolean um negate basically means whether we should negate this pattern or not basically uh you can have a special exclamation mark in a git ignore file and then you can have a pattern after that so let's say um i have a file called i don't know test.txt if i have an exclamation mark at the beginning over here then this is going to allow this file right it's not going to ignore it it's going to allow it usually when we think about git ignore files we just think about adding patterns that will ignore files right well this exclamation mark symbol allows us to allow specifically allow a file to be uploaded and not ignore it that's what we call the negate symbol and that's why i'm adding a boolean over here to check for that we're also going to have a dir only boolean so basically this is going to be a directory right not a file and we have from root which is another boolean and this just tells us whether we should start scanning from the root or scan or scan at any depth basically and finally a special little field over here called double star mode. This will be a type of double star mode. And I'll create an enum over here to actually hold all of this. Let's say const. You can say leading double stars. There's really only three options over here, right? You can have double stars at the beginning of the pattern or in the middle of the pattern, or you can have them at the, I guess, the end of the pattern, right? So let's say trailing double star. and middle double stars and they should be starts up here as well there we go awesome so we've defined most of the data types that we're going to use let's create a new git ignore function over here this will take a folder that will be models.folder it'll take a global ignore which will be a list of strings this will be the files that we ignore globally in every single project this comes from the config.json file where we have this ignore key and we just ignore this list of folders or files in every single project that we sync right that's the global ignore list and then finally a boolean over here called read git ignore so this will be a boolean because remember we can configure whether we want to read the git ignore file or not in the config over here we can we can set use git ignore to false i don't know why anyone in their right mind would choose to do that but yeah there is an option to do that so we need to handle that okay and this is going to return a pointer to get ignored there we go the very first thing we need to do is we need to convert this global ignore list of strings into a list of ignore patterns over here so we can actually add them to the get ignore struct so let's say to ignore equals make a list of ignore patterns with a length of zero but a capacity of the same length as the length in the get ignore list then we can loop over the entire list of global ignore And we can say to ignore and create a new ignore pattern struct over here pass in the pattern which will just be patterned there we go and that all we need to do over here then we can say if read git ignore is true then we should do something about that right let's write this in a bit first of all down here we can just return the git ignore a new git ignore struct over here you can pass in to ignore over here and the folder over here now before i actually write the block of code inside this if conditional over here we want to create a new function first and that function is going to read and parse our gitignore file so let's go create that down here i'll call it func read gitignore it'll take a folder that'll be models dot folder and it'll return a list of ignore patterns called patterns and an error called er now we can already see an error over here it says read get ignored redeclared in this block and this is because we already have a function called read get ignore down here we're not going to be using it anymore and so what we should do is we should just comment the entire thing out but before i actually do that i'll just copy this entire thing because we are going to use it as like a starting point now let's comment this go back to the ignore file over here paste this in save so it actually imports all of the stuff over here all of the packages right now we can remove this line down here and in the scanner we can say line equals strings dot trim space s dot text there we go so if for some reason in the git ignore file you have a file a pattern called client.json and you just add a random space next to it we're going to ignore that right obviously now i said that the first step was to ignore the invalid patterns right stuff like over here where we skip the invalid lines right empty lines lines that are comments so let's do that first let's say if the line is an empty string or if strings dot has prefix the line has a prefix of a hash symbol then we know that this is an invalid line and we can skip it so we'll just say continue on to the next line cool now we can create an empty ignore pattern over here and just pass in the pattern which will be line now we can say if strings dot has prefix this is where we actually start to scan for all of the complex patterns that i that i mentioned in the beginning of the video so let's do that let's say strings dot has prefix p dot pattern and a exclamation mark if this is the case we'll say p.negate equals true because this has been negated and we'll say p.pattern equals line and skip the first character. So basically we apply the negation over here by setting negate to true but we also remove the exclamation mark from the actual pattern because we don't need it anymore right. Next if strings dot has suffix remember guys this is a suffix not a prefix pass in p.pattern and the suffix the ending character should be a slash. If that is the case then we know that this is a directory and not a file right so we'll say p.zer only equals true and p.pattern will be strings.trim suffix passing the pattern over here and trim that slash at the end now if we have a prefix that is a slash then we know that this is supposed to be matched from the root directory right so we can say p.from root equals true in this case and we can say p.pattern equals strings dot trim prefix Peter pattern and slash and we just trim that slash at the beginning right again This is all the stuff that we figured out over here in the flow chart And I'm just implementing all of the rules and all the different patterns over here in the code right now Let's say if strings dot has prefix P dot pattern and then a double star with the slash then we'll say P dot double star mode equals leading double stars and let's also remove those stars from the pattern now that we actually set the double star mode we say strings dot trim prefix from p dot pattern and that be the double star slash there we go then if strings dot has suffix p dot pattern and then a slash and double stars then this is good this is going to have the double star mode of trailing double stars there we go and the pattern will be strings dot trim suffix this time not prefix suffix p dot pattern slash double star finally we have strings dot contains p dot pattern and then double stars anywhere in the pattern right if this is the case then that means we're using middle double stars right and now we can clean up the pattern over here as well by saying strings dot replace p dot pattern replace the double stars with an empty string and the end can just be one so that's all we need to do to read the git ignore file and down here in the ignore we can just return patterns right and this patterns is just the list that we create over here right and of course we need to actually append to that patterns list by saying patterns dot append and passing p over here wonderful okay so that's all i'm gonna do for this video let's just do a quick recap we we commented out the read git ignore function over here right in the sync file that's the old function we're not going to use it anymore okay and over here this is obviously going to cause an error in the upload function because this expects the read git ignore file to be to return a list of strings right and that is why we see an error over here we're going to fix that in the next video because we're still actually building this new git ignore parser right but yeah we created this ignore.go file we created the git ignore struct the new git ignore function the ignore patterns the double star mode and then this read git ignore function which is basically just an improvement over the old read git ignore function that we had and this one just parses all of the patterns all the complex patterns that we mentioned in the beginning of the video and it just converts them into a format that you can understand right it converts them into this ignore pattern struck over here with all of these fields and the booleans filled up so we can actually understand what this pattern is and does in code right that's what this read get ignore function is going to do now and it's going to return those patterns and then we're going to use them to actually you know actually figure out whether we should upload a file or not now lastly before we end this video i totally forgot to do this by the way in the new git ignore function in the if read git ignore is equal to true block let's say git ignore list and error equals read git ignore, pass in the folder as well. Let's say if error does not equal to nil, we'll say log dot print f cannot read git ignore will upload all files. Okay. And of course, let's print the error as well by saying percentage V and add a new line and then pass in the error. Now assuming this worked, we can say to ignore dot append, git ignore list as such. And that's pretty much all we need to do. Now each time we call this new git ignore function, we're going to read the gitignore files we're going to parse all the patterns in there we're also going to append the global ignore list to this ignore main list right and we're going to return that gitignore struct and we can then use that to figure out which files to upload which files to not upload we're going to continue building this in the next video stay tuned for that this video is done thank you for watching thank you for following along with the project and stay tuned for the next one Like, comment, subscribe, share, do all of those wonderful things. And lastly, quick shout out to myself. I'm a freelance developer and I build websites, apps, software and stuff like that. So if this is something you're interested in, then check out my website. Link will be in the description. You can also book a call with me for free and we can hop on a call and we can discuss your project again for free. And if you want to work with me on some project or something, this is the best way to get started. That's all for today, guys. Thank you so much for watching and I will see you in the next video. next video