3 public class GitMonitorQueue : MonitorNamePathDir {
10 public string message ; // for commit
11 public bool commit_all;
14 public GitMonitorQueue(MonitorNamePathDir f) {
16 base(f.name, f.path, f.dir);
20 this.commit_all = false;
23 var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
25 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
28 for (var i = 1; i< vpath_ar.length; i++) {
31 this.vpath = string.joinv("/", vpath);
32 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
37 public bool shouldIgnore(GitMonitor gm)
42 // vim.. what a seriously brain dead program..
43 if (this.name == "4913") {
47 if (this.name[0] == '.') {
49 if (this.name == ".htaccess") {
55 //if (f.name.match(/~$/)) {
58 //if (f.name.match(/^nbproject/)) {
61 // ignore anything in top level!!!!
62 if (!this.vpath.length) {
71 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
72 for(var i =0; i < add_files.length; i++) {
73 if (addfiles.index(i).add == add) {
79 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
80 for(var i =0; i < messages.length; i++) {
81 if (messages.index(i).message == message) {
87 public static string messageToString(Array<GitMonitorQueue> messages ) {
89 for(var i =0; i < messages.length; i++) {
90 ret+= messages.index(i).message;
92 return string.joinv(ret,"\n");
99 public class GitMonitor : Monitor
104 * @property {String} the "gitlive" directory, normally ~/gitlive
105 * dset by OWNER... - we should do this as a CTOR.
108 public static string gitlive = "";
111 public Array<GitMonitorQueue> queue ;
112 public bool queueRunning = false;
114 public DateTime lastAdd;
117 public void pause() {
119 // what does this do to the old one...
120 this.queue = new Array<GitMonitorQueue> ();
121 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
125 public void resume () {
127 this.queue = new Array<GitMonitorQueue> ();
128 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
133 * Start the monitoring
134 * and run the queue every 500 milliseconds..
137 public void start() {
138 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
141 this.lastAdd = new DateTime.now();
143 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
145 // call this.monitor on each of 'top'
146 for(int i = 0; i < this.top.length ; i++) {
147 this.monitor(this.top.index(i) );
149 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
154 var notification = new Notify.Notification({
156 GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
160 notification.set_timeout(5);
168 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
169 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
170 if (!_this.queue.length || _this.queueRunning) {
174 var last = this.lastAdd.difference(new DateTime.now());
177 //print("LAST RUN?" + last);
179 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
182 //_this.lastAdd = new Date();
194 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
199 public void monitor (string path, int depth = 0)
202 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
205 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
206 if (depth > 1 && GLib.file_test(path + "/.git" , GLib.FileTest.IS_DIR)) {
211 // FIXME - check if repo is flagged as not autocommit..
212 //var repo = imports.Scm.Repo.Repo.get(path);
213 //if (!repo || !repo.autocommit()) {
219 // check if the repo is to be monitored.
220 //print("PATH : " + path);
223 base.monitor(path, depth);
230 * - pulls the items off the queue
231 * (as commands run concurrently and new items may get added while it's running)
232 * - runs the queue items
236 public void runQueue()
242 this.queueRunning = true;
244 var cmds = new Array<GitMonitorQueue>();
245 for(var i = 0; i < this.queue.length; i++) {
246 cmds.append_val(this.queue.item(i));
249 this.queue = new Array<GitMonitorQueue>();// empty queue!
252 string[] success = {};
253 string[] failure = {};
254 var repos = new Array<GitRepo>(); //??
255 var done = new Array<GitMonitorQueue>();
257 // first build a array of repo's to work with
258 var repo_list = new Array<GitRepo>();
262 //print(JSON.stringify(cmds));
265 for(var i = 0; i < cmds.length; i++) {
268 var gitpath = cmd.gitpath;
269 var ix = GitRepo.indexOf(this.repos, cmd.gitpath);
271 repo_list.append_val(new GitRepo( gitpath ));
272 ix = GitRepo.indexOf(this.repos, cmd.gitpath);
276 //if (typeof(repo_list[gitpath]) == 'undefined') {
277 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
278 // repo_list[gitpath].cmds = [];
279 // repo_list[gitpath].pull();
281 repo_list.item(ix).cmds.append_val(cmd);
285 // build add, remove and commit message list..
287 for(var i = 0;i < repo_list.length;i++) {
290 var repo = repo_list.item(i);
292 var add_files = new Array<GitMonitorQueue>();
293 var remove_files = new Array<GitMonitorQueue>();
294 var messages = new Array<GitMonitorQueue>();
295 //print(JSON.stringify(repo.cmds,null,4));
297 for(var ii = 0;ii < repo.cmds.length;ii++) {
298 var cmd = repo.cmds.item(ii);
304 if (GitMonitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
309 add_files.append_val(cmd);
313 if (GitMonitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
317 // if file exists, do not try and delete it.
318 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
322 remove_files.append_val(cmd);
326 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
330 messages.append_val(cmd);
334 print("Opps unmatched action");
340 // these can fail... at present... as we wildcard stuff.
341 stdout.printf("ADD : %d files" , add_files.length);
343 // make sure added files do not get removed..
345 var remove_files_f = new Array<GitMonitorQueue>();
346 for(var ii = 0;ii < remove_files.length;ii++) {
347 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.item(ii).rm) > -1 ) {
350 remove_files_f.append_val(remove_files.item(ii));
352 stdout.printf("REMOVE : %d files" , remove_files.length);
354 // make sure monitoring is paused so it does not recursively pick up
360 repo.remove(remove_files_f);
365 success += repo.commit(
366 GitMonitorQueue.messageToString(messages),
369 success += repo.push();
372 failure += e.message;
377 // finally merge all the commit messages.
380 // catch notification failures.. so we can carry on..
381 if (success.length) {
384 var notification = new Notify.Notification(
386 string.joinv("\n",success),
391 notification.set_timeout(5);
395 if (failure.length) {
397 var notification = new Notify.Notification({
398 summary: "Git Live ERROR!!",
399 string.joinv("\n",failure),
404 notification.set_timeout(5); // show errros for longer
411 this.queueRunning = false;
418 //string[] just_created;
427 public void onChanged(MonitorNamePathDir src)
429 return; // always ignore this..?
430 //this.parsePath(src);
436 * results in git add + git commit..
439 public void onChangesDoneHint(MonitorNamePathDir src)
447 this.lastAdd = new DateTime.now();
448 var cmd = new GitMonitorQueue(src);
449 if (cmd.shouldIgnore()) {
456 if (this.is_just_created(cmd.path)) {
458 if (typeof(this.just_created[src.path]) !='undefined') {
459 delete this.just_created[src.path];
462 [ src.gitpath, 'add', src.vpath ],
463 [ src.gitpath, 'commit', { message: src.vpath} ]
471 this.queue.append_val(cmd);
473 var cmd = new GitMonitorQueue(src);
474 cmd.action = "commit";
475 cmd.message = src.vpath;
476 this.queue.append_val(cmd);
480 public void onDeleted(MonitorNamePathDir src)
485 this.lastAdd = new DateTime.now();
486 var cmd = new GitMonitorQueue(src);
487 if (cmd.shouldIgnore()) {
490 // should check if monitor needs removing..
491 // it should also check if it was a directory.. - so we dont have to commit all..
494 this.queue.append_val(cmd);
496 var cmd = new GitMonitorQueue(src);
497 cmd.action = "commit";
498 cmd.message = src.vpath;
499 cmd.commit_all = true;
501 this.queue.append_val(cmd);
504 public void onCreated(MonitorNamePathDir src) {
509 this.lastAdd = new DateTime.now();
510 var cmd = new GitMonitorQueue(src);
511 if (cmd.shouldIgnore()) {
515 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
516 // this.just_created[src.path] = true;
517 return; // we do not handle file create flags... - use done hint.
519 // directory has bee created
520 this.monitor(src.path);
521 this.top.append_val(src.path);
522 this.monitor(src.path );
525 // -- no point in adding a dir.. as git does not handle them...
527 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
528 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
534 public void onAttributeChanged(MonitorNamePathDir src) {
539 this.lastAdd = new DateTime.now();
540 var cmd = new GitMonitorQueue(src);
541 if (cmd.shouldIgnore()) {
546 this.queue.append_val(cmd);
548 var cmd = new GitMonitorQueue(src);
549 cmd.action = "commit";
550 cmd.message = "Attribute changed " + cmd.vpath;
551 this.queue.append_val(cmd);
555 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
557 this.lastAdd = new DateTime.now();
558 var cmd_s = new GitMonitorQueue(src);
560 var cmd_d = new GitMonitorQueue(src);
563 if (cmd_d.gitpath != cmd_s.gitpath) {
565 this.onCreated(dest);
566 this.onChangedDoneHint(dest);
569 // needs to handle move to/from unsupported types..
571 if (this.shouldIgnore(src)) {
572 this.onCreated(dest);
573 this.onChangedDoneHint(dest);
577 if (this.shouldIgnore(dest)) {
586 cmd_s.rm = src.vpath;
587 this.queue.append_val(cmd_s);
592 cmd_d.action = "add";
593 cmd_d.add = src.vpath;
594 this.queue.append_val(cmd_d);
597 var cmd = new GitMonitorQueue(dest);
598 cmd.action = "commit";
599 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
600 this.queue.append_val(cmd);