Skip to content

Commit 4b9fe01

Browse files
committed
make tests pass
1 parent 1af2f1a commit 4b9fe01

File tree

2 files changed

+58
-65
lines changed

2 files changed

+58
-65
lines changed

source/git-api.js

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,17 @@ exports.registerApi = (env) => {
168168
);
169169

170170
const repoPs = {};
171-
172171
/**
173172
* memoize nodegit opened repos
174173
* @param {string} repoPath the path to the repository
175174
* @returns {Promise<nodegit.Repository>}
176175
*/
177-
const getRepo = repoPath => {
176+
const getRepo = (repoPath) => {
178177
if (!repoPs[repoPath]) {
179-
repoPs[repoPath] = nodegit.Repository.open(repoPath);
178+
repoPs[repoPath] = nodegit.Repository.open(repoPath).catch((err) => {
179+
repoPs[repoPath] = false;
180+
throw err;
181+
});
180182
}
181183
return repoPs[repoPath];
182184
};
@@ -190,15 +192,17 @@ exports.registerApi = (env) => {
190192
signature,
191193
'Ungit: automatic stash',
192194
nodegit.Stash.FLAGS.INCLUDE_UNTRACKED
193-
).catch(err => {
194-
// TODO figure out which error is for emtpy repo
195-
console.error('Stash failed', err);
195+
).catch((err) => {
196+
// nothing to stash
197+
if (err.errno === -3) return;
198+
console.error(`autostash got errno %d: %s`, err.errno, err.message);
199+
throw err;
196200
});
197201
const out = await fn();
198202
if (!oid) return out;
199203
let index;
200204
await nodegit.Stash.foreach(repo, (i, _msg, stashOid) => {
201-
if (stashOid === oid) index = i;
205+
if (stashOid.equal(oid)) index = i;
202206
});
203207
if (index != null) await nodegit.Stash.pop(repo, index);
204208
return out;
@@ -207,13 +211,46 @@ exports.registerApi = (env) => {
207211
}
208212
};
209213

214+
/**
215+
* @param {nodegit.Commit} c
216+
*/
217+
const formatCommit = (c) => ({
218+
commitDate: c.date().toJSON(),
219+
message: c.message(),
220+
sha1: c.sha(),
221+
});
222+
/**
223+
* @param {nodegit.Commit} c
224+
*/
225+
const getFileStats = async (c) => {
226+
const diffList = await c.getDiff();
227+
// Each diff has the entire patch set for some reason
228+
const patches = await (diffList[0] && diffList[0].patches());
229+
if (!(patches && patches.length)) return [];
230+
231+
return patches.map((patch) => {
232+
const stats = patch.lineStats();
233+
const oldFileName = patch.oldFile().path();
234+
const displayName = patch.newFile().path();
235+
return {
236+
additions: stats.total_additions,
237+
deletions: stats.total_deletions,
238+
fileName: displayName,
239+
oldFileName,
240+
displayName,
241+
// TODO figure out how to get this
242+
type: 'text',
243+
};
244+
});
245+
};
246+
210247
const jsonResultOrFailProm = (res, promise) =>
211248
// TODO shouldn't this be a boolean instead of an object?
212249
promise
213250
.then((o) => res.json(o == null ? {} : o))
214251
.catch((err) => {
215252
winston.warn('Responding with ERROR: ', JSON.stringify(err));
216-
res.status(500).json(err);
253+
res.status(400).json(err);
217254
});
218255

219256
const w = (fn) => (req, res) =>
@@ -349,7 +386,7 @@ exports.registerApi = (env) => {
349386
`${exports.pathPrefix}/reset`,
350387
ensureAuthenticated,
351388
ensurePathExists,
352-
jw(async req => {
389+
jw(async (req) => {
353390
const repoPath = req.body.path;
354391
await autoStash(repoPath, () =>
355392
gitPromise(['reset', `--${req.body.mode}`, req.body.to], repoPath)
@@ -611,9 +648,9 @@ exports.registerApi = (env) => {
611648
`${exports.pathPrefix}/tags`,
612649
ensureAuthenticated,
613650
ensurePathExists,
614-
jw(req => {
651+
jw((req) => {
615652
let pathToRepo = req.query.path;
616-
return nodegit.Repository.open(pathToRepo).then(repo => nodegit.Tag.list(repo));
653+
return nodegit.Repository.open(pathToRepo).then((repo) => nodegit.Tag.list(repo));
617654
})
618655
);
619656

@@ -693,7 +730,7 @@ exports.registerApi = (env) => {
693730
`${exports.pathPrefix}/checkout`,
694731
ensureAuthenticated,
695732
ensurePathExists,
696-
jw(async req => {
733+
jw(async (req) => {
697734
const arg = !!req.body.sha1
698735
? ['checkout', '-b', req.body.name.trim(), req.body.sha1]
699736
: ['checkout', req.body.name.trim()];
@@ -708,7 +745,7 @@ exports.registerApi = (env) => {
708745
`${exports.pathPrefix}/cherrypick`,
709746
ensureAuthenticated,
710747
ensurePathExists,
711-
jw(async req => {
748+
jw(async (req) => {
712749
const repoPath = req.body.path;
713750
await autoStash(repoPath, () => gitPromise(['cherry-pick', req.body.name.trim()], repoPath));
714751
await emitGitDirectoryChanged(repoPath);
@@ -959,77 +996,33 @@ exports.registerApi = (env) => {
959996
}
960997
);
961998

962-
<<<<<<< HEAD
963-
app.get(`${exports.pathPrefix}/quickstatus`, ensureAuthenticated, (req, res) => {
964-
const task = fs.isExists(req.query.path).then((exists) => {
965-
return exists
966-
? gitPromise.revParse(req.query.path)
967-
: { type: 'no-such-path', gitRootPath: req.query.path };
968-
});
969-
jsonResultOrFailProm(res, task);
970-
});
971-
=======
972999
app.get(
9731000
`${exports.pathPrefix}/quickstatus`,
9741001
ensureAuthenticated,
975-
jw(async req => {
1002+
jw(async (req) => {
9761003
const repoPath = path.normalize(req.query.path);
9771004
if (!(await fs.isExists(repoPath))) return { type: 'no-such-path', gitRootPath: repoPath };
9781005
try {
9791006
const repo = await getRepo(repoPath);
9801007
if (repo.isBare()) return { type: 'bare', gitRootPath: repo.path().replace(/\/$/, '') };
9811008
return { type: 'inited', gitRootPath: repo.workdir().replace(/\/$/, '') };
982-
} catch {
1009+
} catch (err) {
9831010
return { type: 'uninited', gitRootPath: repoPath };
9841011
}
9851012
})
9861013
);
987-
>>>>>>> 0e2f0631... nodegit: save/apply/drop stash; quickstatus
988-
989-
/**
990-
* @param {nodegit.Commit} c
991-
*/
992-
const formatCommit = c => ({
993-
commitDate: c.date().toJSON(),
994-
message: c.message(),
995-
sha1: c.sha(),
996-
});
997-
/**
998-
* @param {nodegit.Commit} c
999-
*/
1000-
const getFileStats = async c => {
1001-
const diffList = await c.getDiff();
1002-
// Each diff has the entire patch set for some reason
1003-
const patches = await diffList[0]?.patches();
1004-
if (!patches?.length) return [];
1005-
1006-
return patches.map(patch => {
1007-
const stats = patch.lineStats();
1008-
const oldFileName = patch.oldFile().path();
1009-
const displayName = patch.newFile().path();
1010-
return {
1011-
additions: stats.total_additions,
1012-
deletions: stats.total_deletions,
1013-
fileName: displayName,
1014-
oldFileName,
1015-
displayName,
1016-
// TODO figure out how to get this
1017-
type: 'text',
1018-
};
1019-
});
1020-
};
10211014

10221015
app.get(
10231016
`${exports.pathPrefix}/stashes`,
10241017
ensureAuthenticated,
10251018
ensurePathExists,
1026-
jw(async req => {
1019+
jw(async (req) => {
10271020
const repo = await getRepo(req.query.path);
10281021
const oids = [];
10291022
await nodegit.Stash.foreach(repo, (index, message, oid) => {
10301023
oids.push(oid);
10311024
});
1032-
const stashes = await Promise.all(oids.map(oid => repo.getCommit(oid)));
1025+
const stashes = await Promise.all(oids.map((oid) => repo.getCommit(oid)));
10331026
return Promise.all(
10341027
stashes.map(async (stash, index) => ({
10351028
...formatCommit(stash),
@@ -1045,7 +1038,7 @@ exports.registerApi = (env) => {
10451038
`${exports.pathPrefix}/stashes`,
10461039
ensureAuthenticated,
10471040
ensurePathExists,
1048-
jw(async req => {
1041+
jw(async (req) => {
10491042
const { path: repoPath, message = '' } = req.body;
10501043
const repo = await getRepo(repoPath);
10511044
const signature = await repo.defaultSignature();
@@ -1065,7 +1058,7 @@ exports.registerApi = (env) => {
10651058
`${exports.pathPrefix}/stashes/:id`,
10661059
ensureAuthenticated,
10671060
ensurePathExists,
1068-
jw(async req => {
1061+
jw(async (req) => {
10691062
const { path: repoPath, apply } = req.query;
10701063
const { id } = req.params;
10711064
const index = Number(id);

test/spec.git-api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ describe('git-api', () => {
209209
return common.post(req, '/discardchanges', { path: testDir, file: testFile });
210210
});
211211

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

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

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

0 commit comments

Comments
 (0)