-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
153 lines (143 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
* Copyright (c) 2012, 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env node */
var api = require('../api'), writeError = api.writeError, writeResponse = api.writeResponse;
var git = require('nodegit');
var clone = require('./clone');
var path = require('path');
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
module.exports = {};
module.exports.router = function(options) {
var fileRoot = options.fileRoot;
if (!fileRoot) { throw new Error('options.fileRoot is required'); }
return express.Router()
.use(bodyParser.json())
.get(fileRoot + '*', getIndex)
.put(fileRoot + '*', putIndex)
.post(fileRoot + '*', postIndex);
function getIndex(req, res) {
var repo;
var index;
var filePath;
return clone.getRepo(req)
.then(function(repoResult) {
repo = repoResult;
filePath = clone.getfileRelativePath(repo,req);
return repo;
})
.then(function(repo) {
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
var indexEntry = index.getByPath(filePath);
if (!indexEntry) {
// new untracked file that's not in the index
var readIfExists = req.headers ? Boolean(req.headers['read-if-exists']).valueOf() : false;
if (typeof readIfExists === 'boolean' && readIfExists) {
return 204;
}
return 404;
}
return git.Blob.lookup(repo, indexEntry.id);
})
.then(function(blob) {
if (typeof blob === 'number') {
if (blob === 204) {
api.sendStatus(204, res);
} else {
writeError(404, res, filePath + " not found in index");
}
} else {
writeResponse(200, res, {"Content-Type":"application/octect-stream"}, blob.toString(), false, true);
}
})
.catch(function(err) {
writeError(404, res, err.message);
});
}
function putIndex(req, res) {
var index, repo;
var filePath;
return clone.getRepo(req)
.then(function(_repo) {
repo = _repo;
filePath = clone.getfileRelativePath(repo,req);
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
function doPath(p) {
if (fs.existsSync(path.join(repo.workdir(), p))) {
return index.addByPath(p);
}
return index.removeByPath(p);
}
if (req.body.Path) {
return Promise.all(req.body.Path.map(doPath));
}
return doPath(filePath);
})
.then(function() {
// this will write both files to the index
return index.write();
})
.then(function() {
writeResponse(200, res);
}).catch(function(err) {
writeError(404, res, err.message);
});
}
function postIndex(req, res) {
var repo;
var resetType = req.body.Reset;
var filePath = clone.getfileAbsolutePath(req);
return clone.getRepo(req)
.then(function(_repo) {
repo = _repo;
return git.AnnotatedCommit.fromRevspec(repo, req.body.Commit || "HEAD");
})
.then(function(commit) {
return repo.getCommit(commit.id());
})
.then(function(commit) {
if (req.body.Path) {
var paths = req.body.Path;
if (typeof paths === "string") {
paths = [paths];
}
return git.Reset.default(repo, commit, paths);
} else if (resetType) {
var reset_type = git.Reset.TYPE.HARD;
switch (resetType) {
case "HARD":
reset_type = git.Reset.TYPE.HARD;
break;
case "MIXED":
reset_type = git.Reset.TYPE.MIXED;
break;
case "SOFT":
reset_type = git.Reset.TYPE.SOFT;
break;
}
return git.Reset.reset(repo, commit, reset_type, {});
}
return git.Reset.default(repo, commit, [filePath]);
})
.then(function() {
writeResponse(200, res);
}).catch(function(err) {
writeError(404, res, err.message);
});
}
};