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) {
57 var first = this.queue.get(0);
59 if (!first.repo.is_wip_branch() && first.repo.is_auto_branch()) {
65 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
67 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
68 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
70 if (delay && last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
73 //_this.lastAdd = new Date();
82 public new void pauseError(string failure)
85 var notification = new Notify.Notification(
92 notification.set_timeout(60); // show errros for longer
95 Canberra.Context context;
96 Canberra.Proplist props;
98 Canberra.Context.create (out context);
99 Canberra.Proplist.create (out props);
101 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
102 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
105 context.play_full (0, props, null);
111 this.queueRunning = false;
112 // what does this do to the old one...
113 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
115 StatusIconA.statusicon.pauseError();
117 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
118 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
119 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
122 m.set_keep_above(true);
124 m.set_position(Gtk.WindowPosition.CENTER);
125 m.response.connect( (id_pressed) => {
131 public new void pause() {
133 // what does this do to the old one...
134 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
135 StatusIconA.statusicon.pause();
139 public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
141 //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
146 public new void resume () {
148 this.queue = new Array<GitMonitorQueue> ();
149 StatusIconA.statusicon.resume();
155 * Start the monitoring
156 * and run the queue every 500 milliseconds..
159 public new void start()
161 StatusIconA.statusicon.refreshing();
164 this.lastAdd = new DateTime.now(new TimeZone.local());
166 Timeout.add_full(Priority.LOW, 500, () => {
167 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
168 // call this.monitor on each of 'top'
169 for(int i = 0; i < this.top.length ; i++) {
171 this.monitor(this.top.index(i) );
173 StatusIconA.statusicon.resume();
180 var notification = new Notify.Notification(
182 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
186 notification.set_timeout(5);
189 GLib.debug("Error sending notification to screen: %s",e.message);
191 return false; // do not keep doing this..
201 public new void stop() {
202 StatusIconA.statusicon.pause();
207 public override void monitor (string path, int depth = 0)
210 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
211 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
214 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
215 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
221 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
222 return; // skip non-git directories..
227 // FIXME - check if repo is flagged as not autocommit..
228 //var repo = imports.Scm.Repo.Repo.get(path);
229 //if (!repo || !repo.autocommit()) {
235 // check if the repo is to be monitored.
236 //print("PATH : " + path);
239 base.monitor(path, depth);
246 * - pulls the items off the queue
247 * (as commands run concurrently and new items may get added while it's running)
248 * - runs the queue items
252 public void runQueue()
255 if (this.paused || this.queue.size < 1 ) {
259 foreach(var q in this.queue) {
260 if (!q.shouldIgnore() && !q.repo.is_wip_branch() && q.repo.is_auto_branch()) {
261 var oldq = this.queue;
262 this.queue = new Gee.ArrayList<GitMonitorQueue>();
263 NewBranch.singleton().show(q.repo, oldq);
272 GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
274 this.queueRunning = true;
276 var cmds = new Gee.ArrayList<GitMonitorQueue>();
278 for(var i = 0; i < this.queue.size; i++) {
279 cmds.add(this.queue.get(i));
282 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
284 string[] success = {};
285 string[] failure = {};
286 //var repos = new Array<GitRepo>(); //??
287 //var done = new Array<GitMonitorQueue>();
289 // first build a array of repo's to work with
290 var repo_list = new Array<GitRepo>();
294 //print(JSON.stringify(cmds));
295 // make sure nothing get's added to the queue where we are doing this..
299 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
300 GLib.debug("GitMonitor.runQueue - creating repos");
302 for(var i = 0; i < cmds.size; i++) {
304 var cmd = cmds.get(i);
306 var gitpath = cmd.gitpath;
308 var repo = GitRepo.get( gitpath );
309 if ( !repo.is_wip_branch() && repo.is_auto_branch()) {
310 leave_queued.add(cmd);
314 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
316 var ix = GitRepo.indexOf(repo_list, gitpath);
318 repo_list.append_val( GitRepo.get( gitpath ));
319 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
321 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
323 //if (typeof(repo_list[gitpath]) == 'undefined') {
324 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
325 // repo_list[gitpath].cmds = [];
326 // repo_list[gitpath].pull();
328 repo_list.index(ix).cmds.add(cmd);
331 this.queue = leave_queued;
334 // build add, remove and commit message list..
336 GLib.debug("GitMonitor.runQueue - creating actions");
338 for(var i = 0;i < repo_list.length;i++) {
340 var repo = repo_list.index(i);
342 var add_files = new Gee.ArrayList<GitMonitorQueue>();
343 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
344 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
345 var messages = new Gee.ArrayList<GitMonitorQueue>();
346 //print(JSON.stringify(repo.cmds,null,4));
348 for(var ii = 0;ii < repo.cmds.size;ii++) {
349 var cmd = repo.cmds.get(ii);
351 if (repo.is_ignore(cmd.vname)) {
359 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
367 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
371 // if file exists, do not try and delete it.
372 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
376 remove_files.add(cmd);
380 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
387 stdout.printf("Opps unmatched action %s\n", cmd.action);
392 repo.cmds.clear(); // reset the repo's command list..
394 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
395 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
398 // these can fail... at present... as we wildcard stuff.
400 // make sure added files do not get removed.. ??
402 var remove_files_f = new Array<GitMonitorQueue>();
403 for(var ii = 0;ii < remove_files.length;ii++) {
404 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
407 remove_files_f.append_val(remove_files.index(ii));
409 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
412 // if file was added, then removed,
413 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
414 for(var ii = 0;ii < remove_files.size;ii++) {
418 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
419 // in add and remove - do not remvove
422 remove_files_f.add(remove_files.get(ii));
424 for(var ii = 0;ii < add_files.size;ii++) {
425 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
426 // the add file is in the remove list, and it does not exist - do not add it..
427 print("check exists ? %s\n",add_files.get(ii).fullpath());
429 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
435 add_files_f.add(add_files.get(ii));
438 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
439 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
441 if (add_files_f.size < 1 && remove_files_f.size < 1) {
445 // make sure monitoring is paused so it does not recursively pick up
454 this.pauseError(e.message);
455 //failure += e.message;
456 //print("Pull failed:\n");
462 repo.add(add_files_f);
464 this.pauseError(e.message);
466 failure += e.message;
467 GLib.debug("Add failed:");
470 repo.remove(remove_files_f);
472 this.pauseError(e.message);
474 failure += e.message;
475 GLib.debug("Remove failed:");
479 success += repo.commit(
480 GitMonitorQueue.messageToString(messages),
483 success += repo.push();
488 // if the error is 'nothing to commit, working tree clean'
489 // then it's not an error, - just continue;
490 if (/nothing to commit, working tree clean/.match(e.message)) {
491 GLib.debug("%s",e.message);
492 success += e.message;
500 this.pauseError(e.message);
503 //failure += e.message;
504 //print("Push failed:\n");
511 // finally merge all the commit messages.
514 // catch notification failures.. so we can carry on..
515 if (success.length > 0) {
518 var notification = new Notify.Notification(
520 string.joinv("\n",success),
525 notification.set_timeout(5);
529 //if (failure.length > 0) {
531 // should never get this far...
532 // this.pauseError();
535 GLib.debug(e.message);
538 this.queueRunning = false;
545 //string[] just_created;
554 public override void onChanged(MonitorNamePathDir src)
556 //print("GitMonitor.onChanged\n");
557 return; // always ignore this..?
558 //this.parsePath(src);
564 * results in git add + git commit..
567 public override void onChangesDoneHint(MonitorNamePathDir src)
573 GLib.debug("GitMonitor.onChangedHint");
575 this.lastAdd = new DateTime.now(new TimeZone.local());
576 var cmd = new GitMonitorQueue(src);
577 if (cmd.shouldIgnore()) {
578 GLib.debug("GitMonitor.onChangedHint - ignored");
582 //var add_it = false;
584 if (this.is_just_created(cmd.path)) {
586 if (typeof(this.just_created[src.path]) !='undefined') {
587 delete this.just_created[src.path];
590 [ src.gitpath, 'add', src.vpath ],
591 [ src.gitpath, 'commit', { message: src.vpath} ]
601 cmd = new GitMonitorQueue(src);
602 cmd.action = "commit";
603 cmd.message = cmd.vname;
608 public override void onDeleted(MonitorNamePathDir src)
614 GLib.debug("GitMonitor.onDeleted");
615 this.lastAdd = new DateTime.now(new TimeZone.local());
616 var cmd = new GitMonitorQueue(src);
617 if (cmd.shouldIgnore()) {
620 // should check if monitor needs removing..
621 // it should also check if it was a directory.. - so we dont have to commit all..
625 cmd = new GitMonitorQueue(src);
626 cmd.action = "commit";
627 cmd.message = cmd.vname;
628 cmd.commit_all = true;
633 public override void onCreated(MonitorNamePathDir src) {
638 GLib.debug("GitMonitor.onCreated");
639 this.lastAdd = new DateTime.now(new TimeZone.local());
640 var cmd = new GitMonitorQueue(src);
641 if (cmd.shouldIgnore()) {
645 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
646 // this.just_created[src.path] = true;
647 return; // we do not handle file create flags... - use done hint.
649 // directory has bee created
650 this.monitor(src.path);
651 //this.top.append_val(src.path);
652 //this.monitor(src.path );
655 // -- no point in adding a dir.. as git does not handle them...
657 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
658 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
664 public override void onAttributeChanged(MonitorNamePathDir src)
670 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
671 if (src.dir == GitMonitor.gitlive) {
672 return; // attribute on top level..
675 this.lastAdd = new DateTime.now(new TimeZone.local());
676 var cmd = new GitMonitorQueue(src);
677 if (cmd.shouldIgnore()) {
683 cmd = new GitMonitorQueue(src);
684 cmd.action = "commit";
685 cmd.message = "Attribute changed " + cmd.vname;
689 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
695 GLib.debug("GitMonitor.onMoved");
696 this.lastAdd = new DateTime.now(new TimeZone.local());
697 var cmd_s = new GitMonitorQueue(src);
699 var cmd_d = new GitMonitorQueue(dest);
702 if (cmd_d.gitpath != cmd_s.gitpath) {
704 this.onCreated(dest);
705 this.onChangesDoneHint(dest);
708 // needs to handle move to/from unsupported types..
710 if (cmd_s.shouldIgnore()) {
711 this.onCreated(dest);
712 this.onChangesDoneHint(dest);
716 if (cmd_d.shouldIgnore()) {
722 GLib.debug("RM: %s", cmd_s.vname);
724 this.queue.add(cmd_s);
728 GLib.debug("ADD: %s", cmd_d.vname);
729 cmd_d.action = "add";
730 this.queue.add(cmd_d);
733 var cmd = new GitMonitorQueue(dest);
734 cmd.action = "commit";
735 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
736 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
737 cmd.message = cmd_d.vname;