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[0].length < 1) {
32 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
35 for (var i = 1; i< vpath_ar.length; i++) {
39 this.vdir = string.joinv("/", vpath);
41 this.vname = this.vdir + (this.vdir.length > 0 ? "/" : "") + this.name;
46 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
47 "gitpath: %s\nvdir: %s\nvname: %s\n",
48 this.name, this.path, this.dir,
49 this.gitpath, this.vdir, this.vname
53 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
58 public bool shouldIgnore()
62 // vim.. what a seriously brain dead program..
63 if (this.name == "4913") {
67 if (this.name[0] == '.') {
69 if (this.name == ".htaccess") {
72 if (this.name == ".gitignore") {
79 if (this.name[this.name.length -1] == '~') {
82 // netbeans / android studio.. silly temp files..
84 if (Regex.match_simple("___jb_old___$", this.name)) {
87 if (Regex.match_simple("___jb_bak___$", this.name)) {
90 //if (f.name.match(/^nbproject/)) {
93 // ignore anything in top level!!!!
94 if (this.gitpath.length < 1) {
103 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
105 for(var i =0; i < add_files.length; i++) {
106 if (add_files.index(i).vname == add) {
112 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
113 for(var i =0; i < messages.length; i++) {
114 if (messages.index(i).message == message) {
120 public static string messageToString(Array<GitMonitorQueue> messages ) {
122 for(var i =0; i < messages.length; i++) {
123 ret+= messages.index(i).message;
125 return string.joinv("\n",ret);
127 public static string queueArrayToString(Array<GitMonitorQueue> list) {
129 for(var i =0; i < list.length; i++) {
131 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
137 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
138 for(var i =0; i < list.length; i++) {
139 var test = list.index(i);
140 if (list.index(i).gitpath != cmd_s.gitpath) {
143 if (list.index(i).vname != cmd_s.vname) {
146 if (list.index(i).action != action) {
153 public string fullpath()
155 return this.gitpath + "/" + this.vname;
164 public class GitMonitor : Monitor
167 public static GitMonitor gitmonitor;
169 * @property {String} the "gitlive" directory, normally ~/gitlive
170 * dset by OWNER... - we should do this as a CTOR.
173 public static string gitlive;
176 public Array<GitMonitorQueue> queue ;
177 public bool queueRunning = false;
179 public DateTime lastAdd;
181 private Canberra.Context sound_context;
183 public GitMonitor () {
187 this.queue = new Array<GitMonitorQueue>();
188 GitMonitor.gitmonitor = this;
190 Canberra.Context.create(out sound_context);
193 Timeout.add_full(Priority.LOW, 1000, () => {
195 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
197 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
198 if (this.queue.length < 1 || this.queueRunning) {
202 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
204 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
205 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
207 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
210 //_this.lastAdd = new Date();
220 public new void pauseError(string failure)
223 var notification = new Notify.Notification(
230 notification.set_timeout(60); // show errros for longer
233 sound_context.play(0, Canberra.PROP_EVENT_ID, "message-new-instant", Canberra.PROP_EVENT_DESCRIPTION, "Problem with gitlive");
238 this.queueRunning = false;
239 // what does this do to the old one...
240 this.queue = new Array<GitMonitorQueue> ();
242 StatusIconA.statusicon.pauseError();
244 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
245 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
246 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
249 m.set_keep_above(true);
251 m.set_position(Gtk.WindowPosition.CENTER);
252 m.response.connect( (id_pressed) => {
258 public new void pause() {
260 // what does this do to the old one...
261 this.queue = new Array<GitMonitorQueue> ();
262 StatusIconA.statusicon.pause();
267 public new void resume () {
269 this.queue = new Array<GitMonitorQueue> ();
270 StatusIconA.statusicon.resume();
276 * Start the monitoring
277 * and run the queue every 500 milliseconds..
280 public new void start()
282 StatusIconA.statusicon.refreshing();
285 this.lastAdd = new DateTime.now(new TimeZone.local());
287 Timeout.add_full(Priority.LOW, 500, () => {
288 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
289 // call this.monitor on each of 'top'
290 for(int i = 0; i < this.top.length ; i++) {
292 this.monitor(this.top.index(i) );
294 StatusIconA.statusicon.resume();
301 var notification = new Notify.Notification(
303 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
307 notification.set_timeout(5);
310 GLib.debug("Error sending notification to screen: %s\n",e.message);
312 return false; // do not keep doing this..
322 public new void stop() {
323 StatusIconA.statusicon.pause();
328 public new void monitor (string path, int depth = 0)
331 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
334 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
335 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
340 // FIXME - check if repo is flagged as not autocommit..
341 //var repo = imports.Scm.Repo.Repo.get(path);
342 //if (!repo || !repo.autocommit()) {
348 // check if the repo is to be monitored.
349 //print("PATH : " + path);
352 base.monitor(path, depth);
359 * - pulls the items off the queue
360 * (as commands run concurrently and new items may get added while it's running)
361 * - runs the queue items
365 public void runQueue()
371 GLib.debug("GitMonitor.runQueue\n");
373 this.queueRunning = true;
375 var cmds = new Array<GitMonitorQueue>();
377 for(var i = 0; i < this.queue.length; i++) {
378 cmds.append_val(this.queue.index(i));
381 this.queue = new Array<GitMonitorQueue>();// empty queue!
384 string[] success = {};
385 string[] failure = {};
386 //var repos = new Array<GitRepo>(); //??
387 //var done = new Array<GitMonitorQueue>();
389 // first build a array of repo's to work with
390 var repo_list = new Array<GitRepo>();
394 //print(JSON.stringify(cmds));
395 // make sure nothing get's added to the queue where we are doing this..
399 GLib.debug("GitMonitor.runQueue - creating repos\n");
401 for(var i = 0; i < cmds.length; i++) {
403 var cmd = cmds.index(i);
405 var gitpath = cmd.gitpath;
406 GLib.debug("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
408 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
410 repo_list.append_val(new GitRepo( gitpath ));
411 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
413 GLib.debug("GitMonitor.runQueue - adding to repolist %d\n", ix);
415 //if (typeof(repo_list[gitpath]) == 'undefined') {
416 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
417 // repo_list[gitpath].cmds = [];
418 // repo_list[gitpath].pull();
420 repo_list.index(ix).cmds.append_val(cmd);
424 // build add, remove and commit message list..
426 GLib.debug("GitMonitor.runQueue - creating actions\n");
428 for(var i = 0;i < repo_list.length;i++) {
430 var repo = repo_list.index(i);
432 var add_files = new Array<GitMonitorQueue>();
433 var add_files_f = new Array<GitMonitorQueue>();
434 var remove_files = new Array<GitMonitorQueue>();
435 var messages = new Array<GitMonitorQueue>();
436 //print(JSON.stringify(repo.cmds,null,4));
438 for(var ii = 0;ii < repo.cmds.length;ii++) {
439 var cmd = repo.cmds.index(ii);
441 if (repo.is_ignore(cmd.vname)) {
449 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
453 add_files.append_val(cmd);
457 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
461 // if file exists, do not try and delete it.
462 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
466 remove_files.append_val(cmd);
470 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
473 messages.append_val(cmd);
477 stdout.printf("Opps unmatched action %s\n", cmd.action);
481 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
482 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
485 // these can fail... at present... as we wildcard stuff.
487 // make sure added files do not get removed.. ??
489 var remove_files_f = new Array<GitMonitorQueue>();
490 for(var ii = 0;ii < remove_files.length;ii++) {
491 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
494 remove_files_f.append_val(remove_files.index(ii));
496 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
499 // if file was added, then removed,
500 var remove_files_f = new Array<GitMonitorQueue>();
501 for(var ii = 0;ii < remove_files.length;ii++) {
505 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
506 // in add and remove - do not remvove
509 remove_files_f.append_val(remove_files.index(ii));
511 for(var ii = 0;ii < add_files.length;ii++) {
512 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
513 // the add file is in the remove list, and it does not exist - do not add it..
514 print("check exists ? %s\n",add_files.index(ii).fullpath());
516 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
524 add_files_f.append_val(add_files.index(ii));
527 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
528 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
530 if (add_files_f.length < 1 && remove_files_f.length < 1) {
534 // make sure monitoring is paused so it does not recursively pick up
543 this.pauseError(e.message);
544 //failure += e.message;
545 //print("Pull failed:\n");
551 repo.add(add_files_f);
553 this.pauseError(e.message);
555 failure += e.message;
556 GLib.debug("Add failed:\n");
559 repo.remove(remove_files_f);
561 this.pauseError(e.message);
563 failure += e.message;
564 GLib.debug("Remove failed:\n");
569 success += repo.commit(
570 GitMonitorQueue.messageToString(messages),
573 success += repo.push();
577 // if the error is 'nothing to commit, working tree clean'
578 // then it's not an error, - just continue;
579 if (/nothing to commit, working tree clean/.match(e.message)) {
580 GLib.debug("%s\n",e.message);
581 success += e.message;
589 this.pauseError(e.message);
592 //failure += e.message;
593 //print("Push failed:\n");
600 // finally merge all the commit messages.
603 // catch notification failures.. so we can carry on..
604 if (success.length > 0) {
607 var notification = new Notify.Notification(
609 string.joinv("\n",success),
614 notification.set_timeout(5);
618 //if (failure.length > 0) {
620 // should never get this far...
621 // this.pauseError();
624 GLib.debug(e.message);
627 this.queueRunning = false;
634 //string[] just_created;
643 public override void onChanged(MonitorNamePathDir src)
645 //print("GitMonitor.onChanged\n");
646 return; // always ignore this..?
647 //this.parsePath(src);
653 * results in git add + git commit..
656 public override void onChangesDoneHint(MonitorNamePathDir src)
662 GLib.debug("GitMonitor.onChangedHint\n");
664 this.lastAdd = new DateTime.now(new TimeZone.local());
665 var cmd = new GitMonitorQueue(src);
666 if (cmd.shouldIgnore()) {
671 //var add_it = false;
673 if (this.is_just_created(cmd.path)) {
675 if (typeof(this.just_created[src.path]) !='undefined') {
676 delete this.just_created[src.path];
679 [ src.gitpath, 'add', src.vpath ],
680 [ src.gitpath, 'commit', { message: src.vpath} ]
688 this.queue.append_val(cmd);
690 cmd = new GitMonitorQueue(src);
691 cmd.action = "commit";
692 cmd.message = cmd.vname;
693 this.queue.append_val(cmd);
697 public override void onDeleted(MonitorNamePathDir src)
703 GLib.debug("GitMonitor.onDeleted\n");
704 this.lastAdd = new DateTime.now(new TimeZone.local());
705 var cmd = new GitMonitorQueue(src);
706 if (cmd.shouldIgnore()) {
709 // should check if monitor needs removing..
710 // it should also check if it was a directory.. - so we dont have to commit all..
712 this.queue.append_val(cmd);
714 cmd = new GitMonitorQueue(src);
715 cmd.action = "commit";
716 cmd.message = cmd.vname;
717 cmd.commit_all = true;
719 this.queue.append_val(cmd);
722 public override void onCreated(MonitorNamePathDir src) {
727 GLib.debug("GitMonitor.onCreated\n");
728 this.lastAdd = new DateTime.now(new TimeZone.local());
729 var cmd = new GitMonitorQueue(src);
730 if (cmd.shouldIgnore()) {
734 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
735 // this.just_created[src.path] = true;
736 return; // we do not handle file create flags... - use done hint.
738 // directory has bee created
739 this.monitor(src.path);
740 //this.top.append_val(src.path);
741 //this.monitor(src.path );
744 // -- no point in adding a dir.. as git does not handle them...
746 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
747 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
753 public override void onAttributeChanged(MonitorNamePathDir src) {
758 GLib.debug("GitMonitor.onAttributeChanged\n");
759 if (src.dir == GitMonitor.gitlive) {
760 return; // attribute on top level..
763 this.lastAdd = new DateTime.now(new TimeZone.local());
764 var cmd = new GitMonitorQueue(src);
765 if (cmd.shouldIgnore()) {
769 this.queue.append_val(cmd);
771 cmd = new GitMonitorQueue(src);
772 cmd.action = "commit";
773 cmd.message = "Attribute changed " + cmd.vname;
774 this.queue.append_val(cmd);
777 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
783 GLib.debug("GitMonitor.onMoved\n");
784 this.lastAdd = new DateTime.now(new TimeZone.local());
785 var cmd_s = new GitMonitorQueue(src);
787 var cmd_d = new GitMonitorQueue(dest);
790 if (cmd_d.gitpath != cmd_s.gitpath) {
792 this.onCreated(dest);
793 this.onChangesDoneHint(dest);
796 // needs to handle move to/from unsupported types..
798 if (cmd_s.shouldIgnore()) {
799 this.onCreated(dest);
800 this.onChangesDoneHint(dest);
804 if (cmd_d.shouldIgnore()) {
813 GLib.debug("RM: %s\n", cmd_s.vname);
815 this.queue.append_val(cmd_s);
819 GLib.debug("ADD: %s\n", cmd_d.vname);
820 cmd_d.action = "add";
821 this.queue.append_val(cmd_d);
824 var cmd = new GitMonitorQueue(dest);
825 cmd.action = "commit";
826 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
827 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
828 cmd.message = cmd_d.vname;
831 this.queue.append_val(cmd);