Created
September 4, 2014 23:36
-
-
Save jonbrennecke/2a0e33d12cb092770971 to your computer and use it in GitHub Desktop.
Recover file contents from "git reset --hard" (file names are another story)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cp = require('child_process'), | |
fs = require('fs'), | |
execSync = require('exec-sync'); | |
var fsck = cp.spawn("git", ["fsck", "--cache", "--unreachable","$(git for-each-ref --format=\"%(objectname)\")"]) | |
var stdout = ''; | |
fsck.stdout.on('data',function(data){ | |
stdout += data.toString(); | |
}); | |
fsck.on('close',function(){ | |
var commits = stdout.match(/(.*)\n/g).map(function(line){ | |
var match = line.match(/blob\s+(.*)\n/) | |
return match ? match[1] : "" | |
}); | |
console.log(commits.length) | |
commits = commits.slice(600,commits.length); // test | |
commits.forEach(function(commit){ | |
if (commit) { | |
var output = execSync('git show ' + commit); | |
fs.writeFile( __dirname + "/dumps/" + commit, output, function(err){ | |
if (err) | |
throw err | |
}); | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment