4 public class GitMonitor : Monitor
7 public static GitMonitor gitmonitor;
9 * @property {String} the "gitlive" directory, normally ~/gitlive
10 * dset by OWNER... - we should do this as a CTOR.
13 public static string gitlive;
16 public Array<GitMonitorQueue> queue ;
17 public bool queueRunning = false;
19 public DateTime lastAdd;
26 this.queue = new Array<GitMonitorQueue>();
27 GitMonitor.gitmonitor = this;
28 Timeout.add_full(Priority.LOW, 500, () => {
30 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
32 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
33 if (this.queue.length < 1 || this.queueRunning) {
37 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
39 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
40 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
42 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
45 //_this.lastAdd = new Date();
54 public new void pauseError(string failure)
57 var notification = new Notify.Notification(
64 notification.set_timeout(60); // show errros for longer
67 Canberra.Context context;
68 Canberra.Proplist props;
70 Canberra.Context.create (out context);
71 Canberra.Proplist.create (out props);
73 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
74 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
77 context.play_full (0, props, null);
83 this.queueRunning = false;
84 // what does this do to the old one...
85 this.queue = new Array<GitMonitorQueue> ();
87 StatusIconA.statusicon.pauseError();
89 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
90 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
91 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
94 m.set_keep_above(true);
96 m.set_position(Gtk.WindowPosition.CENTER);
97 m.response.connect( (id_pressed) => {
103 public new void pause() {
105 // what does this do to the old one...
106 this.queue = new Array<GitMonitorQueue> ();
107 StatusIconA.statusicon.pause();
112 public new void resume () {
114 this.queue = new Array<GitMonitorQueue> ();
115 StatusIconA.statusicon.resume();
121 * Start the monitoring
122 * and run the queue every 500 milliseconds..
125 public new void start()
127 StatusIconA.statusicon.refreshing();
130 this.lastAdd = new DateTime.now(new TimeZone.local());
132 Timeout.add_full(Priority.LOW, 500, () => {
133 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
134 // call this.monitor on each of 'top'
135 for(int i = 0; i < this.top.length ; i++) {
137 this.monitor(this.top.index(i) );
139 StatusIconA.statusicon.resume();
146 var notification = new Notify.Notification(
148 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
152 notification.set_timeout(5);
155 GLib.debug("Error sending notification to screen: %s",e.message);
157 return false; // do not keep doing this..
167 public new void stop() {
168 StatusIconA.statusicon.pause();
173 public new void monitor (string path, int depth = 0)
176 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
179 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
180 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
185 // FIXME - check if repo is flagged as not autocommit..
186 //var repo = imports.Scm.Repo.Repo.get(path);
187 //if (!repo || !repo.autocommit()) {
193 // check if the repo is to be monitored.
194 //print("PATH : " + path);
197 base.monitor(path, depth);
204 * - pulls the items off the queue
205 * (as commands run concurrently and new items may get added while it's running)
206 * - runs the queue items
210 public void runQueue()
216 GLib.debug("GitMonitor.runQueue\n");
218 this.queueRunning = true;
220 var cmds = new Array<GitMonitorQueue>();
222 for(var i = 0; i < this.queue.length; i++) {
223 cmds.append_val(this.queue.index(i));
226 this.queue = new Array<GitMonitorQueue>();// empty queue!
229 string[] success = {};
230 string[] failure = {};
231 //var repos = new Array<GitRepo>(); //??
232 //var done = new Array<GitMonitorQueue>();
234 // first build a array of repo's to work with
235 var repo_list = new Array<GitRepo>();
239 //print(JSON.stringify(cmds));
240 // make sure nothing get's added to the queue where we are doing this..
244 GLib.debug("GitMonitor.runQueue - creating repos");
246 for(var i = 0; i < cmds.length; i++) {
248 var cmd = cmds.index(i);
250 var gitpath = cmd.gitpath;
252 var repo = GitRepo.get( gitpath );
253 if ( !repo.isWipBranch()) {
257 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
259 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
261 repo_list.append_val( GitRepo.get( gitpath ));
262 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
264 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
266 //if (typeof(repo_list[gitpath]) == 'undefined') {
267 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
268 // repo_list[gitpath].cmds = [];
269 // repo_list[gitpath].pull();
271 repo_list.index(ix).cmds.append_val(cmd);
275 // build add, remove and commit message list..
277 GLib.debug("GitMonitor.runQueue - creating actions");
279 for(var i = 0;i < repo_list.length;i++) {
281 var repo = repo_list.index(i);
283 var add_files = new Array<GitMonitorQueue>();
284 var add_files_f = new Array<GitMonitorQueue>();
285 var remove_files = new Array<GitMonitorQueue>();
286 var messages = new Array<GitMonitorQueue>();
287 //print(JSON.stringify(repo.cmds,null,4));
289 for(var ii = 0;ii < repo.cmds.length;ii++) {
290 var cmd = repo.cmds.index(ii);
292 if (repo.is_ignore(cmd.vname)) {
300 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
304 add_files.append_val(cmd);
308 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
312 // if file exists, do not try and delete it.
313 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
317 remove_files.append_val(cmd);
321 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
324 messages.append_val(cmd);
328 stdout.printf("Opps unmatched action %s\n", cmd.action);
332 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
333 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
336 // these can fail... at present... as we wildcard stuff.
338 // make sure added files do not get removed.. ??
340 var remove_files_f = new Array<GitMonitorQueue>();
341 for(var ii = 0;ii < remove_files.length;ii++) {
342 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
345 remove_files_f.append_val(remove_files.index(ii));
347 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
350 // if file was added, then removed,
351 var remove_files_f = new Array<GitMonitorQueue>();
352 for(var ii = 0;ii < remove_files.length;ii++) {
356 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
357 // in add and remove - do not remvove
360 remove_files_f.append_val(remove_files.index(ii));
362 for(var ii = 0;ii < add_files.length;ii++) {
363 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
364 // the add file is in the remove list, and it does not exist - do not add it..
365 print("check exists ? %s\n",add_files.index(ii).fullpath());
367 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
375 add_files_f.append_val(add_files.index(ii));
378 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
379 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
381 if (add_files_f.length < 1 && remove_files_f.length < 1) {
385 // make sure monitoring is paused so it does not recursively pick up
394 this.pauseError(e.message);
395 //failure += e.message;
396 //print("Pull failed:\n");
402 repo.add(add_files_f);
404 this.pauseError(e.message);
406 failure += e.message;
407 GLib.debug("Add failed:");
410 repo.remove(remove_files_f);
412 this.pauseError(e.message);
414 failure += e.message;
415 GLib.debug("Remove failed:");
420 success += repo.commit(
421 GitMonitorQueue.messageToString(messages),
424 success += repo.push();
428 // if the error is 'nothing to commit, working tree clean'
429 // then it's not an error, - just continue;
430 if (/nothing to commit, working tree clean/.match(e.message)) {
431 GLib.debug("%s",e.message);
432 success += e.message;
440 this.pauseError(e.message);
443 //failure += e.message;
444 //print("Push failed:\n");
451 // finally merge all the commit messages.
454 // catch notification failures.. so we can carry on..
455 if (success.length > 0) {
458 var notification = new Notify.Notification(
460 string.joinv("\n",success),
465 notification.set_timeout(5);
469 //if (failure.length > 0) {
471 // should never get this far...
472 // this.pauseError();
475 GLib.debug(e.message);
478 this.queueRunning = false;
485 //string[] just_created;
494 public override void onChanged(MonitorNamePathDir src)
496 //print("GitMonitor.onChanged\n");
497 return; // always ignore this..?
498 //this.parsePath(src);
504 * results in git add + git commit..
507 public override void onChangesDoneHint(MonitorNamePathDir src)
513 GLib.debug("GitMonitor.onChangedHint");
515 this.lastAdd = new DateTime.now(new TimeZone.local());
516 var cmd = new GitMonitorQueue(src);
517 if (cmd.shouldIgnore()) {
518 GLib.debug("GitMonitor.onChangedHint - ignored");
522 //var add_it = false;
524 if (this.is_just_created(cmd.path)) {
526 if (typeof(this.just_created[src.path]) !='undefined') {
527 delete this.just_created[src.path];
530 [ src.gitpath, 'add', src.vpath ],
531 [ src.gitpath, 'commit', { message: src.vpath} ]
539 this.queue.append_val(cmd);
541 cmd = new GitMonitorQueue(src);
542 cmd.action = "commit";
543 cmd.message = cmd.vname;
544 this.queue.append_val(cmd);
548 public override void onDeleted(MonitorNamePathDir src)
554 GLib.debug("GitMonitor.onDeleted");
555 this.lastAdd = new DateTime.now(new TimeZone.local());
556 var cmd = new GitMonitorQueue(src);
557 if (cmd.shouldIgnore()) {
560 // should check if monitor needs removing..
561 // it should also check if it was a directory.. - so we dont have to commit all..
563 this.queue.append_val(cmd);
565 cmd = new GitMonitorQueue(src);
566 cmd.action = "commit";
567 cmd.message = cmd.vname;
568 cmd.commit_all = true;
570 this.queue.append_val(cmd);
573 public override void onCreated(MonitorNamePathDir src) {
578 GLib.debug("GitMonitor.onCreated");
579 this.lastAdd = new DateTime.now(new TimeZone.local());
580 var cmd = new GitMonitorQueue(src);
581 if (cmd.shouldIgnore()) {
585 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
586 // this.just_created[src.path] = true;
587 return; // we do not handle file create flags... - use done hint.
589 // directory has bee created
590 this.monitor(src.path);
591 //this.top.append_val(src.path);
592 //this.monitor(src.path );
595 // -- no point in adding a dir.. as git does not handle them...
597 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
598 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
604 public override void onAttributeChanged(MonitorNamePathDir src) {
609 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
610 if (src.dir == GitMonitor.gitlive) {
611 return; // attribute on top level..
614 this.lastAdd = new DateTime.now(new TimeZone.local());
615 var cmd = new GitMonitorQueue(src);
616 if (cmd.shouldIgnore()) {
620 this.queue.append_val(cmd);
622 cmd = new GitMonitorQueue(src);
623 cmd.action = "commit";
624 cmd.message = "Attribute changed " + cmd.vname;
625 this.queue.append_val(cmd);
628 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
634 GLib.debug("GitMonitor.onMoved");
635 this.lastAdd = new DateTime.now(new TimeZone.local());
636 var cmd_s = new GitMonitorQueue(src);
638 var cmd_d = new GitMonitorQueue(dest);
641 if (cmd_d.gitpath != cmd_s.gitpath) {
643 this.onCreated(dest);
644 this.onChangesDoneHint(dest);
647 // needs to handle move to/from unsupported types..
649 if (cmd_s.shouldIgnore()) {
650 this.onCreated(dest);
651 this.onChangesDoneHint(dest);
655 if (cmd_d.shouldIgnore()) {
664 GLib.debug("RM: %s", cmd_s.vname);
666 this.queue.append_val(cmd_s);
670 GLib.debug("ADD: %s", cmd_d.vname);
671 cmd_d.action = "add";
672 this.queue.append_val(cmd_d);
675 var cmd = new GitMonitorQueue(dest);
676 cmd.action = "commit";
677 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
678 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
679 cmd.message = cmd_d.vname;
682 this.queue.append_val(cmd);