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);
122 public class GitMonitor : Monitor
125 public static GitMonitor gitmonitor;
127 * @property {String} the "gitlive" directory, normally ~/gitlive
128 * dset by OWNER... - we should do this as a CTOR.
131 public static string gitlive;
134 public Array<GitMonitorQueue> queue ;
135 public bool queueRunning = false;
137 public DateTime lastAdd;
140 public GitMonitor () {
141 this.queue = new Array<GitMonitorQueue>();
142 GitMonitor.gitmonitor = this;
148 public new void pause() {
150 // what does this do to the old one...
151 this.queue = new Array<GitMonitorQueue> ();
152 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
156 public new void resume () {
158 this.queue = new Array<GitMonitorQueue> ();
159 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
164 * Start the monitoring
165 * and run the queue every 500 milliseconds..
168 public new void start() {
169 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
172 this.lastAdd = new DateTime.now(new TimeZone.local());
174 Timeout.add_full(Priority.LOW, 500, () => {
175 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
176 // call this.monitor on each of 'top'
177 for(int i = 0; i < this.top.length ; i++) {
179 this.monitor(this.top.index(i) );
181 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
188 var notification = new Notify.Notification(
190 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
194 notification.set_timeout(5);
199 return false; // do not keep doing this..
204 Timeout.add_full(Priority.LOW, 1000, () => {
205 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
207 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
208 if (this.queue.length < 1 || this.queueRunning) {
212 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
214 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
215 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
217 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
220 //_this.lastAdd = new Date();
230 public new void stop() {
231 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
236 public new void monitor (string path, int depth = 0)
239 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
242 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
243 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
248 // FIXME - check if repo is flagged as not autocommit..
249 //var repo = imports.Scm.Repo.Repo.get(path);
250 //if (!repo || !repo.autocommit()) {
256 // check if the repo is to be monitored.
257 //print("PATH : " + path);
260 base.monitor(path, depth);
267 * - pulls the items off the queue
268 * (as commands run concurrently and new items may get added while it's running)
269 * - runs the queue items
273 public void runQueue()
279 print("GitMonitor.runQueue\n");
281 this.queueRunning = true;
283 var cmds = new Array<GitMonitorQueue>();
285 for(var i = 0; i < this.queue.length; i++) {
286 cmds.append_val(this.queue.index(i));
289 this.queue = new Array<GitMonitorQueue>();// empty queue!
292 string[] success = {};
293 string[] failure = {};
294 //var repos = new Array<GitRepo>(); //??
295 //var done = new Array<GitMonitorQueue>();
297 // first build a array of repo's to work with
298 var repo_list = new Array<GitRepo>();
302 //print(JSON.stringify(cmds));
303 // make sure nothing get's added to the queue where we are doing this..
307 print("GitMonitor.runQueue - creating repos\n");
309 for(var i = 0; i < cmds.length; i++) {
311 var cmd = cmds.index(i);
313 var gitpath = cmd.gitpath;
314 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
316 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
318 repo_list.append_val(new GitRepo( gitpath ));
319 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
321 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
323 //if (typeof(repo_list[gitpath]) == 'undefined') {
324 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
325 // repo_list[gitpath].cmds = [];
326 // repo_list[gitpath].pull();
328 repo_list.index(ix).cmds.append_val(cmd);
332 // build add, remove and commit message list..
334 print("GitMonitor.runQueue - creating actions\n");
336 for(var i = 0;i < repo_list.length;i++) {
338 var repo = repo_list.index(i);
340 var add_files = new Array<GitMonitorQueue>();
341 var add_files_f = new Array<GitMonitorQueue>();
342 var remove_files = new Array<GitMonitorQueue>();
343 var messages = new Array<GitMonitorQueue>();
344 //print(JSON.stringify(repo.cmds,null,4));
346 for(var ii = 0;ii < repo.cmds.length;ii++) {
347 var cmd = repo.cmds.index(ii);
353 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
357 add_files.append_val(cmd);
361 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
365 // if file exists, do not try and delete it.
366 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
370 remove_files.append_val(cmd);
374 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
377 messages.append_val(cmd);
381 stdout.printf("Opps unmatched action %s\n", cmd.action);
387 // these can fail... at present... as we wildcard stuff.
388 stdout.printf("ADD : %u files\n" , add_files.length);
390 // make sure added files do not get removed.. ??
392 var remove_files_f = new Array<GitMonitorQueue>();
393 for(var ii = 0;ii < remove_files.length;ii++) {
394 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
397 remove_files_f.append_val(remove_files.index(ii));
399 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
402 // if file was added, then removed,
403 var remove_files_f = new Array<GitMonitorQueue>();
404 for(var ii = 0;ii < remove_files.length;ii++) {
405 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
406 // in add and remove - do not remvove
409 remove_files_f.append_val(remove_files.index(ii));
411 for(var ii = 0;ii < add_files.length;ii++) {
412 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
413 // in add and remove - do not remvove
416 add_files_f.append_val(add_files.index(ii));
421 // make sure monitoring is paused so it does not recursively pick up
426 failure += e.message;
434 failure += e.message;
437 repo.remove(remove_files_f);
439 failure += e.message;
446 success += repo.commit(
447 GitMonitorQueue.messageToString(messages),
450 success += repo.push();
453 failure += e.message;
458 // finally merge all the commit messages.
461 // catch notification failures.. so we can carry on..
462 if (success.length > 0) {
465 var notification = new Notify.Notification(
467 string.joinv("\n",success),
472 notification.set_timeout(5);
476 if (failure.length > 0) {
478 var notification = new Notify.Notification(
480 string.joinv("\n",failure),
485 notification.set_timeout(5); // show errros for longer
492 this.queueRunning = false;
499 //string[] just_created;
508 public override void onChanged(MonitorNamePathDir src)
510 print("GitMonitor.onChanged\n");
511 return; // always ignore this..?
512 //this.parsePath(src);
518 * results in git add + git commit..
521 public override void onChangesDoneHint(MonitorNamePathDir src)
523 print("GitMonitor.onChangedHint\n");
529 this.lastAdd = new DateTime.now(new TimeZone.local());
530 var cmd = new GitMonitorQueue(src);
531 if (cmd.shouldIgnore()) {
536 //var add_it = false;
538 if (this.is_just_created(cmd.path)) {
540 if (typeof(this.just_created[src.path]) !='undefined') {
541 delete this.just_created[src.path];
544 [ src.gitpath, 'add', src.vpath ],
545 [ src.gitpath, 'commit', { message: src.vpath} ]
553 this.queue.append_val(cmd);
555 cmd = new GitMonitorQueue(src);
556 cmd.action = "commit";
557 cmd.message = cmd.vname;
558 this.queue.append_val(cmd);
562 public override void onDeleted(MonitorNamePathDir src)
564 print("GitMonitor.onDeleted\n");
568 this.lastAdd = new DateTime.now(new TimeZone.local());
569 var cmd = new GitMonitorQueue(src);
570 if (cmd.shouldIgnore()) {
573 // should check if monitor needs removing..
574 // it should also check if it was a directory.. - so we dont have to commit all..
576 this.queue.append_val(cmd);
578 cmd = new GitMonitorQueue(src);
579 cmd.action = "commit";
580 cmd.message = cmd.vname;
581 cmd.commit_all = true;
583 this.queue.append_val(cmd);
586 public override void onCreated(MonitorNamePathDir src) {
587 print("GitMonitor.onCreated\n");
591 this.lastAdd = new DateTime.now(new TimeZone.local());
592 var cmd = new GitMonitorQueue(src);
593 if (cmd.shouldIgnore()) {
597 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
598 // this.just_created[src.path] = true;
599 return; // we do not handle file create flags... - use done hint.
601 // directory has bee created
602 this.monitor(src.path);
603 //this.top.append_val(src.path);
604 //this.monitor(src.path );
607 // -- no point in adding a dir.. as git does not handle them...
609 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
610 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
616 public override void onAttributeChanged(MonitorNamePathDir src) {
617 print("GitMonitor.onAttributeChanged\n");
621 if (src.dir == GitMonitor.gitlive) {
622 return; // attribute on top level..
625 this.lastAdd = new DateTime.now(new TimeZone.local());
626 var cmd = new GitMonitorQueue(src);
627 if (cmd.shouldIgnore()) {
631 this.queue.append_val(cmd);
633 cmd = new GitMonitorQueue(src);
634 cmd.action = "commit";
635 cmd.message = "Attribute changed " + cmd.vname;
636 this.queue.append_val(cmd);
639 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
641 print("GitMonitor.onMoved\n");
645 this.lastAdd = new DateTime.now(new TimeZone.local());
646 var cmd_s = new GitMonitorQueue(src);
648 var cmd_d = new GitMonitorQueue(src);
651 if (cmd_d.gitpath != cmd_s.gitpath) {
653 this.onCreated(dest);
654 this.onChangesDoneHint(dest);
657 // needs to handle move to/from unsupported types..
659 if (cmd_s.shouldIgnore()) {
660 this.onCreated(dest);
661 this.onChangesDoneHint(dest);
665 if (cmd_d.shouldIgnore()) {
674 this.queue.append_val(cmd_s);
679 cmd_d.action = "add";
680 this.queue.append_val(cmd_d);
683 var cmd = new GitMonitorQueue(dest);
684 cmd.action = "commit";
685 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
686 this.queue.append_val(cmd);