Skip to content

Commit

Permalink
make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Apr 27, 2020
1 parent 0e2f063 commit 23f0436
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
84 changes: 44 additions & 40 deletions source/git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ exports.registerApi = env => {
);

const repoPs = {};

/**
* memoize nodegit opened repos
* @param {string} repoPath the path to the repository
* @returns {Promise<nodegit.Repository>}
*/
const getRepo = repoPath => {
if (!repoPs[repoPath]) {
repoPs[repoPath] = nodegit.Repository.open(repoPath);
repoPs[repoPath] = nodegit.Repository.open(repoPath).catch(err => {
repoPs[repoPath] = false;
throw err;
});
}
return repoPs[repoPath];
};
Expand All @@ -179,14 +181,16 @@ exports.registerApi = env => {
'Ungit: automatic stash',
nodegit.Stash.FLAGS.INCLUDE_UNTRACKED
).catch(err => {
// TODO figure out which error is for emtpy repo
console.error('Stash failed', err);
// nothing to stash
if (err.errno === -3) return;
console.error(`autostash got errno %d: %s`, err.errno, err.message);
throw err;
});
const out = await fn();
if (!oid) return out;
let index;
await nodegit.Stash.foreach(repo, (i, _msg, stashOid) => {
if (stashOid === oid) index = i;
if (stashOid.equal(oid)) index = i;
});
if (index != null) await nodegit.Stash.pop(repo, index);
return out;
Expand All @@ -195,13 +199,46 @@ exports.registerApi = env => {
}
};

/**
* @param {nodegit.Commit} c
*/
const formatCommit = c => ({
commitDate: c.date().toJSON(),
message: c.message(),
sha1: c.sha(),
});
/**
* @param {nodegit.Commit} c
*/
const getFileStats = async c => {
const diffList = await c.getDiff();
// Each diff has the entire patch set for some reason
const patches = await (diffList[0] && diffList[0].patches());
if (!(patches && patches.length)) return [];

return patches.map(patch => {
const stats = patch.lineStats();
const oldFileName = patch.oldFile().path();
const displayName = patch.newFile().path();
return {
additions: stats.total_additions,
deletions: stats.total_deletions,
fileName: displayName,
oldFileName,
displayName,
// TODO figure out how to get this
type: 'text',
};
});
};

const jsonResultOrFailProm = (res, promise) =>
// TODO shouldn't this be a boolean instead of an object?
promise
.then(o => res.json(o == null ? {} : o))
.catch(err => {
winston.warn('Responding with ERROR: ', JSON.stringify(err));
res.status(500).json(err);
res.status(400).json(err);
});

const w = fn => (req, res) =>
Expand Down Expand Up @@ -957,45 +994,12 @@ exports.registerApi = env => {
const repo = await getRepo(repoPath);
if (repo.isBare()) return { type: 'bare', gitRootPath: repo.path().replace(/\/$/, '') };
return { type: 'inited', gitRootPath: repo.workdir().replace(/\/$/, '') };
} catch {
} catch (err) {
return { type: 'uninited', gitRootPath: repoPath };
}
})
);

/**
* @param {nodegit.Commit} c
*/
const formatCommit = c => ({
commitDate: c.date().toJSON(),
message: c.message(),
sha1: c.sha(),
});
/**
* @param {nodegit.Commit} c
*/
const getFileStats = async c => {
const diffList = await c.getDiff();
// Each diff has the entire patch set for some reason
const patches = await diffList[0]?.patches();
if (!patches?.length) return [];

return patches.map(patch => {
const stats = patch.lineStats();
const oldFileName = patch.oldFile().path();
const displayName = patch.newFile().path();
return {
additions: stats.total_additions,
deletions: stats.total_deletions,
fileName: displayName,
oldFileName,
displayName,
// TODO figure out how to get this
type: 'text',
};
});
};

app.get(
`${exports.pathPrefix}/stashes`,
ensureAuthenticated,
Expand Down
4 changes: 2 additions & 2 deletions test/spec.git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('git-api', () => {
return common.post(req, '/discardchanges', { path: testDir, file: testFile });
});

it('modifying a test file should work', () => {
it('modifying a test file should work part deux', () => {
return common.post(req, '/testing/changefile', { file: path.join(testDir, testFile) });
});

Expand Down Expand Up @@ -281,7 +281,7 @@ describe('git-api', () => {
return common.post(req, '/testing/createfile', { file: path.join(testDir, testFile3) });
});

it('status should list the new file', () => {
it('status should list the new file once again', () => {
return common.get(req, '/status', { path: testDir }).then(res => {
expect(Object.keys(res.files).length).to.be(1);
expect(res.files[testFile3]).to.eql({
Expand Down

0 comments on commit 23f0436

Please sign in to comment.