base diff support

This commit is contained in:
Davide Alberani 2018-01-21 11:20:42 +01:00
parent c0889e1801
commit fcbef86f3b
4 changed files with 110 additions and 6 deletions

View file

@ -124,7 +124,27 @@ def get_history(id_):
delete = 0 delete = 0
history.append({'id': commit_id, 'message': message, 'insertions': insert, 'deletions': delete, history.append({'id': commit_id, 'message': message, 'insertions': insert, 'deletions': delete,
'changes': max(insert, delete)}) 'changes': max(insert, delete)})
return history lastid = None
if history and 'id' in history[0]:
lastid = history[0]['id']
for idx, item in enumerate(history):
item['seq'] = idx + 1
return {'history': history, 'lastid': lastid}
def get_diff(id_, diff, oldid=None):
def _history(id_, diff, oldid, queue):
os.chdir('storage/%s' % id_)
p = subprocess.Popen([GIT_CMD, 'diff', diff, oldid or '%s~' % diff], stdout=subprocess.PIPE)
stdout, _ = p.communicate()
queue.put(stdout)
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=_history, args=(id_, diff, oldid, queue))
p.start()
res = queue.get().decode('utf-8')
p.join()
return {'diff': res}
def scheduler_update(scheduler, id_): def scheduler_update(scheduler, id_):
schedule = get_schedule(id_, addID=False) schedule = get_schedule(id_, addID=False)
@ -302,7 +322,13 @@ class ResetSchedulesHandler(BaseHandler):
class HistoryHandler(BaseHandler): class HistoryHandler(BaseHandler):
@gen.coroutine @gen.coroutine
def get(self, id_, *args, **kwargs): def get(self, id_, *args, **kwargs):
self.write({'history': get_history(id_)}) self.write(get_history(id_))
class DiffHandler(BaseHandler):
@gen.coroutine
def get(self, id_, diff, oldid=None, *args, **kwargs):
self.write(get_diff(id_, diff, oldid))
class TemplateHandler(BaseHandler): class TemplateHandler(BaseHandler):
@ -347,13 +373,17 @@ def serve():
_reset_schedules_path = r'schedules/reset' _reset_schedules_path = r'schedules/reset'
_schedules_path = r'schedules/?(?P<id_>\d+)?' _schedules_path = r'schedules/?(?P<id_>\d+)?'
_history_path = r'history/?(?P<id_>\d+)' _history_path = r'history/?(?P<id_>\d+)'
_diff_path = r'diff/(?P<id_>\d+)/(?P<diff>\s+)/?(?P<oldid>\s+)?'
_diff_path = r'diff/(?P<id_>\d+)/(?P<diff>[0-9a-f]+)/?(?P<oldid>[0-9a-f]+)?/?'
application = tornado.web.Application([ application = tornado.web.Application([
('/api/%s' % _reset_schedules_path, ResetSchedulesHandler, init_params), (r'/api/%s' % _reset_schedules_path, ResetSchedulesHandler, init_params),
(r'/api/v%s/%s' % (API_VERSION, _reset_schedules_path), ResetSchedulesHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _reset_schedules_path), ResetSchedulesHandler, init_params),
('/api/%s' % _schedules_path, SchedulesHandler, init_params), (r'/api/%s' % _schedules_path, SchedulesHandler, init_params),
(r'/api/v%s/%s' % (API_VERSION, _schedules_path), SchedulesHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _schedules_path), SchedulesHandler, init_params),
('/api/%s' % _history_path, HistoryHandler, init_params), (r'/api/%s' % _history_path, HistoryHandler, init_params),
(r'/api/v%s/%s' % (API_VERSION, _history_path), HistoryHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _history_path), HistoryHandler, init_params),
(r'/api/%s' % _diff_path, DiffHandler, init_params),
(r'/api/v%s/%s' % (API_VERSION, _diff_path), DiffHandler, init_params),
(r'/?(.*)', TemplateHandler, init_params), (r'/?(.*)', TemplateHandler, init_params),
], ],
static_path=os.path.join(os.path.dirname(__file__), 'dist/static'), static_path=os.path.join(os.path.dirname(__file__), 'dist/static'),

2
dist/base.html vendored
View file

