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;
44 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
45 "gitpath: %s\nvdir: %s\nvname: %s\n",
46 this.name, this.path, this.dir,
47 this.gitpath, this.vdir, this.vname
51 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
56 public bool shouldIgnore()
60 // vim.. what a seriously brain dead program..
61 if (this.name == "4913") {
65 if (this.name[0] == '.') {
67 if (this.name == ".htaccess") {
73 if (this.name[this.name.length -1] == '~') {
77 //if (f.name.match(/~$/)) {
80 //if (f.name.match(/^nbproject/)) {
83 // ignore anything in top level!!!!
84 if (this.gitpath.length < 1) {
93 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
95 for(var i =0; i < add_files.length; i++) {
96 if (add_files.index(i).vname == add) {
102 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
103 for(var i =0; i < messages.length; i++) {
104 if (messages.index(i).message == message) {
110 public static string messageToString(Array<GitMonitorQueue> messages ) {
112 for(var i =0; i < messages.length; i++) {
113 ret+= messages.index(i).message;
115 return string.joinv("\n",ret);
117 public static string queueArrayToString(Array<GitMonitorQueue> list) {
119 for(var i =0; i < list.length; i++) {
121 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
127 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
128 for(var i =0; i < list.length; i++) {
129 var test = list.index(i);
130 if (list.index(i).gitpath != cmd_s.gitpath) {
133 if (list.index(i).vname != cmd_s.vname) {
136 if (list.index(i).action != action) {
143 public string fullpath()
145 return this.gitpath + "/" + this.vname;
154 public class GitMonitor : Monitor
157 public static GitMonitor gitmonitor;
159 * @property {String} the "gitlive" directory, normally ~/gitlive
160 * dset by OWNER... - we should do this as a CTOR.
163 public static string gitlive;
166 public Array<GitMonitorQueue> queue ;
167 public bool queueRunning = false;
169 public DateTime lastAdd;
172 public GitMonitor () {
173 this.queue = new Array<GitMonitorQueue>();
174 GitMonitor.gitmonitor = this;
180 public new void pause() {
182 // what does this do to the old one...
183 this.queue = new Array<GitMonitorQueue> ();
184 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
188 public new void resume () {
190 this.queue = new Array<GitMonitorQueue> ();
191 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
196 * Start the monitoring
197 * and run the queue every 500 milliseconds..
200 public new void start() {
201 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
204 this.lastAdd = new DateTime.now(new TimeZone.local());
206 Timeout.add_full(Priority.LOW, 500, () => {
207 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
208 // call this.monitor on each of 'top'
209 for(int i = 0; i < this.top.length ; i++) {
211 this.monitor(this.top.index(i) );
213 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
220 var notification = new Notify.Notification(
222 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
226 notification.set_timeout(5);
231 return false; // do not keep doing this..
236 Timeout.add_full(Priority.LOW, 1000, () => {
237 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
239 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
240 if (this.queue.length < 1 || this.queueRunning) {
244 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
246 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
247 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
249 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
252 //_this.lastAdd = new Date();
262 public new void stop() {
263 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
268 public new void monitor (string path, int depth = 0)
271 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
274 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
275 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
280 // FIXME - check if repo is flagged as not autocommit..
281 //var repo = imports.Scm.Repo.Repo.get(path);
282 //if (!repo || !repo.autocommit()) {
288 // check if the repo is to be monitored.
289 //print("PATH : " + path);
292 base.monitor(path, depth);
299 * - pulls the items off the queue
300 * (as commands run concurrently and new items may get added while it's running)
301 * - runs the queue items
305 public void runQueue()
311 print("GitMonitor.runQueue\n");
313 this.queueRunning = true;
315 var cmds = new Array<GitMonitorQueue>();
317 for(var i = 0; i < this.queue.length; i++) {
318 cmds.append_val(this.queue.index(i));
321 this.queue = new Array<GitMonitorQueue>();// empty queue!
324 string[] success = {};
325 string[] failure = {};
326 //var repos = new Array<GitRepo>(); //??
327 //var done = new Array<GitMonitorQueue>();
329 // first build a array of repo's to work with
330 var repo_list = new Array<GitRepo>();
334 //print(JSON.stringify(cmds));
335 // make sure nothing get's added to the queue where we are doing this..
339 print("GitMonitor.runQueue - creating repos\n");
341 for(var i = 0; i < cmds.length; i++) {
343 var cmd = cmds.index(i);
345 var gitpath = cmd.gitpath;
346 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
348 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
350 repo_list.append_val(new GitRepo( gitpath ));
351 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
353 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
355 //if (typeof(repo_list[gitpath]) == 'undefined') {
356 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
357 // repo_list[gitpath].cmds = [];
358 // repo_list[gitpath].pull();
360 repo_list.index(ix).cmds.append_val(cmd);
364 // build add, remove and commit message list..
366 print("GitMonitor.runQueue - creating actions\n");
368 for(var i = 0;i < repo_list.length;i++) {
370 var repo = repo_list.index(i);
372 var add_files = new Array<GitMonitorQueue>();
373 var add_files_f = new Array<GitMonitorQueue>();
374 var remove_files = new Array<GitMonitorQueue>();
375 var messages = new Array<GitMonitorQueue>();
376 //print(JSON.stringify(repo.cmds,null,4));
378 for(var ii = 0;ii < repo.cmds.length;ii++) {
379 var cmd = repo.cmds.index(ii);
385 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
389 add_files.append_val(cmd);
393 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
397 // if file exists, do not try and delete it.
398 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
402 remove_files.append_val(cmd);
406 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
409 messages.append_val(cmd);
413 stdout.printf("Opps unmatched action %s\n", cmd.action);
417 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
418 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
420 // these can fail... at present... as we wildcard stuff.
422 // make sure added files do not get removed.. ??
424 var remove_files_f = new Array<GitMonitorQueue>();
425 for(var ii = 0;ii < remove_files.length;ii++) {
426 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
429 remove_files_f.append_val(remove_files.index(ii));
431 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
434 // if file was added, then removed,
435 var remove_files_f = new Array<GitMonitorQueue>();
436 for(var ii = 0;ii < remove_files.length;ii++) {
440 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
441 // in add and remove - do not remvove
444 remove_files_f.append_val(remove_files.index(ii));
446 for(var ii = 0;ii < add_files.length;ii++) {
447 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
448 // the add file is in the remove list, and it does not exist - do not add it..
449 print("check exists ? %s\n",add_files.index(ii).fullpath());
451 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
459 add_files_f.append_val(add_files.index(ii));
462 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
463 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
466 // make sure monitoring is paused so it does not recursively pick up
471 failure += e.message;
477 repo.add(add_files_f);
479 failure += e.message;
482 repo.remove(remove_files_f);
484 failure += e.message;
491 success += repo.commit(
492 GitMonitorQueue.messageToString(messages),
495 success += repo.push();
498 failure += e.message;
503 // finally merge all the commit messages.
506 // catch notification failures.. so we can carry on..
507 if (success.length > 0) {
510 var notification = new Notify.Notification(
512 string.joinv("\n",success),
517 notification.set_timeout(5);
521 if (failure.length > 0) {
523 var notification = new Notify.Notification(
525 string.joinv("\n",failure),
530 notification.set_timeout(5); // show errros for longer
537 this.queueRunning = false;
544 //string[] just_created;
553 public override void onChanged(MonitorNamePathDir src)
555 print("GitMonitor.onChanged\n");
556 return; // always ignore this..?
557 //this.parsePath(src);
563 * results in git add + git commit..
566 public override void onChangesDoneHint(MonitorNamePathDir src)
568 print("GitMonitor.onChangedHint\n");
574 this.lastAdd = new DateTime.now(new TimeZone.local());
575 var cmd = new GitMonitorQueue(src);
576 if (cmd.shouldIgnore()) {
581 //var add_it = false;
583 if (this.is_just_created(cmd.path)) {
585 if (typeof(this.just_created[src.path]) !='undefined') {
586 delete this.just_created[src.path];
589 [ src.gitpath, 'add', src.vpath ],
590 [ src.gitpath, 'commit', { message: src.vpath} ]
598 this.queue.append_val(cmd);
600 cmd = new GitMonitorQueue(src);
601 cmd.action = "commit";
602 cmd.message = cmd.vname;
603 this.queue.append_val(cmd);
607 public override void onDeleted(MonitorNamePathDir src)
609 print("GitMonitor.onDeleted\n");
613 this.lastAdd = new DateTime.now(new TimeZone.local());
614 var cmd = new GitMonitorQueue(src);
615 if (cmd.shouldIgnore()) {
618 // should check if monitor needs removing..
619 // it should also check if it was a directory.. - so we dont have to commit all..
621 this.queue.append_val(cmd);
623 cmd = new GitMonitorQueue(src);
624 cmd.action = "commit";
625 cmd.message = cmd.vname;
626 cmd.commit_all = true;
628 this.queue.append_val(cmd);
631 public override void onCreated(MonitorNamePathDir src) {
632 print("GitMonitor.onCreated\n");
636 this.lastAdd = new DateTime.now(new TimeZone.local());
637 var cmd = new GitMonitorQueue(src);
638 if (cmd.shouldIgnore()) {
642 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
643 // this.just_created[src.path] = true;
644 return; // we do not handle file create flags... - use done hint.
646 // directory has bee created
647 this.monitor(src.path);
648 //this.top.append_val(src.path);
649 //this.monitor(src.path );
652 // -- no point in adding a dir.. as git does not handle them...
654 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
655 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
661 public override void onAttributeChanged(MonitorNamePathDir src) {
662 print("GitMonitor.onAttributeChanged\n");
666 if (src.dir == GitMonitor.gitlive) {
667 return; // attribute on top level..
670 this.lastAdd = new DateTime.now(new TimeZone.local());
671 var cmd = new GitMonitorQueue(src);
672 if (cmd.shouldIgnore()) {
676 this.queue.append_val(cmd);
678 cmd = new GitMonitorQueue(src);
679 cmd.action = "commit";
680 cmd.message = "Attribute changed " + cmd.vname;
681 this.queue.append_val(cmd);
684 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
686 print("GitMonitor.onMoved\n");
690 this.lastAdd = new DateTime.now(new TimeZone.local());
691 var cmd_s = new GitMonitorQueue(src);
693 var cmd_d = new GitMonitorQueue(dest);
696 if (cmd_d.gitpath != cmd_s.gitpath) {
698 this.onCreated(dest);
699 this.onChangesDoneHint(dest);
702 // needs to handle move to/from unsupported types..
704 if (cmd_s.shouldIgnore()) {
705 this.onCreated(dest);
706 this.onChangesDoneHint(dest);
710 if (cmd_d.shouldIgnore()) {
719 print("RM: %s\n", cmd_s.vname);
721 this.queue.append_val(cmd_s);
725 print("ADD: %s\n", cmd_d.vname);
726 cmd_d.action = "add";
727 this.queue.append_val(cmd_d);
730 var cmd = new GitMonitorQueue(dest);
731 cmd.action = "commit";
732 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
733 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
734 cmd.message = cmd_d.vname;
737 this.queue.append_val(cmd);