Sam
Sam

Reputation: 3160

Perform a `git clean` in a repository using NodeGit

I have a NodeGit repository, repo. This repository has some local changes, both in tracked and untracked files & directories. How can I clean this repository, with the same effect as the git clean -f -d command, so that afterwards the local changes are no longer there?

Thanks for the help.

Upvotes: 2

Views: 301

Answers (1)

bk2204
bk2204

Reputation: 76469

NodeGit is a wrapper around libgit2, which is a lower-level library. It's not designed to handle a lot of the wholesale complex tasks like most Git commands do, and as a consequence there isn't a built-in technique to clean. This isn't just NodeGit: libgit2 also lacks any built-in clean functionality.

However, it is possible to implement, albeit with some difficulty. libgit2 provides a way to get the status of a file, and you can include untracked files and recurse into ignored directories. The untracked files will be marked as being new in the working tree but not have any index flags set for them. They can then be deleted.

That doesn't solve the problem of cleaning up the empty directories, so you'll have to manually track which directories had untracked files and then go through after the fact and determine whether the directory is empty and if so, remove it. If you also want to remove empty directories, then you have to do that walk yourself, because the status code doesn't enumerate directories, only files.

If this seems like a lot of work, well, it is. git clean does a decent amount of work under the hood, and so doing it by hand is necessarily complex.

Upvotes: 3

Related Questions