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 ) {
228 foreach(var q in this.queue) {
229 if (!q.shouldIgnore() && !q.repo.is_wip_branch()) {
230 NewBranch.singleton().show(q.repo);
238 GLib.debug("GitMonitor.runQueue\n");
240 this.queueRunning = true;
242 var cmds = new Gee.ArrayList<GitMonitorQueue>();
244 for(var i = 0; i < this.queue.size; i++) {
245 cmds.add(this.queue.get(i));
248 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
250 string[] success = {};
251 string[] failure = {};
252 //var repos = new Array<GitRepo>(); //??
253 //var done = new Array<GitMonitorQueue>();
255 // first build a array of repo's to work with
256 var repo_list = new Array<GitRepo>();
260 //print(JSON.stringify(cmds));
261 // make sure nothing get's added to the queue where we are doing this..
265 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
266 GLib.debug("GitMonitor.runQueue - creating repos");
268 for(var i = 0; i < cmds.size; i++) {
270 var cmd = cmds.get(i);
272 var gitpath = cmd.gitpath;
274 var repo = GitRepo.get( gitpath );
275 if ( !repo.is_wip_branch()) {
276 leave_queued.add(cmd);
280 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
282 var ix = GitRepo.indexOf(repo_list, gitpath);
284 repo_list.append_val( GitRepo.get( gitpath ));
285 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
287 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
289 //if (typeof(repo_list[gitpath]) == 'undefined') {
290 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
291 // repo_list[gitpath].cmds = [];
292 // repo_list[gitpath].pull();
294 repo_list.index(ix).cmds.add(cmd);
297 this.queue = leave_queued;
300 // build add, remove and commit message list..
302 GLib.debug("GitMonitor.runQueue - creating actions");
304 for(var i = 0;i < repo_list.length;i++) {
306 var repo = repo_list.index(i);
308 var add_files = new Gee.ArrayList<GitMonitorQueue>();
309 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
310 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
311 var messages = new Gee.ArrayList<GitMonitorQueue>();
312 //print(JSON.stringify(repo.cmds,null,4));
314 for(var ii = 0;ii < repo.cmds.size;ii++) {
315 var cmd = repo.cmds.get(ii);
317 if (repo.is_ignore(cmd.vname)) {
325 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
333 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
337 // if file exists, do not try and delete it.
338 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
342 remove_files.add(cmd);
346 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
353 stdout.printf("Opps unmatched action %s\n", cmd.action);
358 repo.cmds.clear(); // reset the repo's command list..
360 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
361 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
364 // these can fail... at present... as we wildcard stuff.
366 // make sure added files do not get removed.. ??
368 var remove_files_f = new Array<GitMonitorQueue>();
369 for(var ii = 0;ii < remove_files.length;ii++) {
370 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
373 remove_files_f.append_val(remove_files.index(ii));
375 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
378 // if file was added, then removed,
379 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
380 for(var ii = 0;ii < remove_files.size;ii++) {
384 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
385 // in add and remove - do not remvove
388 remove_files_f.add(remove_files.get(ii));
390 for(var ii = 0;ii < add_files.size;ii++) {
391 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
392 // the add file is in the remove list, and it does not exist - do not add it..
393 print("check exists ? %s\n",add_files.get(ii).fullpath());
395 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
401 add_files_f.add(add_files.get(ii));
404 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
405 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
407 if (add_files_f.size < 1 && remove_files_f.size < 1) {
411 // make sure monitoring is paused so it does not recursively pick up
420 this.pauseError(e.message);
421 //failure += e.message;
422 //print("Pull failed:\n");
428 repo.add(add_files_f);
430 this.pauseError(e.message);
432 failure += e.message;
433 GLib.debug("Add failed:");
436 repo.remove(remove_files_f);
438 this.pauseError(e.message);
440 failure += e.message;
441 GLib.debug("Remove failed:");
445 success += repo.commit(
446 GitMonitorQueue.messageToString(messages),
449 success += repo.push();
454 // if the error is 'nothing to commit, working tree clean'
455 // then it's not an error, - just continue;
456 if (/nothing to commit, working tree clean/.match(e.message)) {
457 GLib.debug("%s",e.message);
458 success += e.message;
466 this.pauseError(e.message);
469 //failure += e.message;
470 //print("Push failed:\n");
477 // finally merge all the commit messages.
480 // catch notification failures.. so we can carry on..
481 if (success.length > 0) {
484 var notification = new Notify.Notification(
486 string.joinv("\n",success),
491 notification.set_timeout(5);
495 //if (failure.length > 0) {
497 // should never get this far...
498 // this.pauseError();
501 GLib.debug(e.message);
504 this.queueRunning = false;
511 //string[] just_created;
520 public override void onChanged(MonitorNamePathDir src)
522 //print("GitMonitor.onChanged\n");
523 return; // always ignore this..?
524 //this.parsePath(src);
530 * results in git add + git commit..
533 public override void onChangesDoneHint(MonitorNamePathDir src)
539 GLib.debug("GitMonitor.onChangedHint");
541 this.lastAdd = new DateTime.now(new TimeZone.local());
542 var cmd = new GitMonitorQueue(src);
543 if (cmd.shouldIgnore()) {
544 GLib.debug("GitMonitor.onChangedHint - ignored");
548 //var add_it = false;
550 if (this.is_just_created(cmd.path)) {
552 if (typeof(this.just_created[src.path]) !='undefined') {
553 delete this.just_created[src.path];
556 [ src.gitpath, 'add', src.vpath ],
557 [ src.gitpath, 'commit', { message: src.vpath} ]
567 cmd = new GitMonitorQueue(src);
568 cmd.action = "commit";
569 cmd.message = cmd.vname;
573 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
574 NewBranch.singleton().show(cmd.repo);
579 public override void onDeleted(MonitorNamePathDir src)
585 GLib.debug("GitMonitor.onDeleted");
586 this.lastAdd = new DateTime.now(new TimeZone.local());
587 var cmd = new GitMonitorQueue(src);
588 if (cmd.shouldIgnore()) {
591 // should check if monitor needs removing..
592 // it should also check if it was a directory.. - so we dont have to commit all..
596 cmd = new GitMonitorQueue(src);
597 cmd.action = "commit";
598 cmd.message = cmd.vname;
599 cmd.commit_all = true;
603 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
604 NewBranch.singleton().show(cmd.repo);
609 public override void onCreated(MonitorNamePathDir src) {
614 GLib.debug("GitMonitor.onCreated");
615 this.lastAdd = new DateTime.now(new TimeZone.local());
616 var cmd = new GitMonitorQueue(src);
617 if (cmd.shouldIgnore()) {
621 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
622 // this.just_created[src.path] = true;
623 return; // we do not handle file create flags... - use done hint.
625 // directory has bee created
626 this.monitor(src.path);
627 //this.top.append_val(src.path);
628 //this.monitor(src.path );
631 // -- no point in adding a dir.. as git does not handle them...
633 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
634 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
640 public override void onAttributeChanged(MonitorNamePathDir src)
646 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
647 if (src.dir == GitMonitor.gitlive) {
648 return; // attribute on top level..
651 this.lastAdd = new DateTime.now(new TimeZone.local());
652 var cmd = new GitMonitorQueue(src);
653 if (cmd.shouldIgnore()) {
659 cmd = new GitMonitorQueue(src);
660 cmd.action = "commit";
661 cmd.message = "Attribute changed " + cmd.vname;
664 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
665 NewBranch.singleton().show(cmd.repo);
669 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
675 GLib.debug("GitMonitor.onMoved");
676 this.lastAdd = new DateTime.now(new TimeZone.local());
677 var cmd_s = new GitMonitorQueue(src);
679 var cmd_d = new GitMonitorQueue(dest);
682 if (cmd_d.gitpath != cmd_s.gitpath) {
684 this.onCreated(dest);
685 this.onChangesDoneHint(dest);
688 // needs to handle move to/from unsupported types..
690 if (cmd_s.shouldIgnore()) {
691 this.onCreated(dest);
692 this.onChangesDoneHint(dest);
696 if (cmd_d.shouldIgnore()) {
705 GLib.debug("RM: %s", cmd_s.vname);
707 this.queue.add(cmd_s);
711 GLib.debug("ADD: %s", cmd_d.vname);
712 cmd_d.action = "add";
713 this.queue.add(cmd_d);
716 var cmd = new GitMonitorQueue(dest);
717 cmd.action = "commit";
718 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
719 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
720 cmd.message = cmd_d.vname;
726 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
727 NewBranch.singleton().show(cmd.repo);
729 // this is where it get's complicated...
730 // as we might be moving across repo's...