'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator'));
var _objectWithoutProperties = _interopDefault(require('babel-runtime/helpers/objectWithoutProperties'));
var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator'));
var path = _interopDefault(require('path'));
var models_js = require('./models.js');
var managers_js = require('./managers.js');
var _getIterator = _interopDefault(require('babel-runtime/core-js/get-iterator'));
var _Set = _interopDefault(require('babel-runtime/core-js/set'));
var _toConsumableArray = _interopDefault(require('babel-runtime/helpers/toConsumableArray'));
var buffer = require('buffer');
var stream = require('stream');
var pad = _interopDefault(require('pad'));
var pako = _interopDefault(require('pako'));
var createHash = _interopDefault(require('sha.js'));
var utils_js = require('./utils.js');

/**
 * Read and/or write to the git config file(s)
 * @param {Object} args - Arguments object
 * @param {FSModule} args.fs - The filesystem holding the git repo
 * @param {string} args.dir - The path to the [working tree](index.html#dir-vs-gitdir) directory
 * @param {string} [args.gitdir=path.join(dir, '.git')] - The path to the [git directory](index.html#dir-vs-gitdir)
 * @param {string} args.path -  The key of the git config entry.
 * @param {string} [args.value] - A value to store at that path.
 * @returns {Promise<any>} - Resolves with the config value
 *
 * If no `value` is provided, it does a read.
 * If a `value` is provided, it does a write.
 *
 * @example
 * let repo = {fs, dir: '<@.@>'}
 *
 * // Write config value
 * await git.config({
 *   ...repo,
 *   path: '<@user.name@>',
 *   value: '<@Mr. Test@>'
 * })
 *
 * // Read config value
 * let value = await git.config({
 *   ...repo,
 *   path: '<@user.name@>'
 * })
 * console.log(value)
 */
var config = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref) {
    var dir = _ref.dir,
        _ref$gitdir = _ref.gitdir,
        gitdir = _ref$gitdir === undefined ? path.join(dir, '.git') : _ref$gitdir,
        _fs = _ref.fs,
        _ref$all = _ref.all,
        all = _ref$all === undefined ? false : _ref$all,
        _ref$append = _ref.append,
        append = _ref$append === undefined ? false : _ref$append,
        args = _objectWithoutProperties(_ref, ['dir', 'gitdir', 'fs', 'all', 'append']);

    var fs, path$$1, value, config;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            fs = new models_js.FileSystem(_fs);
            path$$1 = args.path, value = args.value;
            _context.next = 4;
            return managers_js.GitConfigManager.get({ fs: fs, gitdir: gitdir });

          case 4:
            config = _context.sent;

            if (!(value === undefined && !args.hasOwnProperty('value'))) {
              _context.next = 13;
              break;
            }

            if (!all) {
              _context.next = 10;
              break;
            }

            return _context.abrupt('return', config.getall(path$$1));

          case 10:
            return _context.abrupt('return', config.get(path$$1));

          case 11:
            _context.next = 22;
            break;

          case 13:
            if (!append) {
              _context.next = 18;
              break;
            }

            _context.next = 16;
            return config.append(path$$1, value);

          case 16:
            _context.next = 20;
            break;

          case 18:
            _context.next = 20;
            return config.set(path$$1, value);

          case 20:
            _context.next = 22;
            return managers_js.GitConfigManager.save({ fs: fs, gitdir: gitdir, config: config });

          case 22:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function config(_x) {
    return _ref2.apply(this, arguments);
  };
}();

