3 The monitor suffers from various issues - basically event flows while it's running...
7 - monitors for file changes
8 -- adds to QUEUE when occurs.
10 - queue runs in background.
11 - if it's got stuff in it..
13 - now?? - if on master - try and branch
14 == do user selection, until we have branched
15 == then start monitoring again..
25 public class GitMonitor : Monitor
28 public static GitMonitor gitmonitor;
30 * @property {String} the "gitlive" directory, normally ~/gitlive
31 * dset by OWNER... - we should do this as a CTOR.
34 public static string gitlive;
37 private Gee.ArrayList<GitMonitorQueue> queue ;
38 public bool queueRunning = false;
40 public DateTime lastAdd;
47 this.queue = new Gee.ArrayList<GitMonitorQueue>();
48 GitMonitor.gitmonitor = this;
49 Timeout.add_full(Priority.LOW, 500, () => {
51 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
53 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
54 if (this.queue.size < 1 || this.queueRunning) {
58 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
60 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
61 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
63 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
66 //_this.lastAdd = new Date();
75 public new void pauseError(string failure)
78 var notification = new Notify.Notification(
85 notification.set_timeout(60); // show errros for longer
88 Canberra.Context context;
89 Canberra.Proplist props;
91 Canberra.Context.create (out context);
92 Canberra.Proplist.create (out props);
94 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
95 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
98 context.play_full (0, props, null);
104 this.queueRunning = false;
105 // what does this do to the old one...
106 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
108 StatusIconA.statusicon.pauseError();
110 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
111 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
112 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
115 m.set_keep_above(true);
117 m.set_position(Gtk.WindowPosition.CENTER);
118 m.response.connect( (id_pressed) => {
124 public new void pause() {
126 // what does this do to the old one...
127 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
128 StatusIconA.statusicon.pause();
132 public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
134 //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
139 public new void resume () {
141 this.queue = new Array<GitMonitorQueue> ();
142 StatusIconA.statusicon.resume();
148 * Start the monitoring
149 * and run the queue every 500 milliseconds..
152 public new void start()
154 StatusIconA.statusicon.refreshing();
157 this.lastAdd = new DateTime.now(new TimeZone.local());
159 Timeout.add_full(Priority.LOW, 500, () => {
160 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
161 // call this.monitor on each of 'top'
162 for(int i = 0; i < this.top.length ; i++) {
164 this.monitor(this.top.index(i) );
166 StatusIconA.statusicon.resume();
173 var notification = new Notify.Notification(
175 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
179 notification.set_timeout(5);
182 GLib.debug("Error sending notification to screen: %s",e.message);
184 return false; // do not keep doing this..
194 public new void stop() {
195 StatusIconA.statusicon.pause();
200 public override void monitor (string path, int depth = 0)
203 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
204 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
207 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
208 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
214 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
215 return; // skip non-git directories..
220 // FIXME - check if repo is flagged as not autocommit..
221 //var repo = imports.Scm.Repo.Repo.get(path);
222 //if (!repo || !repo.autocommit()) {
228 // check if the repo is to be monitored.
229 //print("PATH : " + path);
232 base.monitor(path, depth);
239 * - pulls the items off the queue
240 * (as commands run concurrently and new items may get added while it's running)
241 * - runs the queue items
245 public void runQueue()
248 if (this.paused || this.queue.size < 1 ) {
252 foreach(var q in this.queue) {
253 if (!q.shouldIgnore() && !q.repo.is_wip_branch()) {
254 var oldq = this.queue;
255 this.queue = new Gee.ArrayList<GitMonitorQueue>();
256 NewBranch.singleton().show(q.repo, oldq);
265 GLib.debug("GitMonitor.runQueue\n");
267 this.queueRunning = true;
269 var cmds = new Gee.ArrayList<GitMonitorQueue>();
271 for(var i = 0; i < this.queue.size; i++) {
272 cmds.add(this.queue.get(i));
275 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
277 string[] success = {};
278 string[] failure = {};
279 //var repos = new Array<GitRepo>(); //??
280 //var done = new Array<GitMonitorQueue>();
282 // first build a array of repo's to work with
283 var repo_list = new Array<GitRepo>();
287 //print(JSON.stringify(cmds));
288 // make sure nothing get's added to the queue where we are doing this..
292 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
293 GLib.debug("GitMonitor.runQueue - creating repos");
295 for(var i = 0; i < cmds.size; i++) {
297 var cmd = cmds.get(i);
299 var gitpath = cmd.gitpath;
301 var repo = GitRepo.get( gitpath );
302 if ( !repo.is_wip_branch()) {
303 leave_queued.add(cmd);
307 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
309 var ix = GitRepo.indexOf(repo_list, gitpath);
311 repo_list.append_val( GitRepo.get( gitpath ));
312 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
314 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
316 //if (typeof(repo_list[gitpath]) == 'undefined') {
317 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
318 // repo_list[gitpath].cmds = [];
319 // repo_list[gitpath].pull();
321 repo_list.index(ix).cmds.add(cmd);
324 this.queue = leave_queued;
327 // build add, remove and commit message list..
329 GLib.debug("GitMonitor.runQueue - creating actions");
331 for(var i = 0;i < repo_list.length;i++) {
333 var repo = repo_list.index(i);
335 var add_files = new Gee.ArrayList<GitMonitorQueue>();
336 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
337 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
338 var messages = new Gee.ArrayList<GitMonitorQueue>();
339 //print(JSON.stringify(repo.cmds,null,4));
341 for(var ii = 0;ii < repo.cmds.size;ii++) {
342 var cmd = repo.cmds.get(ii);
344 if (repo.is_ignore(cmd.vname)) {
352 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
360 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
364 // if file exists, do not try and delete it.
365 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
369 remove_files.add(cmd);
373 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
380 stdout.printf("Opps unmatched action %s\n", cmd.action);
385 repo.cmds.clear(); // reset the repo's command list..
387 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
388 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
391 // these can fail... at present... as we wildcard stuff.
393 // make sure added files do not get removed.. ??
395 var remove_files_f = new Array<GitMonitorQueue>();
396 for(var ii = 0;ii < remove_files.length;ii++) {
397 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
400 remove_files_f.append_val(remove_files.index(ii));
402 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
405 // if file was added, then removed,
406 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
407 for(var ii = 0;ii < remove_files.size;ii++) {
411 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
412 // in add and remove - do not remvove
415 remove_files_f.add(remove_files.get(ii));
417 for(var ii = 0;ii < add_files.size;ii++) {
418 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
419 // the add file is in the remove list, and it does not exist - do not add it..
420 print("check exists ? %s\n",add_files.get(ii).fullpath());
422 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
428 add_files_f.add(add_files.get(ii));
431 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
432 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
434 if (add_files_f.size < 1 && remove_files_f.size < 1) {
438 // make sure monitoring is paused so it does not recursively pick up
447 this.pauseError(e.message);
448 //failure += e.message;
449 //print("Pull failed:\n");
455 repo.add(add_files_f);
457 this.pauseError(e.message);
459 failure += e.message;
460 GLib.debug("Add failed:");
463 repo.remove(remove_files_f);
465 this.pauseError(e.message);
467 failure += e.message;
468 GLib.debug("Remove failed:");
472 success += repo.commit(
473 GitMonitorQueue.messageToString(messages),
476 success += repo.push();
481 // if the error is 'nothing to commit, working tree clean'
482 // then it's not an error, - just continue;
483 if (/nothing to commit, working tree clean/.match(e.message)) {
484 GLib.debug("%s",e.message);
485 success += e.message;
493 this.pauseError(e.message);
496 //failure += e.message;
497 //print("Push failed:\n");
504 // finally merge all the commit messages.
507 // catch notification failures.. so we can carry on..
508 if (success.length > 0) {
511 var notification = new Notify.Notification(
513 string.joinv("\n",success),
518 notification.set_timeout(5);
522 //if (failure.length > 0) {
524 // should never get this far...
525 // this.pauseError();
528 GLib.debug(e.message);
531 this.queueRunning = false;
538 //string[] just_created;
547 public override void onChanged(MonitorNamePathDir src)
549 //print("GitMonitor.onChanged\n");
550 return; // always ignore this..?
551 //this.parsePath(src);
557 * results in git add + git commit..
560 public override void onChangesDoneHint(MonitorNamePathDir src)
566 GLib.debug("GitMonitor.onChangedHint");
568 this.lastAdd = new DateTime.now(new TimeZone.local());
569 var cmd = new GitMonitorQueue(src);
570 if (cmd.shouldIgnore()) {
571 GLib.debug("GitMonitor.onChangedHint - ignored");
575 //var add_it = false;
577 if (this.is_just_created(cmd.path)) {
579 if (typeof(this.just_created[src.path]) !='undefined') {
580 delete this.just_created[src.path];
583 [ src.gitpath, 'add', src.vpath ],
584 [ src.gitpath, 'commit', { message: src.vpath} ]
594 cmd = new GitMonitorQueue(src);
595 cmd.action = "commit";
596 cmd.message = cmd.vname;
601 public override void onDeleted(MonitorNamePathDir src)
607 GLib.debug("GitMonitor.onDeleted");
608 this.lastAdd = new DateTime.now(new TimeZone.local());
609 var cmd = new GitMonitorQueue(src);
610 if (cmd.shouldIgnore()) {
613 // should check if monitor needs removing..
614 // it should also check if it was a directory.. - so we dont have to commit all..
618 cmd = new GitMonitorQueue(src);
619 cmd.action = "commit";
620 cmd.message = cmd.vname;
621 cmd.commit_all = true;
626 public override void onCreated(MonitorNamePathDir src) {
631 GLib.debug("GitMonitor.onCreated");
632 this.lastAdd = new DateTime.now(new TimeZone.local());
633 var cmd = new GitMonitorQueue(src);
634 if (cmd.shouldIgnore()) {
638 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
639 // this.just_created[src.path] = true;
640 return; // we do not handle file create flags... - use done hint.
642 // directory has bee created
643 this.monitor(src.path);
644 //this.top.append_val(src.path);
645 //this.monitor(src.path );
648 // -- no point in adding a dir.. as git does not handle them...
650 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
651 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
657 public override void onAttributeChanged(MonitorNamePathDir src)
663 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
664 if (src.dir == GitMonitor.gitlive) {
665 return; // attribute on top level..
668 this.lastAdd = new DateTime.now(new TimeZone.local());
669 var cmd = new GitMonitorQueue(src);
670 if (cmd.shouldIgnore()) {
676 cmd = new GitMonitorQueue(src);
677 cmd.action = "commit";
678 cmd.message = "Attribute changed " + cmd.vname;
682 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
688 GLib.debug("GitMonitor.onMoved");
689 this.lastAdd = new DateTime.now(new TimeZone.local());
690 var cmd_s = new GitMonitorQueue(src);
692 var cmd_d = new GitMonitorQueue(dest);
695 if (cmd_d.gitpath != cmd_s.gitpath) {
697 this.onCreated(dest);
698 this.onChangesDoneHint(dest);
701 // needs to handle move to/from unsupported types..
703 if (cmd_s.shouldIgnore()) {
704 this.onCreated(dest);
705 this.onChangesDoneHint(dest);
709 if (cmd_d.shouldIgnore()) {
715 GLib.debug("RM: %s", cmd_s.vname);
717 this.queue.add(cmd_s);
721 GLib.debug("ADD: %s", cmd_d.vname);
722 cmd_d.action = "add";
723 this.queue.add(cmd_d);
726 var cmd = new GitMonitorQueue(dest);
727 cmd.action = "commit";
728 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
729 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
730 cmd.message = cmd_d.vname;