diff --git a/source/git-api.js b/source/git-api.js index e4f894320..1b52a5d56 100644 --- a/source/git-api.js +++ b/source/git-api.js @@ -173,7 +173,15 @@ exports.registerApi = env => { if (config.autoStashAndPop) { const repo = await getRepo(repoPath); const signature = await repo.defaultSignature(); - const oid = await nodegit.Stash.save(repo, signature, 'Ungit: automatic stash', 0); + const oid = await nodegit.Stash.save( + repo, + 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); + }); const out = await fn(); if (!oid) return out; let index; @@ -939,14 +947,21 @@ exports.registerApi = env => { } ); - 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 => { + 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 { + return { type: 'uninited', gitRootPath: repoPath }; + } + }) + ); /** * @param {nodegit.Commit} c @@ -1003,28 +1018,44 @@ exports.registerApi = env => { }) ); - app.post(`${exports.pathPrefix}/stashes`, ensureAuthenticated, ensurePathExists, (req, res) => { - jsonResultOrFailProm( - res, - gitPromise(['stash', 'save', '--include-untracked', req.body.message || ''], req.body.path) - ) - .finally(emitGitDirectoryChanged.bind(null, req.body.path)) - .finally(emitWorkingTreeChanged.bind(null, req.body.path)); - }); + app.post( + `${exports.pathPrefix}/stashes`, + ensureAuthenticated, + ensurePathExists, + jw(async req => { + const { path: repoPath, message = '' } = req.body; + const repo = await getRepo(repoPath); + const signature = await repo.defaultSignature(); + const oid = await nodegit.Stash.save( + repo, + signature, + message, + nodegit.Stash.FLAGS.INCLUDE_UNTRACKED + ); + await emitGitDirectoryChanged(repoPath); + await emitWorkingTreeChanged(repoPath); + return oid; + }) + ); app.delete( `${exports.pathPrefix}/stashes/:id`, ensureAuthenticated, ensurePathExists, - (req, res) => { - const type = req.query.apply === 'true' ? 'apply' : 'drop'; - jsonResultOrFailProm( - res, - gitPromise(['stash', type, `stash@{${req.params.id}}`], req.query.path) - ) - .finally(emitGitDirectoryChanged.bind(null, req.query.path)) - .finally(emitWorkingTreeChanged.bind(null, req.query.path)); - } + jw(async req => { + const { path: repoPath, apply } = req.query; + const { id } = req.params; + const index = Number(id); + if (isNaN(index) || index < 0) throw new Error(`Invalid index ${id}`); + const repo = await getRepo(repoPath); + if (apply === 'true') { + await nodegit.Stash.apply(repo, index); + } else { + await nodegit.Stash.drop(repo, index); + } + await emitGitDirectoryChanged(repoPath); + await emitWorkingTreeChanged(repoPath); + }) ); app.get(`${exports.pathPrefix}/gitconfig`, ensureAuthenticated, (req, res) => { diff --git a/source/server.js b/source/server.js index 8e16a4866..b51d189bc 100644 --- a/source/server.js +++ b/source/server.js @@ -100,7 +100,6 @@ if (config.allowedIPs) { res .status(403) .send( - 403, '
You are trying to connect to an Ungit instance from an unathorized host.
' ); @@ -386,7 +385,7 @@ app.post('/api/userconfig', ensureAuthenticated, (req, res) => { }); app.get('/api/fs/exists', ensureAuthenticated, (req, res) => { - res.json(fs.existsSync(req.query['path'])); + res.json(fs.existsSync(req.query.path)); }); app.get('/api/fs/listDirectories', ensureAuthenticated, (req, res) => {