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 GitMonitorQueue.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) {
148 public class GitMonitor : Monitor
151 public static GitMonitor gitmonitor;
153 * @property {String} the "gitlive" directory, normally ~/gitlive
154 * dset by OWNER... - we should do this as a CTOR.
157 public static string gitlive;
160 public Array<GitMonitorQueue> queue ;
161 public bool queueRunning = false;
163 public DateTime lastAdd;
166 public GitMonitor () {
167 this.queue = new Array<GitMonitorQueue>();
168 GitMonitor.gitmonitor = this;
174 public new void pause() {
176 // what does this do to the old one...
177 this.queue = new Array<GitMonitorQueue> ();
178 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
182 public new void resume () {
184 this.queue = new Array<GitMonitorQueue> ();
185 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
190 * Start the monitoring
191 * and run the queue every 500 milliseconds..
194 public new void start() {
195 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
198 this.lastAdd = new DateTime.now(new TimeZone.local());
200 Timeout.add_full(Priority.LOW, 500, () => {
201 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
202 // call this.monitor on each of 'top'
203 for(int i = 0; i < this.top.length ; i++) {
205 this.monitor(this.top.index(i) );
207 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
214 var notification = new Notify.Notification(
216 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
220 notification.set_timeout(5);
225 return false; // do not keep doing this..
230 Timeout.add_full(Priority.LOW, 1000, () => {
231 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
233 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
234 if (this.queue.length < 1 || this.queueRunning) {
238 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
240 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
241 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
243 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
246 //_this.lastAdd = new Date();
256 public new void stop() {
257 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
262 public new void monitor (string path, int depth = 0)
265 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
268 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
269 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
274 // FIXME - check if repo is flagged as not autocommit..
275 //var repo = imports.Scm.Repo.Repo.get(path);
276 //if (!repo || !repo.autocommit()) {
282 // check if the repo is to be monitored.
283 //print("PATH : " + path);
286 base.monitor(path, depth);
293 * - pulls the items off the queue
294 * (as commands run concurrently and new items may get added while it's running)
295 * - runs the queue items
299 public void runQueue()
305 print("GitMonitor.runQueue\n");
307 this.queueRunning = true;
309 var cmds = new Array<GitMonitorQueue>();
311 for(var i = 0; i < this.queue.length; i++) {
312 cmds.append_val(this.queue.index(i));
315 this.queue = new Array<GitMonitorQueue>();// empty queue!
318 string[] success = {};
319 string[] failure = {};
320 //var repos = new Array<GitRepo>(); //??
321 //var done = new Array<GitMonitorQueue>();
323 // first build a array of repo's to work with
324 var repo_list = new Array<GitRepo>();
328 //print(JSON.stringify(cmds));
329 // make sure nothing get's added to the queue where we are doing this..
333 print("GitMonitor.runQueue - creating repos\n");
335 for(var i = 0; i < cmds.length; i++) {
337 var cmd = cmds.index(i);
339 var gitpath = cmd.gitpath;
340 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
342 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
344 repo_list.append_val(new GitRepo( gitpath ));
345 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
347 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
349 //if (typeof(repo_list[gitpath]) == 'undefined') {
350 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
351 // repo_list[gitpath].cmds = [];
352 // repo_list[gitpath].pull();
354 repo_list.index(ix).cmds.append_val(cmd);
358 // build add, remove and commit message list..
360 print("GitMonitor.runQueue - creating actions\n");
362 for(var i = 0;i < repo_list.length;i++) {
364 var repo = repo_list.index(i);
366 var add_files = new Array<GitMonitorQueue>();
367 var add_files_f = new Array<GitMonitorQueue>();
368 var remove_files = new Array<GitMonitorQueue>();
369 var messages = new Array<GitMonitorQueue>();
370 //print(JSON.stringify(repo.cmds,null,4));
372 for(var ii = 0;ii < repo.cmds.length;ii++) {
373 var cmd = repo.cmds.index(ii);
379 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
383 add_files.append_val(cmd);
387 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
391 // if file exists, do not try and delete it.
392 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
396 remove_files.append_val(cmd);
400 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
403 messages.append_val(cmd);
407 stdout.printf("Opps unmatched action %s\n", cmd.action);
411 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
412 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
414 // these can fail... at present... as we wildcard stuff.
416 // make sure added files do not get removed.. ??
418 var remove_files_f = new Array<GitMonitorQueue>();
419 for(var ii = 0;ii < remove_files.length;ii++) {
420 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
423 remove_files_f.append_val(remove_files.index(ii));
425 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
428 // if file was added, then removed,
429 var remove_files_f = new Array<GitMonitorQueue>();
430 for(var ii = 0;ii < remove_files.length;ii++) {
431 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
432 // in add and remove - do not remvove
435 remove_files_f.append_val(remove_files.index(ii));
437 for(var ii = 0;ii < add_files.length;ii++) {
438 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
439 // in add and remove - do not remvove
442 add_files_f.append_val(add_files.index(ii));
445 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
446 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
449 // make sure monitoring is paused so it does not recursively pick up
454 failure += e.message;
460 repo.add(add_files_f);
462 failure += e.message;
465 repo.remove(remove_files_f);
467 failure += e.message;
474 success += repo.commit(
475 GitMonitorQueue.messageToString(messages),
478 success += repo.push();
481 failure += e.message;
486 // finally merge all the commit messages.
489 // catch notification failures.. so we can carry on..
490 if (success.length > 0) {
493 var notification = new Notify.Notification(
495 string.joinv("\n",success),
500 notification.set_timeout(5);
504 if (failure.length > 0) {
506 var notification = new Notify.Notification(
508 string.joinv("\n",failure),
513 notification.set_timeout(5); // show errros for longer
520 this.queueRunning = false;
527 //string[] just_created;
536 public override void onChanged(MonitorNamePathDir src)
538 print("GitMonitor.onChanged\n");
539 return; // always ignore this..?
540 //this.parsePath(src);
546 * results in git add + git commit..
549 public override void onChangesDoneHint(MonitorNamePathDir src)
551 print("GitMonitor.onChangedHint\n");
557 this.lastAdd = new DateTime.now(new TimeZone.local());
558 var cmd = new GitMonitorQueue(src);
559 if (cmd.shouldIgnore()) {
564 //var add_it = false;
566 if (this.is_just_created(cmd.path)) {
568 if (typeof(this.just_created[src.path]) !='undefined') {
569 delete this.just_created[src.path];
572 [ src.gitpath, 'add', src.vpath ],
573 [ src.gitpath, 'commit', { message: src.vpath} ]
581 this.queue.append_val(cmd);
583 cmd = new GitMonitorQueue(src);
584 cmd.action = "commit";
585 cmd.message = cmd.vname;
586 this.queue.append_val(cmd);
590 public override void onDeleted(MonitorNamePathDir src)
592 print("GitMonitor.onDeleted\n");
596 this.lastAdd = new DateTime.now(new TimeZone.local());
597 var cmd = new GitMonitorQueue(src);
598 if (cmd.shouldIgnore()) {
601 // should check if monitor needs removing..
602 // it should also check if it was a directory.. - so we dont have to commit all..
604 this.queue.append_val(cmd);
606 cmd = new GitMonitorQueue(src);
607 cmd.action = "commit";
608 cmd.message = cmd.vname;
609 cmd.commit_all = true;
611 this.queue.append_val(cmd);
614 public override void onCreated(MonitorNamePathDir src) {
615 print("GitMonitor.onCreated\n");
619 this.lastAdd = new DateTime.now(new TimeZone.local());
620 var cmd = new GitMonitorQueue(src);
621 if (cmd.shouldIgnore()) {
625 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
626 // this.just_created[src.path] = true;
627 return; // we do not handle file create flags... - use done hint.
629 // directory has bee created
630 this.monitor(src.path);
631 //this.top.append_val(src.path);
632 //this.monitor(src.path );
635 // -- no point in adding a dir.. as git does not handle them...
637 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
638 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
644 public override void onAttributeChanged(MonitorNamePathDir src) {
645 print("GitMonitor.onAttributeChanged\n");
649 if (src.dir == GitMonitor.gitlive) {
650 return; // attribute on top level..
653 this.lastAdd = new DateTime.now(new TimeZone.local());
654 var cmd = new GitMonitorQueue(src);
655 if (cmd.shouldIgnore()) {
659 this.queue.append_val(cmd);
661 cmd = new GitMonitorQueue(src);
662 cmd.action = "commit";
663 cmd.message = "Attribute changed " + cmd.vname;
664 this.queue.append_val(cmd);
667 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
669 print("GitMonitor.onMoved\n");
673 this.lastAdd = new DateTime.now(new TimeZone.local());
674 var cmd_s = new GitMonitorQueue(src);
676 var cmd_d = new GitMonitorQueue(dest);
679 if (cmd_d.gitpath != cmd_s.gitpath) {
681 this.onCreated(dest);
682 this.onChangesDoneHint(dest);
685 // needs to handle move to/from unsupported types..
687 if (cmd_s.shouldIgnore()) {
688 this.onCreated(dest);
689 this.onChangesDoneHint(dest);
693 if (cmd_d.shouldIgnore()) {
702 print("RM: %s\n", cmd_s.vname);
704 this.queue.append_val(cmd_s);
708 print("ADD: %s\n", cmd_d.vname);
709 cmd_d.action = "add";
710 this.queue.append_val(cmd_d);
713 var cmd = new GitMonitorQueue(dest);
714 cmd.action = "commit";
715 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
716 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
717 cmd.message = cmd_d.vname;
720 this.queue.append_val(cmd);