3 public class GitMontitorQueue : MonitorNamePathDir {
10 public string message ; // for commit
11 public bool commit_all;
14 public GitMontitorQueue(MonitorNamePathDir f) {
19 this.commit_all = false;
22 var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
24 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
27 for (var i = 1; i< vpath_ar.length; i++) {
30 this.vpath = string.joinv("/", vpath);
31 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
36 public bool shouldIgnore(GitMonitor gm)
41 // vim.. what a seriously brain dead program..
42 if (this.name == "4913") {
46 if (this.name[0] == ".") {
48 if (this.name == ".htaccess") {
54 //if (f.name.match(/~$/)) {
57 //if (f.name.match(/^nbproject/)) {
60 // ignore anything in top level!!!!
61 if (!this.vpath.length) {
70 public static int indexOfAdd( Array<GtkMonitorQueue> add_files, string add) {
71 for(var i =0; i < add_files.length; i++) {
72 if (addfiles.index(i).add == add) {
78 public static int indexOfMessage(Array<GtkMonitorQueue> messages, string message) {
79 for(var i =0; i < messages.length; i++) {
80 if (messages.index(i).message == message) {
86 public static string messageToString(Array<GtkMonitorQueue> messages ) {
88 for(var i =0; i < messages.length; i++) {
89 ret+= messages.index(i).message;
91 return string.joinv(ret,"\n");
98 public class GitMonitor : Monitor
103 * @property {String} the "gitlive" directory, normally ~/gitlive
104 * dset by OWNER... - we should do this as a CTOR.
107 public static string gitlive = "";
110 public Array<GtkMonitorQueue> queue ;
111 public bool queueRunning = false;
113 public DateTime lastAdd;
116 public void pause() {
118 // what does this do to the old one...
119 this.queue = new Array<FileMonitor> ();
120 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
124 public void resume () {
126 this.queue = new Array<FileMonitor> ();
127 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
132 * Start the monitoring
133 * and run the queue every 500 milliseconds..
136 public void start() {
137 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
140 this.lastAdd = new DateTime.now();
142 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
144 // call this.monitor on each of 'top'
145 for(int i = 0; i < this.top.length ; i++) {
146 this.monitor(this.top.index(i) );
148 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
153 var notification = new Notify.Notification({
155 GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
159 notification.set_timeout(5);
167 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
168 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
169 if (!_this.queue.length || _this.queueRunning) {
173 var last = this.lastAdd.difference(new DateTime.now());
176 //print("LAST RUN?" + last);
178 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
181 //_this.lastAdd = new Date();
193 StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
198 public void monitor (string path, int depth = 0)
201 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
204 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
205 if (depth > 1 && GLib.file_test(path + "/.git" , GLib.FileTest.IS_DIR)) {
210 // FIXME - check if repo is flagged as not autocommit..
211 //var repo = imports.Scm.Repo.Repo.get(path);
212 //if (!repo || !repo.autocommit()) {
218 // check if the repo is to be monitored.
219 //print("PATH : " + path);
222 base.monitor(path, depth);
229 * - pulls the items off the queue
230 * (as commands run concurrently and new items may get added while it's running)
231 * - runs the queue items
235 public void runQueue()
241 this.queueRunning = true;
243 var cmds = new Array<GitMontitorQueue>();
244 for(var i = 0; i < this.queue.length; i++) {
245 cmds.append_val(this.queue.item(i));
248 this.queue = new Array<GitMontitorQueue>();// empty queue!
251 var success = new Array<String>();
252 var failure = new Array<GitMontitorQueue>();
253 var repos = new Array<GitRepo>(); //??
254 var done = new Array<GitMontitorQueue>();
256 // first build a array of repo's to work with
257 var repo_list = new Array<GitRepo>();
261 //print(JSON.stringify(cmds));
264 for(var i = 0; i < cmds.length; i++) {
267 var gitpath = cmd.gitpath;
268 var ix = GitRepo.indexOf(this.repos, cmd.gitpath);
270 repo_list.append_val(new GitRepo( gitpath ));
271 ix = GitRepo.indexOf(this.repos, cmd.gitpath);
275 //if (typeof(repo_list[gitpath]) == 'undefined') {
276 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
277 // repo_list[gitpath].cmds = [];
278 // repo_list[gitpath].pull();
280 repo_list.item(ix).cmds.append_val(cmd);
284 // build add, remove and commit message list..
286 for(var i = 0;i < repo_list.length;i++) {
289 var repo = repo_list.item(i);
291 var add_files = new Array<GitMontitorQueue>();
292 var remove_files = new Array<GitMontitorQueue>();
293 var messages = new Array<GitMontitorQueue>();
294 //print(JSON.stringify(repo.cmds,null,4));
296 for(var ii = 0;ii < repo.cmds.length;ii++) {
297 var cmd = repo.cmds.item(ii);
303 if (GitMontitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
308 add_files.append_val(cmd);
312 if (GitMontitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
316 // if file exists, do not try and delete it.
317 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
321 remove_files.append_val(cmd);
325 if (GitMontitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
329 messages.append_val(cmd);
333 print("Opps unmatched action");
339 // these can fail... at present... as we wildcard stuff.
340 stdout.printf("ADD : %d files" , add_files.length);
342 // make sure added files do not get removed..
344 var remove_files_f = new Array<GitMontitorQueue>();
345 for(var ii = 0;ii < remove_files.length;ii++) {
346 if (GitMontitorQueue.indexOfAdd(add_files, remove_files.item(ii).rm) > -1 ) {
349 remove_files_f.append_val(remove_files.item(ii));
351 stdout.printf("REMOVE : %d files" , remove_files.length);
353 // make sure monitoring is paused so it does not recursively pick up
359 repo.remove(remove_files_f);
364 success.append_val(repo.commit(
365 GitMontitorQueue.messageToString(messages),
368 success.push(repo.push());
371 failure.append_val(e.message);
376 // finally merge all the commit messages.
379 // catch notification failures.. so we can carry on..
380 if (success.length) {
381 var success_str = "";
382 for(var ii = 0;ii < success.length;ii++) {
383 success_str+= success.item(ii) + "\n";
386 var notification = new Notify.Notification(
393 notification.set_timeout(5);
397 if (failure.length) {
398 var failure_str = "";
399 for(var ii = 0;ii < failure.length;ii++) {
400 failure_str+= failure.item(ii) + "\n";
402 var notification = new Notify.Notification({
403 summary: "Git Live ERROR!!",
409 notification.set_timeout(5); // show errros for longer
416 this.queueRunning = false;
423 //string[] just_created;
432 public void onChanged(MonitorNamePathDir src)
434 return; // always ignore this..?
435 //this.parsePath(src);
441 * results in git add + git commit..
444 public void onChangesDoneHint(MonitorNamePathDir src)
452 this.lastAdd = new DateTime.now();
453 var cmd = new GitMontitorQueue(src);
454 if (cmd.shouldIgnore()) {
461 if (this.is_just_created(cmd.path)) {
463 if (typeof(this.just_created[src.path]) !='undefined') {
464 delete this.just_created[src.path];
467 [ src.gitpath, 'add', src.vpath ],
468 [ src.gitpath, 'commit', { message: src.vpath} ]
476 this.queue.append_val(cmd);
478 var cmd = new GitMontitorQueue(src);
479 cmd.action = "commit";
480 cmd.message = src.vpath;
481 this.queue.append_val(cmd);
485 public void onDeleted(MonitorNamePathDir src)
490 this.lastAdd = new DateTime.now();
491 var cmd = new GitMontitorQueue(src);
492 if (cmd.shouldIgnore()) {
495 // should check if monitor needs removing..
496 // it should also check if it was a directory.. - so we dont have to commit all..
499 this.queue.append_val(cmd);
501 var cmd = new GitMontitorQueue(src);
502 cmd.action = "commit";
503 cmd.message = src.vpath;
504 cmd.commit_all = true;
506 this.queue.append_val(cmd);
509 public void onCreated(MonitorNamePathDir src) {
514 this.lastAdd = new DateTime.now();
515 var cmd = new GitMontitorQueue(src);
516 if (cmd.shouldIgnore()) {
520 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
521 // this.just_created[src.path] = true;
522 return; // we do not handle file create flags... - use done hint.
524 // directory has bee created
525 this.monitor(src.path);
526 this.top.append_val(src.path);
527 this.monitor(src.path );
530 // -- no point in adding a dir.. as git does not handle them...
532 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
533 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
539 public void onAttributeChanged(MonitorNamePathDir src) {
544 this.lastAdd = new DateTime.now();
545 var cmd = new GitMontitorQueue(src);
546 if (cmd.shouldIgnore()) {
551 this.queue.append_val(cmd);
553 var cmd = new GitMontitorQueue(src);
554 cmd.action = "commit";
555 cmd.message = "Attribute changed " + cmd.vpath;
556 this.queue.append_val(cmd);
560 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
562 this.lastAdd = new DateTime.now();
563 var cmd_s = new GitMontitorQueue(src);
565 var cmd_d = new GitMontitorQueue(src);
568 if (cmd_d.gitpath != cmd_s.gitpath) {
570 this.onCreated(dest);
571 this.onChangedDoneHint(dest);
574 // needs to handle move to/from unsupported types..
576 if (this.shouldIgnore(src)) {
577 this.onCreated(dest);
578 this.onChangedDoneHint(dest);
582 if (this.shouldIgnore(dest)) {
591 cmd_s.rm = src.vpath;
592 this.queue.append_val(cmd_s);
597 cmd_d.action = "add";
598 cmd_d.add = src.vpath;
599 this.queue.append_val(cmd_d);
602 var cmd = new GitMontitorQueue(dest);
603 cmd.action = "commit";
604 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
605 this.queue.append_val(cmd);