Created
May 11, 2014 13:52
-
-
Save crossai-2033/08fbddcfbc36b0cc972e to your computer and use it in GitHub Desktop.
git store object
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 zlib = require('zlib'); | |
var crypto = require('crypto'); | |
var shasum = crypto.createHash('sha1'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var content = 'what is up, doc?'; | |
var header = 'blob ' + content.length + '\0'; | |
var store = header + content; | |
// 创建所有目录 | |
var mkdirs = module.exports.mkdirs = function(dirpath, mode, callback) { | |
fs.exists(dirpath, function(exists) { | |
if(exists) { | |
callback(dirpath); | |
} else { | |
//尝试创建父目录,然后再创建当前目录 | |
mkdirs(path.dirname(dirpath), mode, function(){ | |
fs.mkdir(dirpath, mode, callback); | |
}); | |
} | |
}); | |
}; | |
// SHA-1 | |
shasum.update(store); | |
var sha1 = shasum.digest('hex'); | |
// 压缩存储 | |
zlib.deflate(store, function (err, buffer) { | |
if (!err) { | |
var p = '.git/objects/' + sha1.substr(0, 2) + '/' + sha1.substr(2, 38); | |
var dir = path.dirname(p); | |
mkdirs(dir, '0777', function() { | |
fs.writeFile(p, buffer, function(err) { | |
if (err) throw err; | |
console.log('saved success'); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment