10 static GitRepo _GitRepo;
12 public class GitRepo : Object
15 public Gee.ArrayList<GitMonitorQueue> cmds;
19 public string git_working_dir;
20 public bool debug = false;
22 public Gee.HashMap<string,bool> ignore_files;
23 public GitBranch currentBranch;
25 public RooTicket? activeTicket;
27 public static GitRepo singleton()
29 if (_GitRepo == null) {
30 _GitRepo = new GitRepo.single();
31 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
37 * index of.. matching gitpath..
39 public static int indexOf( Array<GitRepo> repos, string gitpath) {
40 // make a fake object to compare against..
41 var test_repo = GitRepo.get(gitpath);
43 for(var i =0; i < repos.length; i++) {
44 if (repos.index(i).gitdir == test_repo.gitdir) {
52 public Gee.HashMap<string,GitRepo> cache;
56 public static Array<GitRepo> list()
59 //if (GitRepo.list_cache != null) {
60 // unowned Array<GitRepo> ret = GitRepo.list_cache;
63 var cache = GitRepo.singleton().cache;
64 var list_cache = new Array<GitRepo>();
66 var dir = Environment.get_home_dir() + "/gitlive";
68 var f = File.new_for_path(dir);
69 FileEnumerator file_enum;
71 file_enum = f.enumerate_children(
72 FileAttribute.STANDARD_DISPLAY_NAME + ","+
73 FileAttribute.STANDARD_TYPE,
74 FileQueryInfoFlags.NONE,
87 next_file = file_enum.next_file(null);
88 if (next_file == null) {
93 GLib.debug("Error: %s",e.message);
97 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
99 if (next_file.get_file_type() != FileType.DIRECTORY) {
104 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
109 if (next_file.get_display_name()[0] == '.') {
113 var sp = dir+"/"+next_file.get_display_name();
115 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
117 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
121 var rep = GitRepo.get( sp );
122 list_cache.append_val(rep);
132 public static GitRepo get(string path)
134 var cache = GitRepo.singleton().cache;
135 if (cache.has_key(path)) {
136 return cache.get(path);
138 return new GitRepo(path);
141 private GitRepo.single() {
142 // used to create the signleton
147 * @param {Object} cfg - Configuration
148 * (basically repopath is currently only critical one.)
152 private GitRepo(string path) {
154 this.name = File.new_for_path(path).get_basename();
155 this.ignore_files = new Gee.HashMap<string,bool>();
157 this.git_working_dir = path;
158 this.gitdir = path + "/.git";
159 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
160 this.gitdir = path; // naked...
162 this.cmds = new Gee.ArrayList<GitMonitorQueue> ();
164 var cache = GitRepo.singleton().cache;
165 //Repo.superclass.constructor.call(this,cfg);
166 if ( !cache.has_key(path) ) {
167 cache.set( path, this);
170 this.loadActiveTicket();
173 public bool is_wip_branch()
175 return this.currentBranch.name.has_prefix("wip_");
179 public bool is_autocommit ()
181 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
183 public void set_autocommit(bool val)
186 var cur = this.is_autocommit();
187 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
189 return; // no change..
192 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
195 FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" );
200 public bool is_auto_branch ()
202 return FileUtils.test(this.gitdir + "/.gitlive-enable-auto-branch" , FileTest.EXISTS);
205 public void set_auto_branch(bool val)
208 var cur = this.is_auto_branch();
209 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
212 return; // no change..
215 FileUtils.set_contents(this.gitdir + "/.gitlive-enable-auto-branch" , "x");
218 FileUtils.remove(this.gitdir + "/.gitlive-enable-auto-branch" );
222 public bool is_autopush ()
224 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
226 public void set_autopush(bool val)
229 var cur = this.is_autopush();
230 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
232 return; // no change..
235 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
238 FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" );
245 Gee.HashMap<string,GitBranch> branches;
247 public void loadBranches()
249 this.branches = new Gee.HashMap<string,GitBranch>();
251 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
252 var res = this.git( cmd );
253 var lines = res.split("\n");
254 for (var i = 0; i < lines.length ; i++) {
255 var br = new GitBranch(this);
256 if (!br.parseBranchListItem(lines[i])) {
259 GLib.debug("add branch %s", br.realName());
261 branches.set(br.realName(), br);
263 this.currentBranch = br;
272 public string branchesToString()
275 foreach( var br in this.branches.values) {
279 ret += ret.length > 0 ? "," : "";
286 public static void doMerges(string action, string ticket_id, string commit_message)
288 GitMonitor.gitmonitor.stop();
292 foreach(var repo in GitRepo.singleton().cache.values) {
293 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
294 var res = repo.doMerge(action,ticket_id, commit_message);
299 commitrevs += commitrevs.length > 0 ? " " : "";
300 commitrevs += repo.currentBranch.lastrev;
303 if (sucess && action == "CLOSE") {
304 RooTicket.singleton().getById(ticket_id).close(commitrevs);
306 GitMonitor.gitmonitor.start();
310 public bool doMerge(string action, string ticket_id, string commit_message)
312 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
313 // also need to decide which branch we will merge into?
315 if (action == "CLOSE" || action == "LEAVE") {
318 var oldbranch = this.currentBranch.name;
319 this.setActiveTicket(null, "master");
320 string [] cmd = { "merge", "--squash", oldbranch };
322 cmd = { "commit", "-a" , "-m", commit_message };
325 this.loadBranches(); // updates lastrev..
327 var notification = new Notify.Notification(
328 "Merged branch %s to master".printf(oldbranch),
334 notification.set_timeout(5);
342 GitMonitor.gitmonitor.pauseError(e.message);
345 // error~?? -- show the error dialog...
348 if (action == "MASTER") {
349 // merge master into ours..
351 string[] cmd = { "merge", "master" };
353 var notification = new Notify.Notification(
354 "Merged code from master to %s".printf(this.currentBranch.name),
359 notification.set_timeout(5);
364 GitMonitor.gitmonitor.pauseError(e.message);
368 if (action == "EXIT") {
370 var oldbranch = this.currentBranch.name;
371 this.setActiveTicket(null, "master");
373 var notification = new Notify.Notification(
374 "Left branch %s".printf(oldbranch),
379 notification.set_timeout(5);
384 GitMonitor.gitmonitor.pauseError(e.message);
388 // error~?? -- show the error dialog...
394 public void loadActiveTicket()
396 this.activeTicket = null;
397 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
401 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
402 if (ticket_id.length < 1) {
405 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
412 public bool setActiveTicket(RooTicket? ticket, string branchname)
414 if (!this.createBranchNamed(branchname)) {
417 if (ticket != null) {
418 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
420 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
422 this.activeTicket = ticket;
426 public bool createBranchNamed(string branchname)
430 if (this.branches.has_key(branchname)) {
431 // this is where it get's tricky...
433 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
434 var ret = this.git(cmd);
435 stash = ret.length> 1 ;
439 if (stash) { this.git(cmd); }
441 cmd = { "checkout", branchname };
444 GitMonitor.gitmonitor.pauseError(e.message);
448 if (branchname != "master") {
449 string[] cmd = { "merge", "master" };
452 //cmd = { "commit", "-a" , "-m", "merge master changes" };
453 //git chethis.git( cmd );
457 string[] cmd = { "checkout", "master" };
459 GitMonitor.gitmonitor.pauseError(
460 "Use\n\na) git checkout %s\nb) git mergetool\nc) git commit\nd) git push\n d) stash pop \ne) start gitlive again\n".printf(
468 string[] cmd = { "stash", "pop" };
469 if (stash) { this.git(cmd); }
471 GitMonitor.gitmonitor.pauseError(ee.message);
479 string[] cmd = { "checkout", "-b" , branchname };
482 cmd = { "push", "-u" , "origin" ,"HEAD" };
486 GitMonitor.gitmonitor.pauseError(ee.message);
491 var notification = new Notify.Notification(
492 "Changed to branch %s".printf(branchname),
498 notification.set_timeout(5);
502 this.loadBranches(); // update branch list...
503 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
510 * add files to track.
512 * @argument {Array} files the files to add.
514 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
516 // should really find out if these are untracked files each..
517 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
518 // not sure if that is how git works.. but just be certian.
520 for (var i = 0; i < files.size;i++) {
521 var f = files.get(i).vname;
523 string[] cmd = { "add", f };
526 ret += e.message + "\n";
533 public bool is_ignore(string fname) throws Error, SpawnError
535 if (fname == ".gitignore") {
536 this.ignore_files.clear();
539 if (this.ignore_files.has_key(fname)) {
540 return this.ignore_files.get(fname);
544 var ret = this.git( { "check-ignore" , fname } );
545 this.ignore_files.set(fname, ret.length > 0);
546 return ret.length > 0;
547 } catch (SpawnError e) {
548 this.ignore_files.set(fname, false);
557 * remove files to track.
559 * @argument {Array} files the files to add.
561 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
563 // this may fail if files do not exist..
564 // should really find out if these are untracked files each..
565 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
566 // not sure if that is how git works.. but just be certian.
569 for (var i = 0; i < files.size;i++) {
570 var f = files.get(i).vname;
572 string[] cmd = { "rm", "-f" , f };
575 ret += e.message + "\n";
588 * @argument {Object} cfg commit configuration
590 * @property {String} name (optional)
591 * @property {String} email (optional)
592 * @property {String} changed (date) (optional)
593 * @property {String} reason (optional)
594 * @property {Array} files - the files that have changed.
598 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
605 if (typeof(cfg.name) != 'undefined') {
607 'author' : cfg.name + ' <' + cfg.email + '>'
610 "GIT_COMMITTER_NAME" + cfg.name,
611 "GIT_COMMITTER_EMAIL" + cfg.email
615 if (typeof(cfg.changed) != 'undefined') {
616 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
620 string[] args = { "commit", "-m" };
621 args += (message.length > 0 ? message : "Changed" );
622 for (var i = 0; i< files.size ; i++ ) {
623 args += files.get(i).vname; // full path?
626 return this.git(args);
631 * Fetch and merge remote repo changes into current branch..
633 * At present we just need this to update the current working branch..
634 * -- maybe later it will have a few options and do more stuff..
637 public string pull () throws Error, SpawnError
639 // should probably hand error conditions better...
640 string[] cmd = { "pull" , "--no-edit" };
641 return this.git( cmd );
646 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
647 public void pull_async(GitAsyncCallback cb)
650 string[] cmd = { "pull" , "--no-edit" };
651 this.git_async( cmd , cb);
658 * Send local changes to remote repo(s)
660 * At present we just need this to push the current branch.
661 * -- maybe later it will have a few options and do more stuff..
664 public string push () throws Error, SpawnError
667 return this.git({ "push" });
675 * The meaty part.. run spawn.. with git..
680 public string git(string[] args_in ) throws Error, SpawnError
682 // convert arguments.
684 string[] args = { "git" };
685 //args += "--git-dir";
686 //args += this.gitdir;
687 args += "--no-pager";
690 //if (this.gitdir != this.repopath) {
691 // args += "--work-tree";
692 // args += this.repopath;
694 for (var i = 0; i < args_in.length;i++) {
698 //this.lastCmd = args.join(" ");
700 GLib.debug( "CWD=%s", this.git_working_dir );
701 GLib.debug( "cmd: %s", string.joinv (" ", args));
705 string home = "HOME=" + Environment.get_home_dir() ;
707 // do not need to set gitpath..
708 //if (File.exists(this.repo + '/.git/config')) {
709 //env.push("GITPATH=" + this.repo );
713 var cfg = new SpawnConfig(this.git_working_dir , args , env);
716 // may throw error...
717 var sp = new Spawn(cfg);
720 GLib.debug( "GOT: %s" , sp.output);
721 // parse output for some commands ?
725 unowned GitAsyncCallback git_async_on_callback;
726 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
728 // convert arguments.
729 this.git_async_on_callback = cb;
730 string[] args = { "git" };
731 //args += "--git-dir";
732 //args += this.gitdir;
733 args += "--no-pager";
736 //if (this.gitdir != this.repopath) {
737 // args += "--work-tree";
738 // args += this.repopath;
740 for (var i = 0; i < args_in.length;i++) {
744 //this.lastCmd = args.join(" ");
746 GLib.debug( "CWD=%s", this.git_working_dir );
747 //print( "cmd: %s\n", string.joinv (" ", args));
751 string home = "HOME=" + Environment.get_home_dir() ;
753 // do not need to set gitpath..
754 //if (File.exists(this.repo + '/.git/config')) {
755 //env.push("GITPATH=" + this.repo );
759 var cfg = new SpawnConfig(this.git_working_dir , args , env);
763 // may throw error...
764 var sp = new Spawn(cfg);
767 sp.run(this.git_async_on_complete);
771 void git_async_on_complete(int err, string output)
773 GLib.debug("GOT %d : %s", err, output);
774 this.git_async_on_callback(this, err, output);