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 1af2f1a commit 4b9fe01
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 65 deletions.
119 changes: 56 additions & 63 deletions source/git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,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 => {
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 @@ -190,15 +192,17 @@ exports.registerApi = (env) => {
signature,
'Ungit: automatic stash',
nodegit.Stash.FLAGS.INCLUDE_UNTRACKED
).catch(err => {
// TODO figure out which error is for emtpy repo
console.error('Stash failed', err);
).catch((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 @@ -207,13 +211,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 @@ -349,7 +386,7 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/reset`,
ensureAuthenticated,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const repoPath = req.body.path;
await autoStash(repoPath, () =>
gitPromise(['reset', `--${req.body.mode}`, req.body.to], repoPath)
Expand Down Expand Up @@ -611,9 +648,9 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/tags`,
ensureAuthenticated,
ensurePathExists,
jw(req => {
jw((req) => {
let pathToRepo = req.query.path;
return nodegit.Repository.open(pathToRepo).then(repo => nodegit.Tag.list(repo));
return nodegit.Repository.open(pathToRepo).then((repo) => nodegit.Tag.list(repo));
})
);

Expand Down Expand Up @@ -693,7 +730,7 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/checkout`,
ensureAuthenticated,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const arg = !!req.body.sha1
? ['checkout', '-b', req.body.name.trim(), req.body.sha1]
: ['checkout', req.body.name.trim()];
Expand All @@ -708,7 +745,7 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/cherrypick`,
ensureAuthenticated,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const repoPath = req.body.path;
await autoStash(repoPath, () => gitPromise(['cherry-pick', req.body.name.trim()], repoPath));
await emitGitDirectoryChanged(repoPath);
Expand Down Expand Up @@ -959,77 +996,33 @@ exports.registerApi = (env) => {
}
);

<<<<<<< HEAD
app.get(`${exports.pathPrefix}/quickstatus`, ensureAuthenticated, (req, res) => {
const task = fs.isExists(req.query.path).then((exists) => {
return exists
? gitPromise.revParse(req.query.path)
: { type: 'no-such-path', gitRootPath: req.query.path };
});
jsonResultOrFailProm(res, task);
});
=======
app.get(
`${exports.pathPrefix}/quickstatus`,
ensureAuthenticated,
jw(async req => {
jw(async (req) => {
const repoPath = path.normalize(req.query.path);
if (!(await fs.isExists(repoPath))) return { type: 'no-such-path', gitRootPath: repoPath };
try {
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 };
}
})
);
>>>>>>> 0e2f0631... nodegit: save/apply/drop stash; quickstatus

/**
* @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,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const repo = await getRepo(req.query.path);
const oids = [];
await nodegit.Stash.foreach(repo, (index, message, oid) => {
oids.push(oid);
});
const stashes = await Promise.all(oids.map(oid => repo.getCommit(oid)));
const stashes = await Promise.all(oids.map((oid) => repo.getCommit(oid)));
return Promise.all(
stashes.map(async (stash, index) => ({
...formatCommit(stash),
Expand All @@ -1045,7 +1038,7 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/stashes`,
ensureAuthenticated,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const { path: repoPath, message = '' } = req.body;
const repo = await getRepo(repoPath);
const signature = await repo.defaultSignature();
Expand All @@ -1065,7 +1058,7 @@ exports.registerApi = (env) => {
`${exports.pathPrefix}/stashes/:id`,
ensureAuthenticated,
ensurePathExists,
jw(async req => {
jw(async (req) => {
const { path: repoPath, apply } = req.query;
const { id } = req.params;
const index = Number(id);
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 4b9fe01

Please sign in to comment.