Skip to content

Commit 1af2f1a

Browse files
committed
nodegit: save/apply/drop stash; quickstatus
1 parent 309964f commit 1af2f1a

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

source/git-api.js

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,15 @@ exports.registerApi = (env) => {
185185
if (config.autoStashAndPop) {
186186
const repo = await getRepo(repoPath);
187187
const signature = await repo.defaultSignature();
188-
const oid = await nodegit.Stash.save(repo, signature, 'Ungit: automatic stash', 0);
188+
const oid = await nodegit.Stash.save(
189+
repo,
190+
signature,
191+
'Ungit: automatic stash',
192+
nodegit.Stash.FLAGS.INCLUDE_UNTRACKED
193+
).catch(err => {
194+
// TODO figure out which error is for emtpy repo
195+
console.error('Stash failed', err);
196+
});
189197
const out = await fn();
190198
if (!oid) return out;
191199
let index;
@@ -951,6 +959,7 @@ exports.registerApi = (env) => {
951959
}
952960
);
953961

962+
<<<<<<< HEAD
954963
app.get(`${exports.pathPrefix}/quickstatus`, ensureAuthenticated, (req, res) => {
955964
const task = fs.isExists(req.query.path).then((exists) => {
956965
return exists
@@ -959,6 +968,23 @@ exports.registerApi = (env) => {
959968
});
960969
jsonResultOrFailProm(res, task);
961970
});
971+
=======
972+
app.get(
973+
`${exports.pathPrefix}/quickstatus`,
974+
ensureAuthenticated,
975+
jw(async req => {
976+
const repoPath = path.normalize(req.query.path);
977+
if (!(await fs.isExists(repoPath))) return { type: 'no-such-path', gitRootPath: repoPath };
978+
try {
979+
const repo = await getRepo(repoPath);
980+
if (repo.isBare()) return { type: 'bare', gitRootPath: repo.path().replace(/\/$/, '') };
981+
return { type: 'inited', gitRootPath: repo.workdir().replace(/\/$/, '') };
982+
} catch {
983+
return { type: 'uninited', gitRootPath: repoPath };
984+
}
985+
})
986+
);
987+
>>>>>>> 0e2f0631... nodegit: save/apply/drop stash; quickstatus
962988

963989
/**
964990
* @param {nodegit.Commit} c
@@ -1015,28 +1041,44 @@ exports.registerApi = (env) => {
10151041
})
10161042
);
10171043

1018-
app.post(`${exports.pathPrefix}/stashes`, ensureAuthenticated, ensurePathExists, (req, res) => {
1019-
jsonResultOrFailProm(
1020-
res,
1021-
gitPromise(['stash', 'save', '--include-untracked', req.body.message || ''], req.body.path)
1022-
)
1023-
.finally(emitGitDirectoryChanged.bind(null, req.body.path))
1024-
.finally(emitWorkingTreeChanged.bind(null, req.body.path));
1025-
});
1044+
app.post(
1045+
`${exports.pathPrefix}/stashes`,
1046+
ensureAuthenticated,
1047+
ensurePathExists,
1048+
jw(async req => {
1049+
const { path: repoPath, message = '' } = req.body;
1050+
const repo = await getRepo(repoPath);
1051+
const signature = await repo.defaultSignature();
1052+
const oid = await nodegit.Stash.save(
1053+
repo,
1054+
signature,
1055+
message,
1056+
nodegit.Stash.FLAGS.INCLUDE_UNTRACKED
1057+
);
1058+
await emitGitDirectoryChanged(repoPath);
1059+
await emitWorkingTreeChanged(repoPath);
1060+
return oid;
1061+
})
1062+
);
10261063

10271064
app.delete(
10281065
`${exports.pathPrefix}/stashes/:id`,
10291066
ensureAuthenticated,
10301067
ensurePathExists,
1031-
(req, res) => {
1032-
const type = req.query.apply === 'true' ? 'apply' : 'drop';
1033-
jsonResultOrFailProm(
1034-
res,
1035-
gitPromise(['stash', type, `stash@{${req.params.id}}`], req.query.path)
1036-
)
1037-
.finally(emitGitDirectoryChanged.bind(null, req.query.path))
1038-
.finally(emitWorkingTreeChanged.bind(null, req.query.path));
1039-
}
1068+
jw(async req => {
1069+
const { path: repoPath, apply } = req.query;
1070+
const { id } = req.params;
1071+
const index = Number(id);
1072+
if (isNaN(index) || index < 0) throw new Error(`Invalid index ${id}`);
1073+
const repo = await getRepo(repoPath);
1074+
if (apply === 'true') {
1075+
await nodegit.Stash.apply(repo, index);
1076+
} else {
1077+
await nodegit.Stash.drop(repo, index);
1078+
}
1079+
await emitGitDirectoryChanged(repoPath);
1080+
await emitWorkingTreeChanged(repoPath);
1081+
})
10401082
);
10411083

10421084
app.get(`${exports.pathPrefix}/gitconfig`, ensureAuthenticated, (req, res) => {

source/server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ if (config.allowedIPs) {
100100
res
101101
.status(403)
102102
.send(
103-
403,
104103
'<h3>This host is not authorized to connect</h3>' +
105104
'<p>You are trying to connect to an Ungit instance from an unathorized host.</p>'
106105
);
@@ -386,7 +385,7 @@ app.post('/api/userconfig', ensureAuthenticated, (req, res) => {
386385
});
387386

388387
app.get('/api/fs/exists', ensureAuthenticated, (req, res) => {
389-
res.json(fs.existsSync(req.query['path']));
388+
res.json(fs.existsSync(req.query.path));
390389
});
391390

392391
app.get('/api/fs/listDirectories', ensureAuthenticated, (req, res) => {

0 commit comments

Comments
 (0)