Back to blogYouTube Video

Published December 24, 2025

Syncing projects with Google Drive - Part 17 (The shouldIgnore() func)

Can't play the video or having issues? Here's the direct link.

AI Summary

This video is part of a series on syncing projects with Google Drive. It focuses specifically on implementing the `shouldIgnore()` function within a custom `.gitignore` parser. The purpose of this function is to determine whether a specific file should be ignored or uploaded based on the rules defined in the project's ignore file.

Key Takeaways

  • The core objective is building a `.gitignore` parser for a Google Drive sync tool.
  • The `shouldIgnore()` function serves as the decision-making logic to filter files during the upload process.

Description

Book a call: https://calendly.com/itshassanaziz/discuss-a-project ==== ==== ==== In this video, we're gonna continue building our new Gitignore parser and write the shouldIgnore() function. This function will tell us whether we should ignore or upload any given file. Let's do it. ==== ==== ==== LINKS Website: https://www.hassandev.me Portfolio: https://www.hassandev.me/work YouTube: https://www.youtube.com/@itshassanaziz?sub_confirmation=1 My Book: https://www.hassandev.me/designing-websites X / Twitter: https://x.com/nothassanaziz

Transcript

Auto-generated transcript
Hey guys, welcome back to the DriveSinker project. I just opened up this project right now and we're setting things up. There is so much we need to do. Let's open up the gitignore file over here. No long intros over here, we're just going to go straight into the code. I want to create a function over here. This will be a pointer receiver. And where's my IntelliSense? I just opened up this project right now. It's still loading. Okay, there we go. A call this should ignore and we basically just want a function that tells us whether to ignore a certain file or not, right? This will take the path which will be a string and the dir entry which will be in the fs package and the dir entry strict struck in here. I'm not going to return anything for now. Let's just implement the function first. So the first thing I want to do is to grab the relative path that we're getting in this function over here right so this is going to be an absolute path i want to convert this into a relative path so we can use the um file path package to do that for the base path we'll pass in the i should probably rename this to just a g over here so let's type in for me and the base path will just be the folder path and over here we'll pass in the path of course handle any errors as well if we can't convert the relative path then let's just say that the relative path is the path that we get in the function arguments now let's actually clean this up a bit as well by using file path dot to slash and then pass in the path over here and what this is basically going to do is it's going to replace the separator characters with a slash character so you know that paths in windows for instance have a let me show you and oh my god i totally forgot to zoom again there we go you know that paths in windows have this backslash symbol right so it's like c slash user slash hassan or something like that right whereas in linux or in other operating systems we use a regular slash character right so this function is really just here to normalize the path right just to clean it up and make sure that we're not using any invalid characters or anything. So with that done, now we can actually start to loop over the entire patterns list. What's it called? G.toIgnore. And we can actually start to test every single pattern and see which ones match on this path, right? So I'm going to create some initial variables. We'll say ignored, which is a Boolean. This will be false by default. and also target which will be a string. Now let's see if p.from root then we'll say target equals the relative path otherwise we say target equals the base folder name of the relative path Now what exactly am I doing over here we discussed in the previous video about get ignore parsing rules that if the path starts from the root then we supposed to match that pattern from the root as well right which is why i pass in the entire relative path in the target over here otherwise we're supposed to match the pattern anywhere in the in the project at any folder depth right which is why i only use the base name in this case right now let's create another variable called match you might think why am i using match when i already have a variable called ignored over here basically match is going to tell us whether the pattern matched or not and ignored is going to tell us whether we're going to ignore that pattern or not right and we need both of them because we also support negations and get ignored right you'll see you'll see a lot more about this when we actually finish this function. Now we also need to handle the double star modes that we created in the last video I guess. Let's say case leading double stars if strings dot has suffix pass in the pattern and then just a slash and an asterisk a wildcard character after that. This means that this this pattern is a directory and we're supposed to ignore everything inside this directory right so let's do that first of all let's just clean up the suffix let's say trim suffix p dot pattern and pass in the exact same suffix over here then let's say match and error equals file path dot match pass in the pattern and target over here let's handle the error as well and then let's just print the error over here let's say invalid pattern print the pattern as well as the error and a new line there we go there we go and out here let's say else if the string doesn't have that suffix right then our job is a lot simpler we can just say match equals strings dot has suffix p dot pattern relative path and then let's say a slash over here plus p dot pattern over here or let's add another conditional the relative path equals d dot pattern over here there we go and also you see an error over here uh let's just remove the colon symbol over here so we are assigning the match value to this match variable up here and not creating a new one okay now let's move on to the next case let's say trailing double stars this will be very similar to this line over here we'll say if the relative path equals p.pattern or strings.has prefix relative path slash plus, actually not plus over here we concatenate the pattern Remember this is trailing double stars not leading right This comes at the beginning of the pattern not the end So the slash in this case will go to the end and we use has prefix instead of has suffix Okay, that's all we need to do. If either of these conditions match, then we just say match equals true. And then we move on to the final case, which is middle double stars. Now this one has a pattern that looks something like this, right? You have some folder name and you have double stars between there. And then you have another folder name and some path after that, right? So we need to first of all split that into two parts. So for that we can use strings dot split n We'll pass in P dot pattern a slash and let's say two over here Then let's say if the length of parts equals two Then we'll say the left and right parts are parts zero and Parts one and we'll say match equals strings dot contains relative path left and let's also separate this into uh new lines as well strings dot contains relative path right so it contains both of them and let's make sure that strings dot index relative path left is greater than or actually less than strings dot last index relative path and right basically what i'm saying in this conditional over here is that the left folder name comes before the right folder name. It's an extra check over here, right? And that's all we need to do over here. Now we can actually add a default case over here. We'll say match and error equals file pad dot match. Again, notice that I'm not using a colon over here, right? I'm not trying to declare a new variable. I'm just reassigning the variables or reassigning the values to the existing variables. So pass in the pattern over here, as well as the target, handle the error as well. Let's say log print app, invalid pattern, print the pattern and the error, and then a new line. There we go. And if there is an error over here, let's also continue on to the next, the next pattern. So we don't waste any time trying to parse and match with invalid patterns. Now out here, if the pattern was matched, let's check the negation of the pattern. If we are supposed to negate the pattern, we'll say ignored equals false. Otherwise, if this is just a regular pattern without any negations, we'll say ignored equals true right if ignored then we need to return the value over here right we need to return some sort of a boolean field that tells us whether we should ignore this or not now if you go back to sync go which is broken right now until we finish the new tracker but if we go down here you see that right in this block where we actually ignore files and folders if the if the path is a directory then we use the filepad error over here right otherwise we just return a nil over here and this filepad.skipdir allows us to skip entire directories without having to walk through every single file in that directory and that is extremely useful right but there's literally no way to represent that in just a boolean in a boolean you only have true or false right that's all that this function can return right if we're supposed to allow the file we can say true otherwise we can say false but what about if the file is a directory and we want to ignore that right so we need a third return value and the easiest way to implement this is to just go up here and create an enum so that's what i'm going gonna do we'll say type ignore return type and I'll call this an integer and then let's declare some constant values for this we'll say ignore ignore dir pass in the type as well and call this iota there we go ignore file and allow so that's the three things we need to have right now down here in the at the end of this function let's say log dot printf it's always a good idea to have logs guys and print the path over here let's say if d dot is there then we return ignore there otherwise we return ignore file if ignored is not true then none of this will run and we're supposed to allow the file so in that case we'll just return allow by default right and i should probably do that out here at the very end of the function and at the top of this function let's let's also add the return type over here so we don't have any errors and with that we are completely done with the should ignore function i'm going to stop the video over there and in the next video we're going to integrate this entire thing with our synchronizing algorithm over here and then we'll hopefully finally be done with this and have a new custom get ignore implementation that really just ignores all of the complex patterns that we put in our git ignore file. Stay tuned for that. Like, comment, subscribe, and do all of those wonderful things for me. Real quick guys, while I still have your attention over here, let me do a quick self-promotion plug. This is my professional website and I work as a freelancer to develop websites and mobile apps and all of those wonderful things for people. If this is something you're interested in or you know someone who's interested in this and you want to work with me to develop some sort of a project or app or whatever right you can book a free meeting with me over here or you can use the top link in the description to book a call with me it's completely free and we can talk about your project and start building it right now so if that's something you're interested in check out the top link in the description other than that thank you for watching the video and i'll see you in the next one

Share this article

All great things started with a conversation

If you've got a cool project or opportunity and you want me to be a part of it, set up a free meeting with me here, and let's talk. 😊