|
| 1 | +var git = require('../'), |
| 2 | + TreeEntry = function() {}, |
| 3 | + path = require('path'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Refer to vendor/libgit2/include/git2/types.h for filemode definitions. |
| 7 | + * |
| 8 | + * @readonly |
| 9 | + * @enum {Integer} |
| 10 | + */ |
| 11 | +TreeEntry.FileMode = { |
| 12 | + /** 0000000 */ New: 0, |
| 13 | + /** 0040000 */ Tree: 16384, |
| 14 | + /** 0100644 */ Blob: 33188, |
| 15 | + /** 0100755 */ Executable: 33261, |
| 16 | + /** 0120000 */ Link: 40960, |
| 17 | + /** 0160000 */ Commit: 57344 |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Is this TreeEntry a blob? (i.e., a file) |
| 22 | + * @return {Boolean} |
| 23 | + */ |
| 24 | +TreeEntry.prototype.isFile = function() { |
| 25 | + return this.filemode === TreeEntry.FileMode.Blob || |
| 26 | + this.filemode === TreeEntry.FileMode.Executable; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Is this TreeEntry a tree? (i.e., a directory) |
| 31 | + * @return {Boolean} |
| 32 | + */ |
| 33 | +TreeEntry.prototype.isTree = function() { |
| 34 | + return this.filemode === TreeEntry.FileMode.Tree; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Is this TreeEntry a directory? Alias for `isTree` |
| 39 | + * @return {Boolean} |
| 40 | + */ |
| 41 | +TreeEntry.prototype.isDirectory = TreeEntry.prototype.isTree; |
| 42 | + |
| 43 | +/** |
| 44 | + * Is this TreeEntry a blob? Alias for `isFile` |
| 45 | + * @return {Boolean} |
| 46 | + */ |
| 47 | +TreeEntry.prototype.isBlob = TreeEntry.prototype.isFile; |
| 48 | + |
| 49 | +/** |
| 50 | + * Retrieve the SHA for this TreeEntry. |
| 51 | + * @return {String} |
| 52 | + */ |
| 53 | +TreeEntry.prototype.sha = function() { |
| 54 | + return this.oid().sha(); |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Retrieve the tree for this entry. Make sure to call `isTree` first! |
| 59 | + * @return {Tree} |
| 60 | + */ |
| 61 | +TreeEntry.prototype.getTree = function(callback) { |
| 62 | + var self = this; |
| 63 | + this.parent.repo.getTree(this.oid(), function(error, tree) { |
| 64 | + if (error) return callback(error); |
| 65 | + |
| 66 | + tree.entry = self; |
| 67 | + callback(null, tree); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Retrieve the tree for this entry. Make sure to call `isTree` first! |
| 73 | + * @return {Blob} |
| 74 | + */ |
| 75 | +TreeEntry.prototype.getBlob = function(callback) { |
| 76 | + this.parent.repo.getBlob(this.oid(), callback); |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Returns the path for this entry. |
| 81 | + * @return {String} |
| 82 | + */ |
| 83 | +TreeEntry.prototype.path = function(callback) { |
| 84 | + return path.join(this.parent.path(), this.name()); |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Alias for `path` |
| 89 | + */ |
| 90 | +TreeEntry.prototype.toString = function() { |
| 91 | + return this.path(); |
| 92 | +}; |
| 93 | + |
| 94 | +module.exports = TreeEntry; |
0 commit comments