Skip to content

Instantly share code, notes, and snippets.

@JosXa
Created June 27, 2025 21:01
Show Gist options
  • Save JosXa/65cfc9295869ee9de725d34b755f6854 to your computer and use it in GitHub Desktop.
Save JosXa/65cfc9295869ee9de725d34b755f6854 to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
export const metadata: Metadata = {
name: "Temporarily gitignore a file",
}
let p = await path({
hint: "Choose a file or repository path",
startPath: "D:\\projects",
})
const isDirectory = await isDir(p)
/**
* Retrieves a list of temporarily ignored files using git.
* @returns {Promise<string[]>} An array of ignored file paths, or an empty array if an error occurs.
*/
const getIgnoredFiles = async (): Promise<string[]> => {
try {
const { stdout } = await exec("git ls-files -v . | grep ^S", { cwd: p })
return stdout
.split("\n")
.filter((line) => line.startsWith("S "))
.map((line) => line.slice(2).trim())
} catch (error) {
console.error("Error fetching ignored files:", error)
return []
}
}
/**
* Retrieves the top-level path of the git repository for a given path.
* @returns {Promise<string>} The top-level directory path of the repository.
*/
const getTopLevelPath = async (filePath: string): Promise<string> => {
try {
// Determine the directory of the provided path
const directoryPath = isDirectory ? filePath : path.dirname(filePath)
const { stdout } = await exec(`git -C "${directoryPath}" rev-parse --show-toplevel`)
return stdout.trim()
} catch (error) {
console.error("Error getting top-level git directory:", error)
throw error
}
}
if (isDirectory) {
let ignoredFiles = await getIgnoredFiles()
while (ignoredFiles.length > 0) {
const fileToUnignore = await arg("Select a file to unignore", ignoredFiles)
await exec(`git update-index --no-skip-worktree ${fileToUnignore}`, { cwd: p })
// Refresh the ignored files list
ignoredFiles = await getIgnoredFiles()
}
// Show this div when no more files are ignored
await div(md("No files are currently ignored."))
await exit(0)
} else {
while (true) {
const topLevelPath = await getTopLevelPath(p)
await exec(`git update-index --skip-worktree ${p}`, { cwd: topLevelPath })
await div(`File ${p} is now temporarily ignored`)
p = await path({
hint: "Ignore another file?",
startPath: path.dirname(p),
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment