Welcome to the second chapter of the memory project!
In the previous chapter, Memory Taxonomy, we learned how to label our notes using categories like "User" or "Project".
However, having a label isn't enough. If you try to save a Project note, but you aren't actually inside a project folder on your computer, where should that note go?
The Goal: We need a way to check if the user is currently working inside a valid project (specifically, a Git repository) before we attempt to save project-specific data.
The Use Case:
Imagine you are building a tool that tracks bugs in your code. You type save bug-report.
Downloads folder, the system should say, "Wait, you aren't in a project right now."Think of your application as a Construction Crew.
Before the crew starts pouring concrete (saving data), they need to check the survey markers to ensure they are on the correct property.
If the crew builds on an empty lot that they don't own, the building (your data) gets lost or misplaced. Repository Awareness is the foreman checking the map before work begins.
We use a specific function to answer the question: "Am I inside a Git repository right now?"
This check is synchronous, meaning the code stops and looks immediately before doing anything else. It is quick and doesn't require running complex external commands.
In Node.js applications, we find out where we are using process.cwd() (Current Working Directory). We pass this location to our checker.
import { projectIsInGitRepo } from './versions'
// 1. Get our current location on the disk
const myLocation = process.cwd();
// 2. Ask: Is this a valid project site?
const isSafeToBuild = projectIsInGitRepo(myLocation);
Now we can use that simple true or false result to control our application flow.
if (isSafeToBuild) {
console.log("Found a Git repo! Saving project memory...");
// Proceed to save the note
} else {
console.log("Not in a repo. Cannot save project memory.");
// Stop or save to 'Local' storage instead
}
Example Output:
C:/Users/You/MyProject (which has git): Output: trueC:/ (root drive): Output: falseHow does the system actually know? It doesn't use magic; it acts like a detective looking for clues.
Every Git repository has a hidden folder named .git.
When we ask projectIsInGitRepo, the system looks at your current folder. If it doesn't see .git, it steps back to the parent folder and looks again. It keeps going up until it hits the top of your drive.
Let's look at versions.ts. This file contains the logic for our awareness check.
// --- File: versions.ts ---
import { findGitRoot } from '../git.js'
// This function takes a path (cwd) and returns true/false
export function projectIsInGitRepo(cwd: string): boolean {
// findGitRoot walks up the folder tree looking for .git
// If it finds it, it returns the path. If not, it returns null.
return findGitRoot(cwd) !== null
}
Key Takeaway:
We rely on a helper findGitRoot. We don't need to know exactly where the root is right now (e.g., /Users/dev/projects/myapp), we just need to know that it is not null.
If it is not null, it means we are standing on valid "construction property."
You might notice the comments in the source code mention dirIsInGitRepo() for async checks.
// Uses findGitRoot which walks the filesystem (no subprocess)
// Prefer `dirIsInGitRepo()` for async checks
For our Repository Awareness logic, we stick to the synchronous projectIsInGitRepo because we usually need to know this immediately before defining where to save a file.
In this chapter, we added Repository Awareness to our toolkit.
projectIsInGitRepo to validate our current location by hunting for that hidden .git folder.Now that we know what type of data we have (Taxonomy) and where we are allowed to save it (Repository Awareness), we might want to turn certain capabilities on or off based on sophisticated configuration rules.
๐ Next Chapter: Feature Gating
Generated by Code IQ