formats.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. var bodec = require('bodec');
  3. var treeMap = require('../lib/object-codec').treeMap;
  4. module.exports = function (repo) {
  5. var loadAs = repo.loadAs;
  6. repo.loadAs = newLoadAs;
  7. var saveAs = repo.saveAs;
  8. repo.saveAs = newSaveAs;
  9. function newLoadAs(type, hash, callback) {
  10. if (!callback) return newLoadAs.bind(repo, type, hash);
  11. var realType = type === "text" ? "blob":
  12. type === "array" ? "tree" : type;
  13. return loadAs.call(repo, realType, hash, onLoad);
  14. function onLoad(err, body, hash) {
  15. if (body === undefined) return callback(err);
  16. if (type === "text") body = bodec.toUnicode(body);
  17. if (type === "array") body = toArray(body);
  18. return callback(err, body, hash);
  19. }
  20. }
  21. function newSaveAs(type, body, callback) {
  22. if (!callback) return newSaveAs.bind(repo, type, body);
  23. type = type === "text" ? "blob":
  24. type === "array" ? "tree" : type;
  25. if (type === "blob") {
  26. if (typeof body === "string") {
  27. body = bodec.fromUnicode(body);
  28. }
  29. }
  30. else if (type === "tree") {
  31. body = normalizeTree(body);
  32. }
  33. else if (type === "commit") {
  34. body = normalizeCommit(body);
  35. }
  36. else if (type === "tag") {
  37. body = normalizeTag(body);
  38. }
  39. return saveAs.call(repo, type, body, callback);
  40. }
  41. };
  42. function toArray(tree) {
  43. return Object.keys(tree).map(treeMap, tree);
  44. }
  45. function normalizeTree(body) {
  46. var type = body && typeof body;
  47. if (type !== "object") {
  48. throw new TypeError("Tree body must be array or object");
  49. }
  50. var tree = {}, i, l, entry;
  51. // If array form is passed in, convert to object form.
  52. if (Array.isArray(body)) {
  53. for (i = 0, l = body.length; i < l; i++) {
  54. entry = body[i];
  55. tree[entry.name] = {
  56. mode: entry.mode,
  57. hash: entry.hash
  58. };
  59. }
  60. }
  61. else {
  62. var names = Object.keys(body);
  63. for (i = 0, l = names.length; i < l; i++) {
  64. var name = names[i];
  65. entry = body[name];
  66. tree[name] = {
  67. mode: entry.mode,
  68. hash: entry.hash
  69. };
  70. }
  71. }
  72. return tree;
  73. }
  74. function normalizeCommit(body) {
  75. if (!body || typeof body !== "object") {
  76. throw new TypeError("Commit body must be an object");
  77. }
  78. if (!(body.tree && body.author && body.message)) {
  79. throw new TypeError("Tree, author, and message are required for commits");
  80. }
  81. var parents = body.parents || (body.parent ? [ body.parent ] : []);
  82. if (!Array.isArray(parents)) {
  83. throw new TypeError("Parents must be an array");
  84. }
  85. var author = normalizePerson(body.author);
  86. var committer = body.committer ? normalizePerson(body.committer) : author;
  87. return {
  88. tree: body.tree,
  89. parents: parents,
  90. author: author,
  91. committer: committer,
  92. message: body.message
  93. };
  94. }
  95. function normalizeTag(body) {
  96. if (!body || typeof body !== "object") {
  97. throw new TypeError("Tag body must be an object");
  98. }
  99. if (!(body.object && body.type && body.tag && body.tagger && body.message)) {
  100. throw new TypeError("Object, type, tag, tagger, and message required");
  101. }
  102. return {
  103. object: body.object,
  104. type: body.type,
  105. tag: body.tag,
  106. tagger: normalizePerson(body.tagger),
  107. message: body.message
  108. };
  109. }
  110. function normalizePerson(person) {
  111. if (!person || typeof person !== "object") {
  112. throw new TypeError("Person must be an object");
  113. }
  114. if (typeof person.name !== "string" || typeof person.email !== "string") {
  115. throw new TypeError("Name and email are required for person fields");
  116. }
  117. return {
  118. name: person.name,
  119. email: person.email,
  120. date: person.date || new Date()
  121. };
  122. }