var types = {
  commit: 16,
  tree: 32,
  blob: 48,
  tag: 64,
  ofs_delta: 96,
  ref_delta: 112

  /**
   *
   * If there were no errors, then there will be no `errors` property.
   * There can be a mix of `ok` messages and `errors` messages.
   *
   * @typedef {Object} PushResponse
   * @property {Array<string>} [ok] - The first item is "unpack" if the overall operation was successful. The remaining items are the names of refs that were updated successfully.
   * @property {Array<string>} [errors] - If the overall operation threw and error, the first item will be "unpack {Overall error message}". The remaining items are individual refs that failed to be updated in the format "{ref name} {error message}".
   */

  /**
   * Push a branch
   *
   * @param {Object} args - Arguments object
   * @param {FSModule} args.fs - The filesystem holding the git repo
   * @param {string} args.dir - The path to the [working tree](index.html#dir-vs-gitdir) directory
   * @param {string} [args.gitdir=path.join(dir, '.git')] - The path to the [git directory](index.html#dir-vs-gitdir)
   * @param {string} [args.ref=undefined] - Which branch to push. By default this is the currently checked out branch of the repository.
   * @param {string} [args.remote='origin'] - If URL is not specified, determines which remote to use.
   * @param {string} [args.url=undefined] - The URL of the remote git server. The default is the value set in the git config for that remote.
   * @param {string} [args.authUsername=undefined] - The username to use with Basic Auth
   * @param {string} [args.authPassword=undefined] - The password to use with Basic Auth
   * @returns {Promise<PushResponse>} - Resolves successfully when push completes with a detailed description of the operation from the server.
   *
   * @example
   * let repo = {fs, dir: '<@.@>'}
   * let pushResponse = await git.push({
   *   ...repo,
   *   remote: '<@origin@>',
   *   ref: '<@master@>',
   *   authUsername: <@process.env.GITHUB_TOKEN@>,
   *   authPassword: <@process.env.GITHUB_TOKEN@>
   * })
   * console.log(pushResponse)
   */
};var push = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref) {
    var _fs = _ref.fs,
        dir = _ref.dir,
        _ref$gitdir = _ref.gitdir,
        gitdir = _ref$gitdir === undefined ? path.join(dir, '.git') : _ref$gitdir,
        ref = _ref.ref,
        _ref$remote = _ref.remote,
        remote = _ref$remote === undefined ? 'origin' : _ref$remote,
        url = _ref.url,
        authUsername = _ref.authUsername,
        authPassword = _ref.authPassword;
    var fs, fullRef, oid, httpRemote, commits, objects, packstream, oldoid, response;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            fs = new models_js.FileSystem(_fs);
            // TODO: Figure out how pushing tags works. (This only works for branches.)

            if (!(url === undefined)) {
              _context.next = 5;
              break;
            }

            _context.next = 4;
            return config({ fs: fs, gitdir: gitdir, path: 'remote.' + remote + '.url' });

          case 4:
            url = _context.sent;

          case 5:
            fullRef = ref.startsWith('refs/') ? ref : 'refs/heads/' + ref;
            _context.next = 8;
            return managers_js.GitRefManager.resolve({ fs: fs, gitdir: gitdir, ref: ref });

          case 8:
            oid = _context.sent;
            httpRemote = new managers_js.GitRemoteHTTP(url);

            if (authUsername !== undefined && authPassword !== undefined) {
              httpRemote.auth = {
                username: authUsername,
                password: authPassword
              };
            }
            _context.next = 13;
            return httpRemote.preparePush();

          case 13:
            _context.next = 15;
            return listCommits({
              fs: fs,
              gitdir: gitdir,
              start: [oid],
              finish: httpRemote.refs.values()
            });

          case 15:
            commits = _context.sent;
            _context.next = 18;
            return listObjects({ fs: fs, gitdir: gitdir, oids: commits });

          case 18:
            objects = _context.sent;
            packstream = new stream.PassThrough();
            oldoid = httpRemote.refs.get(fullRef) || '0000000000000000000000000000000000000000';

            packstream.write(models_js.GitPktLine.encode(oldoid + ' ' + oid + ' ' + fullRef + '\0 report-status\n'));
            packstream.write(models_js.GitPktLine.flush());
            pack({
              fs: fs,
              gitdir: gitdir,
              oids: [].concat(_toConsumableArray(objects)),
              outputStream: packstream
            });
            _context.next = 26;
            return httpRemote.push(packstream);

          case 26:
            response = _context.sent;
            return _context.abrupt('return', response);

          case 28:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function push(_x) {
    return _ref2.apply(this, arguments);
  };
}();

