7 public class GitMonitor : Monitor
10 public static GitMonitor gitmonitor;
12 * @property {String} the "gitlive" directory, normally ~/gitlive
13 * dset by OWNER... - we should do this as a CTOR.
16 public static string gitlive;
19 private Gee.ArrayList<GitMonitorQueue> queue ;
20 public bool queueRunning = false;
22 public DateTime lastAdd;
29 this.queue = new Gee.ArrayList<GitMonitorQueue>();
30 GitMonitor.gitmonitor = this;
31 Timeout.add_full(Priority.LOW, 500, () => {
33 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
35 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
36 if (this.queue.size < 1 || this.queueRunning) {
40 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
42 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
43 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
45 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
48 //_this.lastAdd = new Date();
57 public new void pauseError(string failure)
60 var notification = new Notify.Notification(
67 notification.set_timeout(60); // show errros for longer
70 Canberra.Context context;
71 Canberra.Proplist props;
73 Canberra.Context.create (out context);
74 Canberra.Proplist.create (out props);
76 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
77 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
80 context.play_full (0, props, null);
86 this.queueRunning = false;
87 // what does this do to the old one...
88 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
90 StatusIconA.statusicon.pauseError();
92 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
93 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
94 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
97 m.set_keep_above(true);
99 m.set_position(Gtk.WindowPosition.CENTER);
100 m.response.connect( (id_pressed) => {
106 public new void pause() {
108 // what does this do to the old one...
109 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
110 StatusIconA.statusicon.pause();
115 public new void resume () {
117 this.queue = new Array<GitMonitorQueue> ();
118 StatusIconA.statusicon.resume();
124 * Start the monitoring
125 * and run the queue every 500 milliseconds..
128 public new void start()
130 StatusIconA.statusicon.refreshing();
133 this.lastAdd = new DateTime.now(new TimeZone.local());
135 Timeout.add_full(Priority.LOW, 500, () => {
136 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
137 // call this.monitor on each of 'top'
138 for(int i = 0; i < this.top.length ; i++) {
140 this.monitor(this.top.index(i) );
142 StatusIconA.statusicon.resume();
149 var notification = new Notify.Notification(
151 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
155 notification.set_timeout(5);
158 GLib.debug("Error sending notification to screen: %s",e.message);
160 return false; // do not keep doing this..
170 public new void stop() {
171 StatusIconA.statusicon.pause();
176 public override void monitor (string path, int depth = 0)
179 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
180 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
183 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
184 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
190 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
191 return; // skip non-git directories..
196 // FIXME - check if repo is flagged as not autocommit..
197 //var repo = imports.Scm.Repo.Repo.get(path);
198 //if (!repo || !repo.autocommit()) {
204 // check if the repo is to be monitored.
205 //print("PATH : " + path);
208 base.monitor(path, depth);
215 * - pulls the items off the queue
216 * (as commands run concurrently and new items may get added while it's running)
217 * - runs the queue items
221 public void runQueue()
224 if (this.paused || this.queue.size < 1 ) {
227 GLib.debug("GitMonitor.runQueue\n");
229 this.queueRunning = true;
231 var cmds = new Gee.ArrayList<GitMonitorQueue>();
233 for(var i = 0; i < this.queue.size; i++) {
234 cmds.add(this.queue.get(i));
237 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
239 string[] success = {};
240 string[] failure = {};
241 //var repos = new Array<GitRepo>(); //??
242 //var done = new Array<GitMonitorQueue>();
244 // first build a array of repo's to work with
245 var repo_list = new Array<GitRepo>();
249 //print(JSON.stringify(cmds));
250 // make sure nothing get's added to the queue where we are doing this..
254 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
255 GLib.debug("GitMonitor.runQueue - creating repos");
257 for(var i = 0; i < cmds.size; i++) {
259 var cmd = cmds.get(i);
261 var gitpath = cmd.gitpath;
263 var repo = GitRepo.get( gitpath );
264 if ( !repo.is_wip_branch()) {
265 leave_queued.add(cmd);
269 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
271 var ix = GitRepo.indexOf(repo_list, gitpath);
273 repo_list.append_val( GitRepo.get( gitpath ));
274 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
276 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
278 //if (typeof(repo_list[gitpath]) == 'undefined') {
279 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
280 // repo_list[gitpath].cmds = [];
281 // repo_list[gitpath].pull();
283 repo_list.index(ix).cmds.add(cmd);
286 this.queue = leave_queued;
289 // build add, remove and commit message list..
291 GLib.debug("GitMonitor.runQueue - creating actions");
293 for(var i = 0;i < repo_list.length;i++) {
295 var repo = repo_list.index(i);
297 var add_files = new Gee.ArrayList<GitMonitorQueue>();
298 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
299 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
300 var messages = new Gee.ArrayList<GitMonitorQueue>();
301 //print(JSON.stringify(repo.cmds,null,4));
303 for(var ii = 0;ii < repo.cmds.size;ii++) {
304 var cmd = repo.cmds.get(ii);
306 if (repo.is_ignore(cmd.vname)) {
314 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
322 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
326 // if file exists, do not try and delete it.
327 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
331 remove_files.add(cmd);
335 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
342 stdout.printf("Opps unmatched action %s\n", cmd.action);
347 repo.cmds.clear(); // reset the repo's command list..
349 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
350 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
353 // these can fail... at present... as we wildcard stuff.
355 // make sure added files do not get removed.. ??
357 var remove_files_f = new Array<GitMonitorQueue>();
358 for(var ii = 0;ii < remove_files.length;ii++) {
359 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
362 remove_files_f.append_val(remove_files.index(ii));
364 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
367 // if file was added, then removed,
368 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
369 for(var ii = 0;ii < remove_files.size;ii++) {
373 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
374 // in add and remove - do not remvove
377 remove_files_f.add(remove_files.get(ii));
379 for(var ii = 0;ii < add_files.size;ii++) {
380 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
381 // the add file is in the remove list, and it does not exist - do not add it..
382 print("check exists ? %s\n",add_files.get(ii).fullpath());
384 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
390 add_files_f.add(add_files.get(ii));
393 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
394 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
396 if (add_files_f.size < 1 && remove_files_f.size < 1) {
400 // make sure monitoring is paused so it does not recursively pick up
409 this.pauseError(e.message);
410 //failure += e.message;
411 //print("Pull failed:\n");
417 repo.add(add_files_f);
419 this.pauseError(e.message);
421 failure += e.message;
422 GLib.debug("Add failed:");
425 repo.remove(remove_files_f);
427 this.pauseError(e.message);
429 failure += e.message;
430 GLib.debug("Remove failed:");
434 success += repo.commit(
435 GitMonitorQueue.messageToString(messages),
438 success += repo.push();
443 // if the error is 'nothing to commit, working tree clean'
444 // then it's not an error, - just continue;
445 if (/nothing to commit, working tree clean/.match(e.message)) {
446 GLib.debug("%s",e.message);
447 success += e.message;
455 this.pauseError(e.message);
458 //failure += e.message;
459 //print("Push failed:\n");
466 // finally merge all the commit messages.
469 // catch notification failures.. so we can carry on..
470 if (success.length > 0) {
473 var notification = new Notify.Notification(
475 string.joinv("\n",success),
480 notification.set_timeout(5);
484 //if (failure.length > 0) {
486 // should never get this far...
487 // this.pauseError();
490 GLib.debug(e.message);
493 this.queueRunning = false;
500 //string[] just_created;
509 public override void onChanged(MonitorNamePathDir src)
511 //print("GitMonitor.onChanged\n");
512 return; // always ignore this..?
513 //this.parsePath(src);
519 * results in git add + git commit..
522 public override void onChangesDoneHint(MonitorNamePathDir src)
528 GLib.debug("GitMonitor.onChangedHint");
530 this.lastAdd = new DateTime.now(new TimeZone.local());
531 var cmd = new GitMonitorQueue(src);
532 if (cmd.shouldIgnore()) {
533 GLib.debug("GitMonitor.onChangedHint - ignored");
537 //var add_it = false;
539 if (this.is_just_created(cmd.path)) {
541 if (typeof(this.just_created[src.path]) !='undefined') {
542 delete this.just_created[src.path];
545 [ src.gitpath, 'add', src.vpath ],
546 [ src.gitpath, 'commit', { message: src.vpath} ]
556 cmd = new GitMonitorQueue(src);
557 cmd.action = "commit";
558 cmd.message = cmd.vname;
562 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
563 NewBranch.singleton().show(cmd.repo);
568 public override void onDeleted(MonitorNamePathDir src)
574 GLib.debug("GitMonitor.onDeleted");
575 this.lastAdd = new DateTime.now(new TimeZone.local());
576 var cmd = new GitMonitorQueue(src);
577 if (cmd.shouldIgnore()) {
580 // should check if monitor needs removing..
581 // it should also check if it was a directory.. - so we dont have to commit all..
585 cmd = new GitMonitorQueue(src);
586 cmd.action = "commit";
587 cmd.message = cmd.vname;
588 cmd.commit_all = true;
592 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
593 NewBranch.singleton().show(cmd.repo);
598 public override void onCreated(MonitorNamePathDir src) {
603 GLib.debug("GitMonitor.onCreated");
604 this.lastAdd = new DateTime.now(new TimeZone.local());
605 var cmd = new GitMonitorQueue(src);
606 if (cmd.shouldIgnore()) {
610 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
611 // this.just_created[src.path] = true;
612 return; // we do not handle file create flags... - use done hint.
614 // directory has bee created
615 this.monitor(src.path);
616 //this.top.append_val(src.path);
617 //this.monitor(src.path );
620 // -- no point in adding a dir.. as git does not handle them...
622 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
623 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
629 public override void onAttributeChanged(MonitorNamePathDir src)
635 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
636 if (src.dir == GitMonitor.gitlive) {
637 return; // attribute on top level..
640 this.lastAdd = new DateTime.now(new TimeZone.local());
641 var cmd = new GitMonitorQueue(src);
642 if (cmd.shouldIgnore()) {
648 cmd = new GitMonitorQueue(src);
649 cmd.action = "commit";
650 cmd.message = "Attribute changed " + cmd.vname;
653 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
654 NewBranch.singleton().show(cmd.repo);
658 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
664 GLib.debug("GitMonitor.onMoved");
665 this.lastAdd = new DateTime.now(new TimeZone.local());
666 var cmd_s = new GitMonitorQueue(src);
668 var cmd_d = new GitMonitorQueue(dest);
671 if (cmd_d.gitpath != cmd_s.gitpath) {
673 this.onCreated(dest);
674 this.onChangesDoneHint(dest);
677 // needs to handle move to/from unsupported types..
679 if (cmd_s.shouldIgnore()) {
680 this.onCreated(dest);
681 this.onChangesDoneHint(dest);
685 if (cmd_d.shouldIgnore()) {
694 GLib.debug("RM: %s", cmd_s.vname);
696 this.queue.add(cmd_s);
700 GLib.debug("ADD: %s", cmd_d.vname);
701 cmd_d.action = "add";
702 this.queue.add(cmd_d);
705 var cmd = new GitMonitorQueue(dest);
706 cmd.action = "commit";
707 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
708 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
709 cmd.message = cmd_d.vname;
715 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
716 NewBranch.singleton().show(cmd.repo);
718 // this is where it get's complicated...
719 // as we might be moving across repo's...