2012-07-02 18:02:34 +00:00
|
|
|
/*
|
|
|
|
* This file is part of gitg
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - Jesse van den Kieboom
|
|
|
|
*
|
|
|
|
* gitg is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* gitg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with gitg. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
namespace Gitg
|
|
|
|
{
|
|
|
|
|
|
|
|
public class CommitModel : Object
|
|
|
|
{
|
|
|
|
private Repository d_repository;
|
|
|
|
private Cancellable? d_cancellable;
|
|
|
|
private Gitg.Commit[] d_ids;
|
2012-07-06 16:47:42 +00:00
|
|
|
private Thread<void*>? d_thread;
|
2012-04-14 11:46:39 +00:00
|
|
|
private Ggit.RevisionWalker? d_walker;
|
|
|
|
private uint d_advertized_size;
|
|
|
|
private uint d_idleid;
|
2012-04-22 13:11:21 +00:00
|
|
|
private Lanes d_lanes;
|
2012-07-19 08:27:38 +00:00
|
|
|
private Ggit.SortMode d_sortmode;
|
2012-04-14 11:46:39 +00:00
|
|
|
|
|
|
|
public uint limit { get; set; }
|
|
|
|
|
2012-07-19 08:27:38 +00:00
|
|
|
public Ggit.SortMode sort_mode
|
|
|
|
{
|
|
|
|
get { return d_sortmode; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (d_sortmode != value)
|
|
|
|
{
|
|
|
|
d_sortmode = value;
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-15 12:55:57 +00:00
|
|
|
private Ggit.OId[] d_include;
|
|
|
|
private Ggit.OId[] d_exclude;
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
public signal void started();
|
|
|
|
public signal void update(uint added);
|
|
|
|
public signal void finished();
|
|
|
|
|
2012-10-30 10:05:51 +00:00
|
|
|
[Notify]
|
2012-04-15 12:55:33 +00:00
|
|
|
public Repository repository
|
|
|
|
{
|
|
|
|
get { return d_repository; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
|
|
|
|
d_walker = null;
|
|
|
|
d_repository = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
public CommitModel(Repository repository)
|
|
|
|
{
|
|
|
|
d_repository = repository;
|
|
|
|
}
|
|
|
|
|
2012-04-22 13:11:21 +00:00
|
|
|
construct
|
|
|
|
{
|
|
|
|
d_lanes = new Lanes();
|
2012-07-06 16:48:38 +00:00
|
|
|
d_cancellable = new Cancellable();
|
|
|
|
d_cancellable.cancel();
|
2012-07-19 08:27:38 +00:00
|
|
|
|
|
|
|
d_sortmode = Ggit.SortMode.TIME | Ggit.SortMode.TOPOLOGICAL;
|
2012-04-22 13:11:21 +00:00
|
|
|
}
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
~CommitModel()
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
2012-04-15 12:55:57 +00:00
|
|
|
public void set_include(Ggit.OId[] ids)
|
|
|
|
{
|
|
|
|
d_include = ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void set_exclude(Ggit.OId[] ids)
|
|
|
|
{
|
|
|
|
d_exclude = ids;
|
|
|
|
}
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
private void cancel()
|
|
|
|
{
|
2012-07-06 16:48:38 +00:00
|
|
|
if (!d_cancellable.is_cancelled())
|
2012-04-14 11:46:39 +00:00
|
|
|
{
|
2012-07-06 16:48:38 +00:00
|
|
|
d_cancellable.cancel();
|
2012-04-14 11:46:39 +00:00
|
|
|
|
2012-07-06 16:48:38 +00:00
|
|
|
d_thread.join();
|
|
|
|
d_thread = null;
|
|
|
|
}
|
2012-04-14 11:46:39 +00:00
|
|
|
|
|
|
|
if (d_idleid != 0)
|
|
|
|
{
|
|
|
|
Source.remove(d_idleid);
|
|
|
|
d_idleid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
d_ids = new Gitg.Commit[0];
|
|
|
|
d_advertized_size = 0;
|
|
|
|
|
2012-04-15 12:56:59 +00:00
|
|
|
emit_started();
|
|
|
|
emit_finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void emit_started()
|
|
|
|
{
|
2012-04-22 13:11:21 +00:00
|
|
|
d_lanes.reset();
|
2012-04-14 11:46:39 +00:00
|
|
|
started();
|
2012-04-15 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void emit_finished()
|
|
|
|
{
|
2012-04-14 11:46:39 +00:00
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2012-04-15 12:56:59 +00:00
|
|
|
protected virtual void emit_update(uint added)
|
|
|
|
{
|
|
|
|
update(added);
|
|
|
|
}
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
public void reload()
|
|
|
|
{
|
|
|
|
cancel();
|
2012-04-15 12:55:10 +00:00
|
|
|
|
2012-07-19 08:27:38 +00:00
|
|
|
if (d_include.length == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-15 12:55:10 +00:00
|
|
|
walk.begin((obj, res) => {
|
|
|
|
walk.end(res);
|
2012-07-06 16:48:38 +00:00
|
|
|
|
|
|
|
d_cancellable.cancel();
|
2012-07-07 09:58:19 +00:00
|
|
|
if (d_thread != null)
|
|
|
|
{
|
|
|
|
d_thread.join();
|
|
|
|
d_thread = null;
|
|
|
|
}
|
2012-04-15 12:55:10 +00:00
|
|
|
});
|
2012-04-14 11:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public uint size()
|
|
|
|
{
|
|
|
|
return d_advertized_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public new Gitg.Commit? @get(uint idx)
|
|
|
|
{
|
|
|
|
Gitg.Commit? ret;
|
|
|
|
|
|
|
|
if (idx >= d_advertized_size)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock(d_ids)
|
|
|
|
{
|
|
|
|
ret = d_ids[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notify_batch(bool isend)
|
|
|
|
{
|
|
|
|
lock(d_idleid)
|
|
|
|
{
|
|
|
|
if (d_idleid != 0)
|
|
|
|
{
|
|
|
|
Source.remove(d_idleid);
|
|
|
|
d_idleid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint newsize = d_ids.length;
|
|
|
|
|
|
|
|
d_idleid = Idle.add(() => {
|
|
|
|
lock(d_idleid)
|
|
|
|
{
|
|
|
|
if (d_idleid == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
d_idleid = 0;
|
|
|
|
|
|
|
|
uint added = newsize - d_advertized_size;
|
|
|
|
d_advertized_size = newsize;
|
|
|
|
|
2012-04-15 12:56:59 +00:00
|
|
|
emit_update(added);
|
2012-04-14 11:46:39 +00:00
|
|
|
|
|
|
|
if (isend)
|
|
|
|
{
|
2012-04-15 12:56:59 +00:00
|
|
|
emit_finished();
|
2012-04-14 11:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-15 12:55:10 +00:00
|
|
|
private async void walk()
|
2012-04-14 11:46:39 +00:00
|
|
|
{
|
2012-04-15 12:55:57 +00:00
|
|
|
Ggit.OId[] included = d_include;
|
|
|
|
Ggit.OId[] excluded = d_exclude;
|
2012-04-14 11:46:39 +00:00
|
|
|
|
|
|
|
uint limit = this.limit;
|
|
|
|
|
2012-04-15 12:55:10 +00:00
|
|
|
SourceFunc cb = walk.callback;
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
ThreadFunc<void*> run = () => {
|
|
|
|
if (d_walker == null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
d_walker = new Ggit.RevisionWalker(d_repository);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
notify_batch(true);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d_walker.reset();
|
2012-07-19 08:27:38 +00:00
|
|
|
d_walker.set_sort_mode(d_sortmode);
|
2012-04-14 11:46:39 +00:00
|
|
|
|
|
|
|
foreach (Ggit.OId oid in included)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
d_walker.push(oid);
|
|
|
|
} catch {};
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Ggit.OId oid in excluded)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
d_walker.hide(oid);
|
|
|
|
} catch {};
|
|
|
|
}
|
|
|
|
|
|
|
|
uint size;
|
|
|
|
|
|
|
|
// Pre-allocate array to store commits
|
|
|
|
lock(d_ids)
|
|
|
|
{
|
|
|
|
d_ids = new Gitg.Commit[1000];
|
|
|
|
|
|
|
|
size = d_ids.length;
|
|
|
|
|
|
|
|
d_ids.length = 0;
|
|
|
|
d_advertized_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Ggit.OId? id;
|
|
|
|
Gitg.Commit? commit;
|
|
|
|
|
|
|
|
if (d_cancellable.is_cancelled())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
id = d_walker.next();
|
2012-07-06 16:49:40 +00:00
|
|
|
|
2012-05-28 20:44:30 +00:00
|
|
|
if (id == null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
commit = d_repository.lookup(id, typeof(Gitg.Commit)) as Gitg.Commit;
|
|
|
|
} catch { break; }
|
|
|
|
|
|
|
|
// Add the id
|
|
|
|
if (d_ids.length == size)
|
|
|
|
{
|
|
|
|
lock(d_ids)
|
|
|
|
{
|
2012-04-24 22:16:26 +00:00
|
|
|
var oldlen = d_ids.length;
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
size *= 2;
|
|
|
|
|
|
|
|
d_ids.resize((int)size);
|
2012-04-24 22:16:26 +00:00
|
|
|
d_ids.length = oldlen;
|
2012-04-14 11:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d_ids += commit;
|
|
|
|
|
2012-04-22 13:11:21 +00:00
|
|
|
int mylane;
|
|
|
|
var lanes = d_lanes.next(commit, out mylane);
|
|
|
|
|
|
|
|
commit.update_lanes((owned)lanes, mylane);
|
|
|
|
|
2012-04-14 11:46:39 +00:00
|
|
|
if (timer.elapsed() >= 200)
|
|
|
|
{
|
|
|
|
notify_batch(false);
|
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit > 0 && d_ids.length == limit)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_batch(true);
|
2012-04-15 12:55:10 +00:00
|
|
|
|
2012-04-22 16:41:39 +00:00
|
|
|
Idle.add((owned)cb);
|
2012-04-14 11:46:39 +00:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-07-06 16:48:38 +00:00
|
|
|
d_cancellable.reset();
|
2012-07-18 07:35:53 +00:00
|
|
|
emit_started();
|
2012-07-06 16:47:42 +00:00
|
|
|
d_thread = new Thread<void*>.try("gitg-history-walk", run);
|
2012-04-15 12:55:10 +00:00
|
|
|
yield;
|
2012-04-14 11:46:39 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2012-04-15 12:56:59 +00:00
|
|
|
emit_finished();
|
2012-07-06 16:48:38 +00:00
|
|
|
|
|
|
|
d_cancellable.cancel();
|
|
|
|
d_thread = null;
|
2012-04-14 11:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ex:set ts=4 noet
|