3 public class GitMonitorQueue : MonitorNamePathDir {
9 public string vpath; // relative path (with in git)
10 public string message ; // for commit
11 public bool commit_all;
17 public GitMonitorQueue(MonitorNamePathDir f) {
19 base(f.name, f.path, f.dir);
23 this.commit_all = false;
26 var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
28 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
31 for (var i = 1; i< vpath_ar.length; i++) {
34 this.vpath = string.joinv("/", vpath);
35 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
40 public bool shouldIgnore(GitMonitor gm)
45 // vim.. what a seriously brain dead program..
46 if (this.name == "4913") {
50 if (this.name[0] == '.') {
52 if (this.name == ".htaccess") {
58 //if (f.name.match(/~$/)) {
61 //if (f.name.match(/^nbproject/)) {
64 // ignore anything in top level!!!!
65 if (this.vpath.length < 1) {
74 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
75 for(var i =0; i < add_files.length; i++) {
76 if (add_files.index(i).add == add) {
82 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
83 for(var i =0; i < messages.length; i++) {
84 if (messages.index(i).message == message) {
90 public static string messageToString(Array<GitMonitorQueue> messages ) {
92 for(var i =0; i < messages.length; i++) {
93 ret+= messages.index(i).message;
95 return string.joinv("\n",ret);
102 public class GitMonitor : Monitor
107 * @property {String} the "gitlive" directory, normally ~/gitlive
108 * dset by OWNER... - we should do this as a CTOR.
111 public static string gitlive = "";
114 public Array<GitMonitorQueue> queue ;
115 public bool queueRunning = false;
117 public DateTime lastAdd;
120 public void pause() {
122 // what does this do to the old one...
123 this.queue = new Array<GitMonitorQueue> ();
124 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
128 public void resume () {
130 this.queue = new Array<GitMonitorQueue> ();
131 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
136 * Start the monitoring
137 * and run the queue every 500 milliseconds..
140 public void start() {
141 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
144 this.lastAdd = new DateTime.now();
146 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
148 // call this.monitor on each of 'top'
149 for(int i = 0; i < this.top.length ; i++) {
150 this.monitor(this.top.index(i) );
152 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
157 var notification = new Notify.Notification({
159 GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
163 notification.set_timeout(5);
171 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
172 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
173 if (!_this.queue.length || _this.queueRunning) {
177 var last = this.lastAdd.difference(new DateTime.now());
180 //print("LAST RUN?" + last);
182 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
185 //_this.lastAdd = new Date();
197 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
202 public void monitor (string path, int depth = 0)
205 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
208 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
209 if (depth > 1 && GLib.file_test(path + "/.git" , GLib.FileTest.IS_DIR)) {
214 // FIXME - check if repo is flagged as not autocommit..
215 //var repo = imports.Scm.Repo.Repo.get(path);
216 //if (!repo || !repo.autocommit()) {
222 // check if the repo is to be monitored.
223 //print("PATH : " + path);
226 base.monitor(path, depth);
233 * - pulls the items off the queue
234 * (as commands run concurrently and new items may get added while it's running)
235 * - runs the queue items
239 public void runQueue()
245 this.queueRunning = true;
247 var cmds = new Array<GitMonitorQueue>();
248 for(var i = 0; i < this.queue.length; i++) {
249 cmds.append_val(this.queue.item(i));
252 this.queue = new Array<GitMonitorQueue>();// empty queue!
255 string[] success = {};
256 string[] failure = {};
257 var repos = new Array<GitRepo>(); //??
258 var done = new Array<GitMonitorQueue>();
260 // first build a array of repo's to work with
261 var repo_list = new Array<GitRepo>();
265 //print(JSON.stringify(cmds));
268 for(var i = 0; i < cmds.length; i++) {
271 var gitpath = cmd.gitpath;
272 var ix = GitRepo.indexOf(this.repos, cmd.gitpath);
274 repo_list.append_val(new GitRepo( gitpath ));
275 ix = GitRepo.indexOf(this.repos, cmd.gitpath);
279 //if (typeof(repo_list[gitpath]) == 'undefined') {
280 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
281 // repo_list[gitpath].cmds = [];
282 // repo_list[gitpath].pull();
284 repo_list.item(ix).cmds.append_val(cmd);
288 // build add, remove and commit message list..
290 for(var i = 0;i < repo_list.length;i++) {
293 var repo = repo_list.item(i);
295 var add_files = new Array<GitMonitorQueue>();
296 var remove_files = new Array<GitMonitorQueue>();
297 var messages = new Array<GitMonitorQueue>();
298 //print(JSON.stringify(repo.cmds,null,4));
300 for(var ii = 0;ii < repo.cmds.length;ii++) {
301 var cmd = repo.cmds.item(ii);
307 if (GitMonitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
312 add_files.append_val(cmd);
316 if (GitMonitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
320 // if file exists, do not try and delete it.
321 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
325 remove_files.append_val(cmd);
329 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
333 messages.append_val(cmd);
337 print("Opps unmatched action");
343 // these can fail... at present... as we wildcard stuff.
344 stdout.printf("ADD : %d files" , add_files.length);
346 // make sure added files do not get removed..
348 var remove_files_f = new Array<GitMonitorQueue>();
349 for(var ii = 0;ii < remove_files.length;ii++) {
350 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.item(ii).rm) > -1 ) {
353 remove_files_f.append_val(remove_files.item(ii));
355 stdout.printf("REMOVE : %d files" , remove_files.length);
357 // make sure monitoring is paused so it does not recursively pick up
363 repo.remove(remove_files_f);
368 success += repo.commit(
369 GitMonitorQueue.messageToString(messages),
372 success += repo.push();
375 failure += e.message;
380 // finally merge all the commit messages.
383 // catch notification failures.. so we can carry on..
384 if (success.length) {
387 var notification = new Notify.Notification(
389 string.joinv("\n",success),
394 notification.set_timeout(5);
398 if (failure.length) {
400 var notification = new Notify.Notification({
401 summary: "Git Live ERROR!!",
402 string.joinv("\n",failure),
407 notification.set_timeout(5); // show errros for longer
414 this.queueRunning = false;
421 //string[] just_created;
430 public void onChanged(MonitorNamePathDir src)
432 return; // always ignore this..?
433 //this.parsePath(src);
439 * results in git add + git commit..
442 public void onChangesDoneHint(MonitorNamePathDir src)
450 this.lastAdd = new DateTime.now();
451 var cmd = new GitMonitorQueue(src);
452 if (cmd.shouldIgnore()) {
459 if (this.is_just_created(cmd.path)) {
461 if (typeof(this.just_created[src.path]) !='undefined') {
462 delete this.just_created[src.path];
465 [ src.gitpath, 'add', src.vpath ],
466 [ src.gitpath, 'commit', { message: src.vpath} ]
474 this.queue.append_val(cmd);
476 var cmd = new GitMonitorQueue(src);
477 cmd.action = "commit";
478 cmd.message = src.vpath;
479 this.queue.append_val(cmd);
483 public void onDeleted(MonitorNamePathDir src)
488 this.lastAdd = new DateTime.now();
489 var cmd = new GitMonitorQueue(src);
490 if (cmd.shouldIgnore()) {
493 // should check if monitor needs removing..
494 // it should also check if it was a directory.. - so we dont have to commit all..
497 this.queue.append_val(cmd);
499 var cmd = new GitMonitorQueue(src);
500 cmd.action = "commit";
501 cmd.message = src.vpath;
502 cmd.commit_all = true;
504 this.queue.append_val(cmd);
507 public void onCreated(MonitorNamePathDir src) {
512 this.lastAdd = new DateTime.now();
513 var cmd = new GitMonitorQueue(src);
514 if (cmd.shouldIgnore()) {
518 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
519 // this.just_created[src.path] = true;
520 return; // we do not handle file create flags... - use done hint.
522 // directory has bee created
523 this.monitor(src.path);
524 this.top.append_val(src.path);
525 this.monitor(src.path );
528 // -- no point in adding a dir.. as git does not handle them...
530 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
531 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
537 public void onAttributeChanged(MonitorNamePathDir src) {
542 this.lastAdd = new DateTime.now();
543 var cmd = new GitMonitorQueue(src);
544 if (cmd.shouldIgnore()) {
549 this.queue.append_val(cmd);
551 var cmd = new GitMonitorQueue(src);
552 cmd.action = "commit";
553 cmd.message = "Attribute changed " + cmd.vpath;
554 this.queue.append_val(cmd);
558 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
560 this.lastAdd = new DateTime.now();
561 var cmd_s = new GitMonitorQueue(src);
563 var cmd_d = new GitMonitorQueue(src);
566 if (cmd_d.gitpath != cmd_s.gitpath) {
568 this.onCreated(dest);
569 this.onChangedDoneHint(dest);
572 // needs to handle move to/from unsupported types..
574 if (this.shouldIgnore(src)) {
575 this.onCreated(dest);
576 this.onChangedDoneHint(dest);
580 if (this.shouldIgnore(dest)) {
589 cmd_s.rm = src.vpath;
590 this.queue.append_val(cmd_s);
595 cmd_d.action = "add";
596 cmd_d.add = src.vpath;
597 this.queue.append_val(cmd_d);
600 var cmd = new GitMonitorQueue(dest);
601 cmd.action = "commit";
602 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
603 this.queue.append_val(cmd);