2 public class GitMonitor : Monitor
7 * @property {String} the "gitlive" directory, normally ~/gitlive
8 * dset by OWNER... - we should do this as a CTOR.
11 public string gitlive = '';
14 public Array<GtkMonitorQueuequeue>;
15 public bool queueRunning = false;
17 public DateTime lastAdd;
22 // what does this do to the old one...
23 this.queue = new Array<FileMonitor> ();
24 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
28 public void resume () {
30 this.queue = new Array<FileMonitor> ();
31 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
36 * Start the monitoring
37 * and run the queue every 500 milliseconds..
41 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
44 this.lastAdd = new DateTime.now();
46 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
48 // call this.monitor on each of 'top'
49 for(int i = 0; i < this.top.length ; i++) {
50 this.monitor(this.top.index(i), ( fm, f_orig, of_orig, event_type) => {
51 this.onEvent (fm, f_orig, of_orig, event_type ) ;
54 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
59 var notification = new Notify.Notification({
61 this.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
65 notification.set_timeout(5);
73 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
74 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
75 if (!_this.queue.length || _this.queueRunning) {
79 var last = this.lastAdd.difference(new DateTime.now());
82 //print("LAST RUN?" + last);
84 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
87 //_this.lastAdd = new Date();
99 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
104 public void monitor (string path, onEventHander fn , int depth = 0)
107 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
110 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
111 if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
116 // FIXME - check if repo is flagged as not autocommit..
117 //var repo = imports.Scm.Repo.Repo.get(path);
118 //if (!repo || !repo.autocommit()) {
124 // check if the repo is to be monitored.
125 //print("PATH : " + path);
128 base.monitor(path,fn, depth);
135 * - pulls the items off the queue
136 * (as commands run concurrently and new items may get added while it's running)
137 * - runs the queue items
141 public void runQueue()
147 this.queueRunning = true;
149 var cmds = new Array<GitMontitorQueue>();
150 for(var i = 0; i < this.queue.length; i++) {
151 cmds.append_val(this.queue.item(i));
154 this.queue = new Array<GitMontitorQueue>();// empty queue!
157 var success = new Array<GitMontitorQueue>();
158 var failure = new Array<GitMontitorQueue>();
159 var repos = new Array<GitMontitorQueue>(); //??
160 var done = new Array<GitMontitorQueue>();
162 // first build a array of repo's to work with
163 var repo_list = new Array<GitMontitorRepo>();
167 //print(JSON.stringify(cmds));
170 for(var i = 0; i < cmds.length; i++) {
173 var gitpath = cmd.gitpath;
174 var ix = GitMontitorRepo.indexOf(this.repos, cmd.gitpath);
176 repo_list.append_val(new GitMontitorRepo( gitpath ));
177 ix = GitMontitorRepo.indexOf(this.repos, cmd.gitpath);
181 //if (typeof(repo_list[gitpath]) == 'undefined') {
182 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
183 // repo_list[gitpath].cmds = [];
184 // repo_list[gitpath].pull();
186 repo_list.item(ix).append_val(cmd);
190 // build add, remove and commit message list..
192 for(var i = 0;i < repo_list.length;i++) {
195 var repo = repo_list.item(i);
197 var add_files = new Array<GitMontitorQueue>();
198 var remove_files = new Array<GitMontitorQueue>();
199 var messages = new Array<GitMontitorQueue>();
200 //print(JSON.stringify(repo.cmds,null,4));
202 for(var ii = 0;ii < repo.length;ii++) {
203 var cmd = repo.item(i);
205 var name = cmd.shift();
206 var arg = cmd.shift();
211 if (GitMontitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
216 add_files.append_val(cmd);
220 if (GitMontitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
224 // if file exists, do not try and delete it.
225 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
229 remove_files.append_val(cmd);
233 if (GitMontitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
237 messages.append_val(cmd);
244 // these can fail... at present... as we wildcard stuff.
245 print("ADD : " + JSON.stringify(add_files));
247 // make sure added files do not get removed..
248 remove_files = remove_files.filter(function(v) {
249 return add_files.indexOf(v) < 0;
251 print("REMOVE : " + JSON.stringify(remove_files));
254 // make sure monitoring is paused so it does not recursively pick up
261 repo.remove(remove_files);
266 success.push(repo.commit({
267 reason : messages.join("\n"),
270 success.push(repo.push());
273 failure.push(e.message);
278 // finally merge all the commit messages.
281 // catch notification failures.. so we can carry on..
282 if (success.length) {
283 print(success.join("\n"));
285 var notification = new Notify.Notification({
286 summary: "Git Live Commited",
287 body : success.join("\n"),
292 notification.set_timeout(5);
296 if (failure.length) {
298 var notification = new Notify.Notification({
299 summary: "Git Live ERROR!!",
300 body : failure.join("\n"),
305 notification.set_timeout(5); // show errros for longer
312 this.queueRunning = false;