3 public class GitMonitorQueue : MonitorNamePathDir {
10 public string message ; // for commit
11 public bool commit_all;
14 public GitMonitorQueue(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<GitMonitorQueue> 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<GitMonitorQueue> 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<GitMonitorQueue> 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<GitMonitorQueue> 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<GitMonitorQueue> ();
120 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
124 public void resume () {
126 this.queue = new Array<GitMonitorQueue> ();
127 StatusIconA.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 StatusIconA.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 StatusIconA.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 StatusIconA.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<GitMonitorQueue>();
244 for(var i = 0; i < this.queue.length; i++) {
245 cmds.append_val(this.queue.item(i));
248 this.queue = new Array<GitMonitorQueue>();// empty queue!
251 string[] success = {};
252 string[] failure = {};
253 var repos = new Array<GitRepo>(); //??
254 var done = new Array<GitMonitorQueue>();
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<GitMonitorQueue>();
292 var remove_files = new Array<GitMonitorQueue>();
293 var messages = new Array<GitMonitorQueue>();
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 (GitMonitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
308 add_files.append_val(cmd);
312 if (GitMonitorQueue.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 (GitMonitorQueue.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<GitMonitorQueue>();
345 for(var ii = 0;ii < remove_files.length;ii++) {
346 if (GitMonitorQueue.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 += repo.commit(
365 GitMonitorQueue.messageToString(messages),
368 success += repo.push();
371 failure += e.message;
376 // finally merge all the commit messages.
379 // catch notification failures.. so we can carry on..
380 if (success.length) {
383 var notification = new Notify.Notification(
385 string.joinv("\n",success),
390 notification.set_timeout(5);
394 if (failure.length) {
396 var notification = new Notify.Notification({
397 summary: "Git Live ERROR!!",
398 string.joinv("\n",failure),
403 notification.set_timeout(5); // show errros for longer
410 this.queueRunning = false;
417 //string[] just_created;
426 public void onChanged(MonitorNamePathDir src)
428 return; // always ignore this..?
429 //this.parsePath(src);
435 * results in git add + git commit..
438 public void onChangesDoneHint(MonitorNamePathDir src)
446 this.lastAdd = new DateTime.now();
447 var cmd = new GitMonitorQueue(src);
448 if (cmd.shouldIgnore()) {
455 if (this.is_just_created(cmd.path)) {
457 if (typeof(this.just_created[src.path]) !='undefined') {
458 delete this.just_created[src.path];
461 [ src.gitpath, 'add', src.vpath ],
462 [ src.gitpath, 'commit', { message: src.vpath} ]
470 this.queue.append_val(cmd);
472 var cmd = new GitMonitorQueue(src);
473 cmd.action = "commit";
474 cmd.message = src.vpath;
475 this.queue.append_val(cmd);
479 public void onDeleted(MonitorNamePathDir src)
484 this.lastAdd = new DateTime.now();
485 var cmd = new GitMonitorQueue(src);
486 if (cmd.shouldIgnore()) {
489 // should check if monitor needs removing..
490 // it should also check if it was a directory.. - so we dont have to commit all..
493 this.queue.append_val(cmd);
495 var cmd = new GitMonitorQueue(src);
496 cmd.action = "commit";
497 cmd.message = src.vpath;
498 cmd.commit_all = true;
500 this.queue.append_val(cmd);
503 public void onCreated(MonitorNamePathDir src) {
508 this.lastAdd = new DateTime.now();
509 var cmd = new GitMonitorQueue(src);
510 if (cmd.shouldIgnore()) {
514 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
515 // this.just_created[src.path] = true;
516 return; // we do not handle file create flags... - use done hint.
518 // directory has bee created
519 this.monitor(src.path);
520 this.top.append_val(src.path);
521 this.monitor(src.path );
524 // -- no point in adding a dir.. as git does not handle them...
526 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
527 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
533 public void onAttributeChanged(MonitorNamePathDir src) {
538 this.lastAdd = new DateTime.now();
539 var cmd = new GitMonitorQueue(src);
540 if (cmd.shouldIgnore()) {
545 this.queue.append_val(cmd);
547 var cmd = new GitMonitorQueue(src);
548 cmd.action = "commit";
549 cmd.message = "Attribute changed " + cmd.vpath;
550 this.queue.append_val(cmd);
554 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
556 this.lastAdd = new DateTime.now();
557 var cmd_s = new GitMonitorQueue(src);
559 var cmd_d = new GitMonitorQueue(src);
562 if (cmd_d.gitpath != cmd_s.gitpath) {
564 this.onCreated(dest);
565 this.onChangedDoneHint(dest);
568 // needs to handle move to/from unsupported types..
570 if (this.shouldIgnore(src)) {
571 this.onCreated(dest);
572 this.onChangedDoneHint(dest);
576 if (this.shouldIgnore(dest)) {
585 cmd_s.rm = src.vpath;
586 this.queue.append_val(cmd_s);
591 cmd_d.action = "add";
592 cmd_d.add = src.vpath;
593 this.queue.append_val(cmd_d);
596 var cmd = new GitMonitorQueue(dest);
597 cmd.action = "commit";
598 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
599 this.queue.append_val(cmd);