/** @ignore */
var listCommits = function () {
  var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(_ref3) {

    // Because git commits are named by their hash, there is no
    // way to construct a cycle. Therefore we won't worry about
    // setting a default recursion limit.
    var walk = function () {
      var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(oid) {
        var _ref7, type, object, commit, parents, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3;

        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                visited.add(oid);
                _context2.next = 3;
                return managers_js.GitObjectManager.read({ fs: fs, gitdir: gitdir, oid: oid });

              case 3:
                _ref7 = _context2.sent;
                type = _ref7.type;
                object = _ref7.object;

                if (!(type !== 'commit')) {
                  _context2.next = 8;
                  break;
                }

                throw new Error('Expected type commit but type is ' + type);

              case 8:
                commit = models_js.GitCommit.from(object);
                parents = commit.headers().parent;
                _iteratorNormalCompletion3 = true;
                _didIteratorError3 = false;
                _iteratorError3 = undefined;
                _context2.prev = 13;
                _iterator3 = _getIterator(parents);

              case 15:
                if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) {
                  _context2.next = 23;
                  break;
                }

                oid = _step3.value;

                if (!(!finishingSet.has(oid) && !visited.has(oid))) {
                  _context2.next = 20;
                  break;
                }

                _context2.next = 20;
                return walk(oid);

              case 20:
                _iteratorNormalCompletion3 = true;
                _context2.next = 15;
                break;

              case 23:
                _context2.next = 29;
                break;

              case 25:
                _context2.prev = 25;
                _context2.t0 = _context2['catch'](13);
                _didIteratorError3 = true;
                _iteratorError3 = _context2.t0;

              case 29:
                _context2.prev = 29;
                _context2.prev = 30;

                if (!_iteratorNormalCompletion3 && _iterator3.return) {
                  _iterator3.return();
                }

              case 32:
                _context2.prev = 32;

                if (!_didIteratorError3) {
                  _context2.next = 35;
                  break;
                }

                throw _iteratorError3;

              case 35:
                return _context2.finish(32);

              case 36:
                return _context2.finish(29);

              case 37:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this, [[13, 25, 29, 37], [30,, 32, 36]]);
      }));

      return function walk(_x3) {
        return _ref6.apply(this, arguments);
      };
    }();

    // Let's go walking!


    var dir = _ref3.dir,
        _ref3$gitdir = _ref3.gitdir,
        gitdir = _ref3$gitdir === undefined ? path.join(dir, '.git') : _ref3$gitdir,
        _fs = _ref3.fs,
        start = _ref3.start,
        finish = _ref3.finish;

    var fs, startingSet, finishingSet, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ref, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _ref5, _oid, visited, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, oid;

    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            fs = new models_js.FileSystem(_fs);
            startingSet = new _Set();
            finishingSet = new _Set();
            _iteratorNormalCompletion = true;
            _didIteratorError = false;
            _iteratorError = undefined;
            _context3.prev = 6;
            _iterator = _getIterator(start);

          case 8:
            if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
              _context3.next = 18;
              break;
            }

            ref = _step.value;
            _context3.t0 = startingSet;
            _context3.next = 13;
            return managers_js.GitRefManager.resolve({ fs: fs, gitdir: gitdir, ref: ref });

          case 13:
            _context3.t1 = _context3.sent;

            _context3.t0.add.call(_context3.t0, _context3.t1);

          case 15:
            _iteratorNormalCompletion = true;
            _context3.next = 8;
            break;

          case 18:
            _context3.next = 24;
            break;

          case 20:
            _context3.prev = 20;
            _context3.t2 = _context3['catch'](6);
            _didIteratorError = true;
            _iteratorError = _context3.t2;

          case 24:
            _context3.prev = 24;
            _context3.prev = 25;

            if (!_iteratorNormalCompletion && _iterator.return) {
              _iterator.return();
            }

          case 27:
            _context3.prev = 27;

            if (!_didIteratorError) {
              _context3.next = 30;
              break;
            }

            throw _iteratorError;

          case 30:
            return _context3.finish(27);

          case 31:
            return _context3.finish(24);

          case 32:
            _iteratorNormalCompletion2 = true;
            _didIteratorError2 = false;
            _iteratorError2 = undefined;
            _context3.prev = 35;
            _iterator2 = _getIterator(finish);

          case 37:
            if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
              _context3.next = 51;
              break;
            }

            _ref5 = _step2.value;
            _context3.prev = 39;
            _context3.next = 42;
            return managers_js.GitRefManager.resolve({ fs: fs, gitdir: gitdir, ref: _ref5 });

          case 42:
            _oid = _context3.sent;

            finishingSet.add(_oid);
            _context3.next = 48;
            break;

          case 46:
            _context3.prev = 46;
            _context3.t3 = _context3['catch'](39);

          case 48:
            _iteratorNormalCompletion2 = true;
            _context3.next = 37;
            break;

          case 51:
            _context3.next = 57;
            break;

          case 53:
            _context3.prev = 53;
            _context3.t4 = _context3['catch'](35);
            _didIteratorError2 = true;
            _iteratorError2 = _context3.t4;

          case 57:
            _context3.prev = 57;
            _context3.prev = 58;

            if (!_iteratorNormalCompletion2 && _iterator2.return) {
              _iterator2.return();
            }

          case 60:
            _context3.prev = 60;

            if (!_didIteratorError2) {
              _context3.next = 63;
              break;
            }

            throw _iteratorError2;

          case 63:
            return _context3.finish(60);

          case 64:
            return _context3.finish(57);

          case 65:
            visited /*: Set<string> */ = new _Set();
            _iteratorNormalCompletion4 = true;
            _didIteratorError4 = false;
            _iteratorError4 = undefined;
            _context3.prev = 69;
            _iterator4 = _getIterator(startingSet);

          case 71:
            if (_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done) {
              _context3.next = 78;
              break;
            }

            oid = _step4.value;
            _context3.next = 75;
            return walk(oid);

          case 75:
            _iteratorNormalCompletion4 = true;
            _context3.next = 71;
            break;

          case 78:
            _context3.next = 84;
            break;

          case 80:
            _context3.prev = 80;
            _context3.t5 = _context3['catch'](69);
            _didIteratorError4 = true;
            _iteratorError4 = _context3.t5;

          case 84:
            _context3.prev = 84;
            _context3.prev = 85;

            if (!_iteratorNormalCompletion4 && _iterator4.return) {
              _iterator4.return();
            }

          case 87:
            _context3.prev = 87;

            if (!_didIteratorError4) {
              _context3.next = 90;
              break;
            }

            throw _iteratorError4;

          case 90:
            return _context3.finish(87);

          case 91:
            return _context3.finish(84);

          case 92:
            return _context3.abrupt('return', visited);

          case 93:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this, [[6, 20, 24, 32], [25,, 27, 31], [35, 53, 57, 65], [39, 46], [58,, 60, 64], [69, 80, 84, 92], [85,, 87, 91]]);
  }));

  return function listCommits(_x2) {
    return _ref4.apply(this, arguments);
  };
}();

