Skip to content

Commit

Permalink
nodegit: save/apply/drop stash; quickstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Apr 26, 2020
1 parent 1459e9c commit f403c58
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
83 changes: 57 additions & 26 deletions source/git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 1 addition & 2 deletions source/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ if (config.allowedIPs) {
res
.status(403)
.send(
403,
'<h3>This host is not authorized to connect</h3>' +
'<p>You are trying to connect to an Ungit instance from an unathorized host.</p>'
);
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit f403c58

Please sign in to comment.