4 public class GitMonitorQueue : MonitorNamePathDir {
10 public string vdir; // relative path (within git)
11 public string vname; // relative filename (within git)
12 public string message ; // for commit
13 public bool commit_all;
15 public GitMonitorQueue(MonitorNamePathDir f) {
17 base(f.name, f.path, f.dir);
20 this.commit_all = false;
22 var vpath_ar = this.dir.substring(GitMonitor.gitlive.length +1).split("/", 0);
24 if (vpath_ar.length < 1 || vpath_ar[0].length < 1) {
33 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
36 for (var i = 1; i< vpath_ar.length; i++) {
40 this.vdir = string.joinv("/", vpath);
42 this.vname = this.vdir + (this.vdir.length > 0 ? "/" : "") + this.name;
47 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
48 "gitpath: %s\nvdir: %s\nvname: %s\n",
49 this.name, this.path, this.dir,
50 this.gitpath, this.vdir, this.vname
54 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
59 public bool shouldIgnore()
62 // vim.. what a seriously brain dead program..
63 if (this.name == "4913") {
64 GLib.debug("ignore name = 4913");
69 if (this.name[0] == '.') {
71 if (this.name == ".htaccess") {
75 if (this.name == ".gitignore") {
78 GLib.debug("ignore name starts with dot %s", this.name);
83 if (this.name[this.name.length -1] == '~') {
84 GLib.debug("ignore name ends with ~");
87 // netbeans / android studio.. silly temp files..
89 if (Regex.match_simple("___jb_old___$", this.name)) {
90 GLib.debug("ignore name includes jb_old");
93 if (Regex.match_simple("___jb_bak___$", this.name)) {
94 GLib.debug("ignore name includes jb_bkc");
97 //if (f.name.match(/^nbproject/)) {
100 // ignore anything in top level!!!!
101 if (this.gitpath.length < 1) {
102 GLib.debug("ignore gitpath length is empty");
111 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
113 for(var i =0; i < add_files.length; i++) {
114 if (add_files.index(i).vname == add) {
120 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
121 for(var i =0; i < messages.length; i++) {
122 if (messages.index(i).message == message) {
128 public static string messageToString(Array<GitMonitorQueue> messages ) {
130 for(var i =0; i < messages.length; i++) {
131 ret+= messages.index(i).message;
133 return string.joinv("\n",ret);
135 public static string queueArrayToString(Array<GitMonitorQueue> list) {
137 for(var i =0; i < list.length; i++) {
139 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
145 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
146 for(var i =0; i < list.length; i++) {
147 var test = list.index(i);
148 if (list.index(i).gitpath != cmd_s.gitpath) {
151 if (list.index(i).vname != cmd_s.vname) {
154 if (list.index(i).action != action) {
161 public string fullpath()
163 return this.gitpath + "/" + this.vname;
172 public class GitMonitor : Monitor
175 public static GitMonitor gitmonitor;
177 * @property {String} the "gitlive" directory, normally ~/gitlive
178 * dset by OWNER... - we should do this as a CTOR.
181 public static string gitlive;
184 public Array<GitMonitorQueue> queue ;
185 public bool queueRunning = false;
187 public DateTime lastAdd;
191 public GitMonitor () {
195 this.queue = new Array<GitMonitorQueue>();
196 GitMonitor.gitmonitor = this;
201 Timeout.add_full(Priority.LOW, 500, () => {
203 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
205 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
206 if (this.queue.length < 1 || this.queueRunning) {
210 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
212 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
213 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
215 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
218 //_this.lastAdd = new Date();
228 public new void pauseError(string failure)
231 var notification = new Notify.Notification(
238 notification.set_timeout(60); // show errros for longer
241 Canberra.Context context;
242 Canberra.Proplist props;
244 Canberra.Context.create (out context);
245 Canberra.Proplist.create (out props);
247 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
248 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
251 context.play_full (0, props, null);
257 this.queueRunning = false;
258 // what does this do to the old one...
259 this.queue = new Array<GitMonitorQueue> ();
261 StatusIconA.statusicon.pauseError();
263 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
264 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
265 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
268 m.set_keep_above(true);
270 m.set_position(Gtk.WindowPosition.CENTER);
271 m.response.connect( (id_pressed) => {
277 public new void pause() {
279 // what does this do to the old one...
280 this.queue = new Array<GitMonitorQueue> ();
281 StatusIconA.statusicon.pause();
286 public new void resume () {
288 this.queue = new Array<GitMonitorQueue> ();
289 StatusIconA.statusicon.resume();
295 * Start the monitoring
296 * and run the queue every 500 milliseconds..
299 public new void start()
301 StatusIconA.statusicon.refreshing();
304 this.lastAdd = new DateTime.now(new TimeZone.local());
306 Timeout.add_full(Priority.LOW, 500, () => {
307 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
308 // call this.monitor on each of 'top'
309 for(int i = 0; i < this.top.length ; i++) {
311 this.monitor(this.top.index(i) );
313 StatusIconA.statusicon.resume();
320 var notification = new Notify.Notification(
322 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
326 notification.set_timeout(5);
329 GLib.debug("Error sending notification to screen: %s",e.message);
331 return false; // do not keep doing this..
341 public new void stop() {
342 StatusIconA.statusicon.pause();
347 public new void monitor (string path, int depth = 0)
350 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
353 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
354 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
359 // FIXME - check if repo is flagged as not autocommit..
360 //var repo = imports.Scm.Repo.Repo.get(path);
361 //if (!repo || !repo.autocommit()) {
367 // check if the repo is to be monitored.
368 //print("PATH : " + path);
371 base.monitor(path, depth);
378 * - pulls the items off the queue
379 * (as commands run concurrently and new items may get added while it's running)
380 * - runs the queue items
384 public void runQueue()
390 GLib.debug("GitMonitor.runQueue\n");
392 this.queueRunning = true;
394 var cmds = new Array<GitMonitorQueue>();
396 for(var i = 0; i < this.queue.length; i++) {
397 cmds.append_val(this.queue.index(i));
400 this.queue = new Array<GitMonitorQueue>();// empty queue!
403 string[] success = {};
404 string[] failure = {};
405 //var repos = new Array<GitRepo>(); //??
406 //var done = new Array<GitMonitorQueue>();
408 // first build a array of repo's to work with
409 var repo_list = new Array<GitRepo>();
413 //print(JSON.stringify(cmds));
414 // make sure nothing get's added to the queue where we are doing this..
418 GLib.debug("GitMonitor.runQueue - creating repos");
420 for(var i = 0; i < cmds.length; i++) {
422 var cmd = cmds.index(i);
424 var gitpath = cmd.gitpath;
425 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
427 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
429 repo_list.append_val(new GitRepo( gitpath ));
430 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
432 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
434 //if (typeof(repo_list[gitpath]) == 'undefined') {
435 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
436 // repo_list[gitpath].cmds = [];
437 // repo_list[gitpath].pull();
439 repo_list.index(ix).cmds.append_val(cmd);
443 // build add, remove and commit message list..
445 GLib.debug("GitMonitor.runQueue - creating actions");
447 for(var i = 0;i < repo_list.length;i++) {
449 var repo = repo_list.index(i);
451 var add_files = new Array<GitMonitorQueue>();
452 var add_files_f = new Array<GitMonitorQueue>();
453 var remove_files = new Array<GitMonitorQueue>();
454 var messages = new Array<GitMonitorQueue>();
455 //print(JSON.stringify(repo.cmds,null,4));
457 for(var ii = 0;ii < repo.cmds.length;ii++) {
458 var cmd = repo.cmds.index(ii);
460 if (repo.is_ignore(cmd.vname)) {
468 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
472 add_files.append_val(cmd);
476 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
480 // if file exists, do not try and delete it.
481 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
485 remove_files.append_val(cmd);
489 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
492 messages.append_val(cmd);
496 stdout.printf("Opps unmatched action %s\n", cmd.action);
500 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
501 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
504 // these can fail... at present... as we wildcard stuff.
506 // make sure added files do not get removed.. ??
508 var remove_files_f = new Array<GitMonitorQueue>();
509 for(var ii = 0;ii < remove_files.length;ii++) {
510 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
513 remove_files_f.append_val(remove_files.index(ii));
515 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
518 // if file was added, then removed,
519 var remove_files_f = new Array<GitMonitorQueue>();
520 for(var ii = 0;ii < remove_files.length;ii++) {
524 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
525 // in add and remove - do not remvove
528 remove_files_f.append_val(remove_files.index(ii));
530 for(var ii = 0;ii < add_files.length;ii++) {
531 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
532 // the add file is in the remove list, and it does not exist - do not add it..
533 print("check exists ? %s\n",add_files.index(ii).fullpath());
535 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
543 add_files_f.append_val(add_files.index(ii));
546 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
547 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
549 if (add_files_f.length < 1 && remove_files_f.length < 1) {
553 // make sure monitoring is paused so it does not recursively pick up
562 this.pauseError(e.message);
563 //failure += e.message;
564 //print("Pull failed:\n");
570 repo.add(add_files_f);
572 this.pauseError(e.message);
574 failure += e.message;
575 GLib.debug("Add failed:");
578 repo.remove(remove_files_f);
580 this.pauseError(e.message);
582 failure += e.message;
583 GLib.debug("Remove failed:");
588 success += repo.commit(
589 GitMonitorQueue.messageToString(messages),
592 success += repo.push();
596 // if the error is 'nothing to commit, working tree clean'
597 // then it's not an error, - just continue;
598 if (/nothing to commit, working tree clean/.match(e.message)) {
599 GLib.debug("%s",e.message);
600 success += e.message;
608 this.pauseError(e.message);
611 //failure += e.message;
612 //print("Push failed:\n");
619 // finally merge all the commit messages.
622 // catch notification failures.. so we can carry on..
623 if (success.length > 0) {
626 var notification = new Notify.Notification(
628 string.joinv("\n",success),
633 notification.set_timeout(5);
637 //if (failure.length > 0) {
639 // should never get this far...
640 // this.pauseError();
643 GLib.debug(e.message);
646 this.queueRunning = false;
653 //string[] just_created;
662 public override void onChanged(MonitorNamePathDir src)
664 //print("GitMonitor.onChanged\n");
665 return; // always ignore this..?
666 //this.parsePath(src);
672 * results in git add + git commit..
675 public override void onChangesDoneHint(MonitorNamePathDir src)
681 GLib.debug("GitMonitor.onChangedHint");
683 this.lastAdd = new DateTime.now(new TimeZone.local());
684 var cmd = new GitMonitorQueue(src);
685 if (cmd.shouldIgnore()) {
686 GLib.debug("GitMonitor.onChangedHint - ignored");
690 //var add_it = false;
692 if (this.is_just_created(cmd.path)) {
694 if (typeof(this.just_created[src.path]) !='undefined') {
695 delete this.just_created[src.path];
698 [ src.gitpath, 'add', src.vpath ],
699 [ src.gitpath, 'commit', { message: src.vpath} ]
707 this.queue.append_val(cmd);
709 cmd = new GitMonitorQueue(src);
710 cmd.action = "commit";
711 cmd.message = cmd.vname;
712 this.queue.append_val(cmd);
716 public override void onDeleted(MonitorNamePathDir src)
722 GLib.debug("GitMonitor.onDeleted");
723 this.lastAdd = new DateTime.now(new TimeZone.local());
724 var cmd = new GitMonitorQueue(src);
725 if (cmd.shouldIgnore()) {
728 // should check if monitor needs removing..
729 // it should also check if it was a directory.. - so we dont have to commit all..
731 this.queue.append_val(cmd);
733 cmd = new GitMonitorQueue(src);
734 cmd.action = "commit";
735 cmd.message = cmd.vname;
736 cmd.commit_all = true;
738 this.queue.append_val(cmd);
741 public override void onCreated(MonitorNamePathDir src) {
746 GLib.debug("GitMonitor.onCreated");
747 this.lastAdd = new DateTime.now(new TimeZone.local());
748 var cmd = new GitMonitorQueue(src);
749 if (cmd.shouldIgnore()) {
753 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
754 // this.just_created[src.path] = true;
755 return; // we do not handle file create flags... - use done hint.
757 // directory has bee created
758 this.monitor(src.path);
759 //this.top.append_val(src.path);
760 //this.monitor(src.path );
763 // -- no point in adding a dir.. as git does not handle them...
765 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
766 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
772 public override void onAttributeChanged(MonitorNamePathDir src) {
777 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
778 if (src.dir == GitMonitor.gitlive) {
779 return; // attribute on top level..
782 this.lastAdd = new DateTime.now(new TimeZone.local());
783 var cmd = new GitMonitorQueue(src);
784 if (cmd.shouldIgnore()) {
788 this.queue.append_val(cmd);
790 cmd = new GitMonitorQueue(src);
791 cmd.action = "commit";
792 cmd.message = "Attribute changed " + cmd.vname;
793 this.queue.append_val(cmd);
796 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
802 GLib.debug("GitMonitor.onMoved");
803 this.lastAdd = new DateTime.now(new TimeZone.local());
804 var cmd_s = new GitMonitorQueue(src);
806 var cmd_d = new GitMonitorQueue(dest);
809 if (cmd_d.gitpath != cmd_s.gitpath) {
811 this.onCreated(dest);
812 this.onChangesDoneHint(dest);
815 // needs to handle move to/from unsupported types..
817 if (cmd_s.shouldIgnore()) {
818 this.onCreated(dest);
819 this.onChangesDoneHint(dest);
823 if (cmd_d.shouldIgnore()) {
832 GLib.debug("RM: %s", cmd_s.vname);
834 this.queue.append_val(cmd_s);
838 GLib.debug("ADD: %s", cmd_d.vname);
839 cmd_d.action = "add";
840 this.queue.append_val(cmd_d);
843 var cmd = new GitMonitorQueue(dest);
844 cmd.action = "commit";
845 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
846 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
847 cmd.message = cmd_d.vname;
850 this.queue.append_val(cmd);