@ -6,10 +6,12 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons">
<link rel="stylesheet" href="/static/css/vue-material.min.css"> <link rel="stylesheet" href="/static/css/vue-material.min.css">
<link rel="stylesheet" href="/static/css/themes/default.css"> <link rel="stylesheet" href="/static/css/themes/default.css">
<link rel="stylesheet" href="/static/css/diff2html.css">
<script src="/static/js/lodash.min.js"></script> <script src="/static/js/lodash.min.js"></script>
<script src="/static/js/vue.js"></script> <script src="/static/js/vue.js"></script>
<script src="/static/js/vue-material.min.js"></script> <script src="/static/js/vue-material.min.js"></script>
<script src="/static/js/axios.min.js"></script> <script src="/static/js/axios.min.js"></script>
<script src="/static/js/diff2html.js"></script>
</head> </head>
<body> <body>
{% block body %}{% end %} {% block body %}{% end %}

65
dist/diff.html vendored
View file

@ -1,5 +1,68 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block body %} {% block body %}
diff page {{ id }} <div id="app">
<div class="md-layout">
<div class="md-layout-item" md-card>
<div id="diffpanel"></div>
</div>
</div>
</div>
<script>
Vue.use(VueMaterial.default);
var app = new Vue({
el: '#app',
delimiters: ['${', '}'],
data: {
history: [],
difftext: '',
id: "{{id}}",
{% try %}
{% if diff %}
diff: "{{diff}}",
{% end %}
{% except %}
diff: null,
{% end %}
{% try %}
{% if oldid %}
oldid: "{{oldid}}",
{% end %}
{% except %}
oldid: null,
{% end %}
},
mounted: function() {
this.getDiff();
},
methods: {
getDiff: function() {
self = this;
var data = axios.get('/api/diff/' + this.id + '/' + this.diff + '/' + (this.oldid || '')).then(function(response) {
console.log(response);
self.difftext = response.data.diff;
var pretty_diff = Diff2Html.getPrettyHtml(self.difftext);
document.getElementById('diffpanel').innerHTML = pretty_diff;
});
}
}
});
</script>
<style>
body {
background-color: white;
}
.md-table {
width: 60%;
min-height: 800px;
max-height: 800px;
}
</style>
{% end %} {% end %}

9
dist/history.html vendored
View file

@ -9,6 +9,11 @@
<h1 class="md-title">History</h1> <h1 class="md-title">History</h1>
</md-table-toolbar> </md-table-toolbar>
<md-table-row slot="md-table-row" slot-scope="{item}"> <md-table-row slot="md-table-row" slot-scope="{item}">
<md-table-cell>
(<a :href="'/diff.html?id=' + id + '&oldid=' + item.id + '&diff=' + lastid">cur</a> | <a :href="'/diff.html?id=' + id + '&diff=' + item.id">prev</a>)
<md-radio name="oldid" v-model="oldid" :value="item.id" v-if="item.seq > 1" :seq="item.seq"></md-radio><span id="placeholder" v-if="item.seq == 1"> ---- </span>
<md-radio name="diff" v-model="diff" :value="item.id" :seq="item.seq"></md-radio>
</md-table-cell>
<md-table-cell md-label="commit ID" md-sort-by="id">${ item.id }</md-table-cell> <md-table-cell md-label="commit ID" md-sort-by="id">${ item.id }</md-table-cell>
<md-table-cell md-label="message" md-sort-by="message">${ item.message }</md-table-cell> <md-table-cell md-label="message" md-sort-by="message">${ item.message }</md-table-cell>
</md-table-row> </md-table-row>
@ -26,6 +31,9 @@ var app = new Vue({
delimiters: ['${', '}'], delimiters: ['${', '}'],
data: { data: {
history: [], history: [],
oldid: null,
diff: null,
lasstid: null,
{% if isinstance(id, str) %} {% if isinstance(id, str) %}
id: "{{id}}", id: "{{id}}",
{% else %} {% else %}
@ -41,6 +49,7 @@ var app = new Vue({
var data = axios.get('/api/history/' + this.id).then(function(response) { var data = axios.get('/api/history/' + this.id).then(function(response) {
console.log(response); console.log(response);
self.history = response.data.history; self.history = response.data.history;
self.lastid = response.data.lastid;
}); });
} }
} }