/** @ignore */
var listObjects = function () {
  var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref8) {

    // We don't do the purest simplest recursion, because we can
    // avoid reading Blob objects entirely since the Tree objects
    // tell us which oids are Blobs and which are Trees.
    var walk = function () {
      var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(oid) {
        var _ref11, type, object, commit, tree, _tree, _iteratorNormalCompletion5, _didIteratorError5, _iteratorError5, _iterator5, _step5, entry;

        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                visited.add(oid);
                _context4.next = 3;
                return managers_js.GitObjectManager.read({ fs: fs, gitdir: gitdir, oid: oid });

              case 3:
                _ref11 = _context4.sent;
                type = _ref11.type;
                object = _ref11.object;

                if (!(type === 'commit')) {
                  _context4.next = 13;
                  break;
                }

                commit = models_js.GitCommit.from(object);
                tree = commit.headers().tree;
                _context4.next = 11;
                return walk(tree);

              case 11:
                _context4.next = 43;
                break;

              case 13:
                if (!(type === 'tree')) {
                  _context4.next = 43;
                  break;
                }

                _tree = models_js.GitTree.from(object);
                _iteratorNormalCompletion5 = true;
                _didIteratorError5 = false;
                _iteratorError5 = undefined;
                _context4.prev = 18;
                _iterator5 = _getIterator( /*: TreeEntry */_tree);

              case 20:
                if (_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done) {
                  _context4.next = 29;
                  break;
                }

                entry = _step5.value;

                visited.add(entry.oid);
                // only recurse for trees

                if (!(entry.type === 'tree')) {
                  _context4.next = 26;
                  break;
                }

                _context4.next = 26;
                return walk(entry.oid);

              case 26:
                _iteratorNormalCompletion5 = true;
                _context4.next = 20;
                break;

              case 29:
                _context4.next = 35;
                break;

              case 31:
                _context4.prev = 31;
                _context4.t0 = _context4['catch'](18);
                _didIteratorError5 = true;
                _iteratorError5 = _context4.t0;

              case 35:
                _context4.prev = 35;
                _context4.prev = 36;

                if (!_iteratorNormalCompletion5 && _iterator5.return) {
                  _iterator5.return();
                }

              case 38:
                _context4.prev = 38;

                if (!_didIteratorError5) {
                  _context4.next = 41;
                  break;
                }

                throw _iteratorError5;

              case 41:
                return _context4.finish(38);

              case 42:
                return _context4.finish(35);

              case 43:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this, [[18, 31, 35, 43], [36,, 38, 42]]);
      }));

      return function walk(_x5) {
        return _ref10.apply(this, arguments);
      };
    }();

    // Let's go walking!


    var dir = _ref8.dir,
        _ref8$gitdir = _ref8.gitdir,
        gitdir = _ref8$gitdir === undefined ? path.join(dir, '.git') : _ref8$gitdir,
        _fs = _ref8.fs,
        oids = _ref8.oids;

    var fs, visited, _iteratorNormalCompletion6, _didIteratorError6, _iteratorError6, _iterator6, _step6, oid;

    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            fs = new models_js.FileSystem(_fs);
            visited /*: Set<string> */ = new _Set();
            _iteratorNormalCompletion6 = true;
            _didIteratorError6 = false;
            _iteratorError6 = undefined;
            _context5.prev = 5;
            _iterator6 = _getIterator(oids);

          case 7:
            if (_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done) {
              _context5.next = 14;
              break;
            }

            oid = _step6.value;
            _context5.next = 11;
            return walk(oid);

          case 11:
            _iteratorNormalCompletion6 = true;
            _context5.next = 7;
            break;

          case 14:
            _context5.next = 20;
            break;

          case 16:
            _context5.prev = 16;
            _context5.t0 = _context5['catch'](5);
            _didIteratorError6 = true;
            _iteratorError6 = _context5.t0;

          case 20:
            _context5.prev = 20;
            _context5.prev = 21;

            if (!_iteratorNormalCompletion6 && _iterator6.return) {
              _iterator6.return();
            }

          case 23:
            _context5.prev = 23;

            if (!_didIteratorError6) {
              _context5.next = 26;
              break;
            }

            throw _iteratorError6;

          case 26:
            return _context5.finish(23);

          case 27:
            return _context5.finish(20);

          case 28:
            return _context5.abrupt('return', visited);

          case 29:
          case 'end':
            return _context5.stop();
        }
      }
    }, _callee5, this, [[5, 16, 20, 28], [21,, 23, 27]]);
  }));

  return function listObjects(_x4) {
    return _ref9.apply(this, arguments);
  };
}();

/** @ignore */
var pack = function () {
  var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_ref12) {
    var dir = _ref12.dir,
        _ref12$gitdir = _ref12.gitdir,
        gitdir = _ref12$gitdir === undefined ? path.join(dir, '.git') : _ref12$gitdir,
        _fs = _ref12.fs,
        oids = _ref12.oids,
        outputStream = _ref12.outputStream;

    var fs, hash, write, writeObject, _iteratorNormalCompletion7, _didIteratorError7, _iteratorError7, _iterator7, _step7, oid, _ref15, type, object, digest;

    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            writeObject = function writeObject(_ref14) {
              var stype = _ref14.stype,
                  object = _ref14.object;

              var lastFour = void 0,
                  multibyte = void 0,
                  length = void 0;
              // Object type is encoded in bits 654
              var type = types[stype];
              if (type === undefined) throw new Error('Unrecognized type: ' + stype);
              // The length encoding get complicated.
              length = object.length;
              // Whether the next byte is part of the variable-length encoded number
              // is encoded in bit 7
              multibyte = length > 15 ? 128 : 0;
              // Last four bits of length is encoded in bits 3210
              lastFour = length & 15;
              // Discard those bits
              length = length >>> 4;
              // The first byte is then (1-bit multibyte?), (3-bit type), (4-bit least sig 4-bits of length)
              var byte = (multibyte | type | lastFour).toString(16);
              write(byte, 'hex');
              // Now we keep chopping away at length 7-bits at a time until its zero,
              // writing out the bytes in what amounts to little-endian order.
              while (multibyte) {
                multibyte = length > 127 ? 128 : 0;
                byte = multibyte | length & 127;
                write(pad(2, byte.toString(16), '0'), 'hex');
                length = length >>> 7;
              }
              // Lastly, we can compress and write the object.
              write(buffer.Buffer.from(pako.deflate(object)));
            };

            write = function write(chunk, enc) {
              outputStream.write(chunk, enc);
              hash.update(chunk, enc);
            };

            fs = new models_js.FileSystem(_fs);
            hash = createHash('sha1');


            write('PACK');
            write('00000002', 'hex');
            // Write a 4 byte (32-bit) int
            write(pad(8, oids.length.toString(16), '0'), 'hex');
            _iteratorNormalCompletion7 = true;
            _didIteratorError7 = false;
            _iteratorError7 = undefined;
            _context6.prev = 10;
            _iterator7 = _getIterator(oids);

          case 12:
            if (_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done) {
              _context6.next = 23;
              break;
            }

            oid = _step7.value;
            _context6.next = 16;
            return managers_js.GitObjectManager.read({ fs: fs, gitdir: gitdir, oid: oid });

          case 16:
            _ref15 = _context6.sent;
            type = _ref15.type;
            object = _ref15.object;

            writeObject({ write: write, object: object, stype: type });

          case 20:
            _iteratorNormalCompletion7 = true;
            _context6.next = 12;
            break;

          case 23:
            _context6.next = 29;
            break;

          case 25:
            _context6.prev = 25;
            _context6.t0 = _context6['catch'](10);
            _didIteratorError7 = true;
            _iteratorError7 = _context6.t0;

          case 29:
            _context6.prev = 29;
            _context6.prev = 30;

            if (!_iteratorNormalCompletion7 && _iterator7.return) {
              _iterator7.return();
            }

          case 32:
            _context6.prev = 32;

            if (!_didIteratorError7) {
              _context6.next = 35;
              break;
            }

            throw _iteratorError7;

          case 35:
            return _context6.finish(32);

          case 36:
            return _context6.finish(29);

          case 37:
            // Write SHA1 checksum
            digest = hash.digest();

            outputStream.end(digest);
            return _context6.abrupt('return', outputStream);

          case 40:
          case 'end':
            return _context6.stop();
        }
      }
    }, _callee6, this, [[10, 25, 29, 37], [30,, 32, 36]]);
  }));

  return function pack(_x6) {
    return _ref13.apply(this, arguments);
  };
}();

exports.models = models_js;
exports.managers = managers_js;
exports.utils = utils_js;
exports.listCommits = listCommits;
exports.listObjects = listObjects;
exports.pack = pack;
//# sourceMappingURL=internal-apis.js.map
