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 27, 2020
1 parent 309964f commit 1af2f1a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
78 changes: 60 additions & 18 deletions source/git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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 @@ -951,6 +959,7 @@ exports.registerApi = (env) => {
}
);

<<<<<<< HEAD
app.get(`${exports.pathPrefix}/quickstatus`, ensureAuthenticated, (req, res) => {
const task = fs.isExists(req.query.path).then((exists) => {
return exists
Expand All @@ -959,6 +968,23 @@ exports.registerApi = (env) => {
});
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 };
}
})
);
>>>>>>> 0e2f0631... nodegit: save/apply/drop stash; quickstatus

/**
* @param {nodegit.Commit} c
Expand Down Expand Up @@ -1015,28 +1041,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 1af2f1a

Please sign in to comment.