Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 4, 2020 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/31828cf1056a9e70db8399be7c33a4ee to your computer and use it in GitHub Desktop.
Save creationix/31828cf1056a9e70db8399be7c33a4ee to your computer and use it in GitHub Desktop.
-- Using luvit.io and https://github.com/creationix/lua-git
local makeChroot = require('coro-fs').chroot
local mount = require('git').mount
coroutine.wrap(function ()
local db = mount(makeChroot(".git"))
local head = db.getHead()
local commit = db.loadAs("commit", head)
local tree = db.loadAs("tree", commit.tree)
local hasConfig = false
local hasHooks = false
for _,entry in pairs(tree) do
if entry.name == 'config' then hasConfig = true end
if entry.name == 'hooks' then hasHooks = true end
end
local empty = db.saveAs('tree', {})
if not hasConfig then
print "Adding empty config folder"
tree[#tree+1] = {hash = empty, name = 'config', mode = 16384}
end
if not hasHooks then
print "Adding empty hooks folder"
tree[#tree+1] = {hash = empty, name = 'hooks', mode = 16384}
end
local newTree = db.saveAs('tree', tree)
commit.tree = newTree
local newCommit = db.saveAs('commit', commit)
p("original commit", head)
p("new commit", newCommit)
end)()
@creationix
Copy link
Author

You can see the generated commit and tree by passing the hash to git cat-file -p

$ git cat-file -p 754ae8c8361c8b09141c757f22050602334ee96c
040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904    config
040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904    hooks
040000 tree cc41d02fab43d45d5405f1a1dcec0bd432da0123    profiles
100644 blob 8725d157a96c48e0c50c0f544b634e365bc11a99    remotes.txt
100644 blob 60f89a501ab6988e2c42f5d37c325bd43eadc80a    settings.yml

Then if you like it, move the current branch to the new commit with a hard reset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment