Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
rewrite nodegit specific code using isomorphic-git (WIP) #47
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg committed Jan 3, 2019
1 parent 94158f1 commit 1af040a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
34 changes: 17 additions & 17 deletions lib/blobHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

'use strict';

const Git = require('nodegit');

const { getBlob } = require('./git');
const { resolveRepositoryPath } = require('./utils');

/**
Expand Down Expand Up @@ -43,35 +42,36 @@ function createMiddleware(options) {

const repPath = resolveRepositoryPath(options, owner, repoName);

let repo;
Git.Repository.open(repPath)
.then((repository) => {
repo = repository;
return repo.getBlob(sha);
})
if (sha.match(/[0-9a-f]/g).length !== 40) {
// invalid sha format
res.status(422).json({
message: 'The sha parameter must be exactly 40 characters and contain only [0-9a-f].',
documentation_url: 'https://developer.github.com/v3/git/blobs/#get-a-blob',
});
return;
}
getBlob(repPath, sha)
.then(((blob) => {
res.json({
sha,
size: blob.rawsize(),
sha: blob.oid,
size: blob.size,
url: `${req.protocol}://${host}${req.path}`,
content: `${blob.content().toString('base64')}\n`,
content: `${blob.content.toString('base64')}\n`,
encoding: 'base64',
});
}))
.catch((err) => {
if (err.errno === -3) {
// TODO: use generic errors
if (err.code === 'ReadObjectFail') {
options.logger.debug(`[blobHandler] resource not found: ${err.message}`);
res.status(404).json({
message: 'Not Found',
documentation_url: 'https://developer.github.com/v3/git/blobs/#get-a-blob',
});
} else {
options.logger.debug(`[blobHandler] errno: ${err.errno} errorFuntion: ${err.errorFunction} message: ${err.message} stack: ${err.stack}`);
options.logger.debug(`[blobHandler] code: ${err.code} message: ${err.message} stack: ${err.stack}`);
next(err);
}
})
.finally(() => {
// TODO: cache Repository instances (key: absolute path)
repo.free();
});
};
}
Expand Down
25 changes: 25 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ git.plugins.set('fs', require('fs'));
* Various helper functions for reading git meta-data and content
*/

/**
* Returns the name (abbreviated form) of the currently checked out branch.
*
* @param {string} dir git repo path
* @returns {Promise<string>} name of the currently checked out branch
*/
async function currentBranch(dir) {
return git.currentBranch({ dir, fullname: false });
}

/**
* Determines whether the specified reference is currently checked out in the working dir.
*
Expand Down Expand Up @@ -128,9 +138,24 @@ async function createBlobReadStream(dir, oid) {
return stream;
}

/**
* Returns a stream for reading the specified blob.
*
* @param {string} dir git repo path
* @param {string} id blob id (sha1)
* @returns {Promise<Object>} object containing `oid` (string), `size` (number)
* and `content` (Buffer)
*/
async function getBlob(dir, id) {
const content = await git.readObject({ dir, oid: id, format: 'content' });
return { oid: id, content: content.object, size: content.object.length };
}

module.exports = {
currentBranch,
getRawContent,
resolveBlob,
isCheckedOut,
createBlobReadStream,
getBlob,
};
1 change: 1 addition & 0 deletions lib/rawHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function createMiddleware(options) {
.then(stream => stream.pipe(res));
})
.catch((err) => {
// TODO: use abstract errors
if (err.code === 'TreeOrBlobNotFoundError') {
options.logger.debug(`[rawHandler] resource not found: ${err.message}`);
res.status(404).send('not found.');
Expand Down
6 changes: 2 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const https = require('https');
const util = require('util');

const fse = require('fs-extra');
const Git = require('nodegit');
const pem = require('pem');
const _ = require('lodash');

let logger = require('./logger');
const app = require('./app');
const git = require('./git');
const { resolveRepositoryPath } = require('./utils');

const DEFAULT_REPO_ROOT = './repos';
Expand Down Expand Up @@ -225,9 +225,7 @@ async function start(rawConfig) {
async function getRepoInfo(rawConfig, owner, repo) {
const cfg = rawConfig || await readConfiguration();
const repPath = resolveRepositoryPath(await initConfiguration(cfg), owner, repo);
const rep = await Git.Repository.open(repPath);
const currentBranch = (await rep.head()).shorthand();
rep.free();
const currentBranch = await git.currentBranch(repPath);
return { owner, repo, currentBranch };
}

Expand Down

0 comments on commit 1af040a

Please sign in to comment.