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;
21 public bool has_local_changes = false;
24 public Gee.HashMap<string,bool> ignore_files;
25 public GitBranch currentBranch;
26 public Gee.HashMap<string,GitBranch> branches; // accessed in GitBranch..
27 public RooTicket? activeTicket;
28 public Gee.HashMap<string,GitRepo> cache;
32 public static GitRepo singleton()
34 if (_GitRepo == null) {
35 _GitRepo = new GitRepo.single();
36 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
42 * index of.. matching gitpath..
44 public static int indexOf( Array<GitRepo> repos, string gitpath) {
45 // make a fake object to compare against..
46 var test_repo = GitRepo.get(gitpath);
48 for(var i =0; i < repos.length; i++) {
49 if (repos.index(i).gitdir == test_repo.gitdir) {
61 public static Array<GitRepo> list()
64 //if (GitRepo.list_cache != null) {
65 // unowned Array<GitRepo> ret = GitRepo.list_cache;
68 var cache = GitRepo.singleton().cache;
69 var list_cache = new Array<GitRepo>();
71 var dir = Environment.get_home_dir() + "/gitlive";
73 var f = File.new_for_path(dir);
74 FileEnumerator file_enum;
76 file_enum = f.enumerate_children(
77 FileAttribute.STANDARD_DISPLAY_NAME + ","+
78 FileAttribute.STANDARD_TYPE,
79 FileQueryInfoFlags.NONE,
92 next_file = file_enum.next_file(null);
93 if (next_file == null) {
98 GLib.debug("Error: %s",e.message);
102 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
104 if (next_file.get_file_type() != FileType.DIRECTORY) {
109 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
114 if (next_file.get_display_name()[0] == '.') {
118 var sp = dir+"/"+next_file.get_display_name();
120 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
122 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
126 var rep = GitRepo.get( sp );
127 list_cache.append_val(rep);
137 public static GitRepo get(string path)
139 var cache = GitRepo.singleton().cache;
140 if (cache.has_key(path)) {
141 return cache.get(path);
143 return new GitRepo(path);
146 private GitRepo.single() {
147 // used to create the signleton
152 * @param {Object} cfg - Configuration
153 * (basically repopath is currently only critical one.)
157 private GitRepo(string path) {
159 this.name = File.new_for_path(path).get_basename();
160 this.ignore_files = new Gee.HashMap<string,bool>();
162 this.git_working_dir = path;
163 this.gitdir = path + "/.git";
164 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
165 this.gitdir = path; // naked...
167 this.cmds = new Gee.ArrayList<GitMonitorQueue> ();
169 var cache = GitRepo.singleton().cache;
170 //Repo.superclass.constructor.call(this,cfg);
171 if ( !cache.has_key(path) ) {
172 cache.set( path, this);
175 this.loadActiveTicket();
179 public bool is_wip_branch()
181 return this.currentBranch.name.has_prefix("wip_");
185 public bool is_autocommit ()
187 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
189 public void set_autocommit(bool val)
192 var cur = this.is_autocommit();
193 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
195 return; // no change..
198 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
201 FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" );
206 public bool is_auto_branch ()
208 return FileUtils.test(this.gitdir + "/.gitlive-enable-auto-branch" , FileTest.EXISTS);
211 public void set_auto_branch(bool val)
214 var cur = this.is_auto_branch();
215 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
218 return; // no change..
221 FileUtils.set_contents(this.gitdir + "/.gitlive-enable-auto-branch" , "x");
224 FileUtils.remove(this.gitdir + "/.gitlive-enable-auto-branch" );
228 public bool is_autopush ()
230 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
232 public void set_autopush(bool val)
235 var cur = this.is_autopush();
236 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
238 return; // no change..
241 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
244 FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" );
253 public void loadBranches()
255 GitBranch.loadBranches(this);
261 public string branchesToString()
264 foreach( var br in this.branches.values) {
268 ret += ret.length > 0 ? "\n" : "";
275 public static void doMerges(string action, string ticket_id, string commit_message)
277 GitMonitor.gitmonitor.stop();
281 foreach(var repo in GitRepo.singleton().cache.values) {
282 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
283 var res = repo.doMerge(action,ticket_id, commit_message);
288 commitrevs += commitrevs.length > 0 ? " " : "";
289 commitrevs += repo.currentBranch.lastrev;
292 if (sucess && action == "CLOSE") {
293 RooTicket.singleton().getById(ticket_id).close(commitrevs);
295 GitMonitor.gitmonitor.start();
299 public bool doMerge(string action, string ticket_id, string commit_message)
301 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
302 // also need to decide which branch we will merge into?
304 if (action == "CLOSE" || action == "LEAVE") {
307 var oldbranch = this.currentBranch.name;
308 this.setActiveTicket(null, "master");
309 string [] cmd = { "merge", "--squash", oldbranch };
311 cmd = { "commit", "-a" , "-m", commit_message };
314 this.loadBranches(); // updates lastrev..
316 var notification = new Notify.Notification(
317 "Merged branch %s to master".printf(oldbranch),
323 notification.set_timeout(5);
331 GitMonitor.gitmonitor.pauseError(e.message);
334 // error~?? -- show the error dialog...
337 if (action == "MASTER") {
338 // merge master into ours..
340 string[] cmd = { "merge", "master" };
342 var notification = new Notify.Notification(
343 "Merged code from master to %s".printf(this.currentBranch.name),
348 notification.set_timeout(5);
353 GitMonitor.gitmonitor.pauseError(e.message);
357 if (action == "EXIT") {
359 var oldbranch = this.currentBranch.name;
360 this.setActiveTicket(null, "master");
362 var notification = new Notify.Notification(
363 "Left branch %s".printf(oldbranch),
368 notification.set_timeout(5);
373 GitMonitor.gitmonitor.pauseError(e.message);
377 // error~?? -- show the error dialog...
383 public void loadActiveTicket()
385 this.activeTicket = null;
386 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
390 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
391 if (ticket_id.length < 1) {
394 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
401 public bool setActiveTicket(RooTicket? ticket, string branchname)
403 if (!this.createBranchNamed(branchname)) {
406 if (ticket != null) {
407 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
409 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
411 this.activeTicket = ticket;
415 public bool createBranchNamed(string branchname)
419 if (this.branches.has_key(branchname)) {
420 this.switchToExistingBranchNamed(branchname);
423 this.createNewBranchNamed(branchname);
426 var notification = new Notify.Notification(
427 "Changed to branch %s".printf(branchname),
433 notification.set_timeout(5);
437 this.loadBranches(); // update branch list...
438 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
441 bool switchToExistingBranchNamed(string branchname)
444 // this is where it get's tricky...
447 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
448 files = this.git(cmd);
449 stash = files.length> 1 ;
453 if (stash) { this.git(cmd); }
457 cmd = { "checkout", branchname };
460 GitMonitor.gitmonitor.pauseError(e.message);
464 if (branchname != "master") {
465 string[] cmd = { "merge", "master" };
472 string[] cmd = { "checkout", "master" };
474 GitMonitor.gitmonitor.pauseError(
475 "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(
483 string[] cmd = { "stash", "pop" };
486 var fl = files.split("\n");
487 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
488 foreach(var f in fl) {
489 if (f.length < 1) continue;
498 GitMonitor.gitmonitor.pauseError(ee.message);
508 bool createNewBranchNamed(string branchname)
512 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
513 var files = this.git(cmd);
514 stash = files.length> 1 ;
516 cmd = { "checkout", "-b" , branchname };
519 cmd = { "push", "-u" , "origin" ,"HEAD" };
523 var fl = files.split("\n");
524 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
525 foreach(var f in fl) {
526 if (f.length < 1) continue;
535 GitMonitor.gitmonitor.pauseError(ee.message);
546 * add files to track.
548 * @argument {Array} files the files to add.
550 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
552 // should really find out if these are untracked files each..
553 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
554 // not sure if that is how git works.. but just be certian.
556 for (var i = 0; i < files.size;i++) {
557 var f = files.get(i).vname;
559 string[] cmd = { "add", f };
562 ret += e.message + "\n";
569 public bool is_ignore(string fname) throws Error, SpawnError
571 if (fname == ".gitignore") {
572 this.ignore_files.clear();
575 if (this.ignore_files.has_key(fname)) {
576 return this.ignore_files.get(fname);
580 var ret = this.git( { "check-ignore" , fname } );
581 this.ignore_files.set(fname, ret.length > 0);
582 return ret.length > 0;
583 } catch (SpawnError e) {
584 this.ignore_files.set(fname, false);
593 * remove files to track.
595 * @argument {Array} files the files to add.
597 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
599 // this may fail if files do not exist..
600 // should really find out if these are untracked files each..
601 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
602 // not sure if that is how git works.. but just be certian.
605 for (var i = 0; i < files.size;i++) {
606 var f = files.get(i).vname;
608 string[] cmd = { "rm", "-f" , f };
611 ret += e.message + "\n";
624 * @argument {Object} cfg commit configuration
626 * @property {String} name (optional)
627 * @property {String} email (optional)
628 * @property {String} changed (date) (optional)
629 * @property {String} reason (optional)
630 * @property {Array} files - the files that have changed.
634 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
641 if (typeof(cfg.name) != 'undefined') {
643 'author' : cfg.name + ' <' + cfg.email + '>'
646 "GIT_COMMITTER_NAME" + cfg.name,
647 "GIT_COMMITTER_EMAIL" + cfg.email
651 if (typeof(cfg.changed) != 'undefined') {
652 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
656 string[] args = { "commit", "-m" };
657 args += (message.length > 0 ? message : "Changed" );
658 for (var i = 0; i< files.size ; i++ ) {
659 args += files.get(i).vname; // full path?
662 return this.git(args);
667 * Fetch and merge remote repo changes into current branch..
669 * At present we just need this to update the current working branch..
670 * -- maybe later it will have a few options and do more stuff..
673 public string pull () throws Error, SpawnError
675 // should probably hand error conditions better...
676 string[] cmd = { "pull" , "--no-edit" };
677 return this.git( cmd );
682 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
683 public void pull_async(GitAsyncCallback cb)
686 string[] cmd = { "pull" , "--no-edit" };
687 this.git_async( cmd , cb);
694 * Send local changes to remote repo(s)
696 * At present we just need this to push the current branch.
697 * -- maybe later it will have a few options and do more stuff..
700 public string push () throws Error, SpawnError
703 return this.git({ "push" });
711 * The meaty part.. run spawn.. with git..
716 public string git(string[] args_in ) throws Error, SpawnError
718 // convert arguments.
720 string[] args = { "git" };
721 //args += "--git-dir";
722 //args += this.gitdir;
723 args += "--no-pager";
726 //if (this.gitdir != this.repopath) {
727 // args += "--work-tree";
728 // args += this.repopath;
730 for (var i = 0; i < args_in.length;i++) {
734 //this.lastCmd = args.join(" ");
736 GLib.debug( "CWD=%s", this.git_working_dir );
737 GLib.debug( "cmd: %s", string.joinv (" ", args));
741 string home = "HOME=" + Environment.get_home_dir() ;
743 // do not need to set gitpath..
744 //if (File.exists(this.repo + '/.git/config')) {
745 //env.push("GITPATH=" + this.repo );
749 var cfg = new SpawnConfig(this.git_working_dir , args , env);
752 // may throw error...
753 var sp = new Spawn(cfg);
756 GLib.debug( "GOT: %s" , sp.output);
757 // parse output for some commands ?
761 unowned GitAsyncCallback git_async_on_callback;
762 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
764 // convert arguments.
765 this.git_async_on_callback = cb;
766 string[] args = { "git" };
767 //args += "--git-dir";
768 //args += this.gitdir;
769 args += "--no-pager";
772 //if (this.gitdir != this.repopath) {
773 // args += "--work-tree";
774 // args += this.repopath;
776 for (var i = 0; i < args_in.length;i++) {
780 //this.lastCmd = args.join(" ");
782 GLib.debug( "CWD=%s", this.git_working_dir );
783 //print( "cmd: %s\n", string.joinv (" ", args));
787 string home = "HOME=" + Environment.get_home_dir() ;
789 // do not need to set gitpath..
790 //if (File.exists(this.repo + '/.git/config')) {
791 //env.push("GITPATH=" + this.repo );
795 var cfg = new SpawnConfig(this.git_working_dir , args , env);
799 // may throw error...
800 var sp = new Spawn(cfg);
803 sp.run(this.git_async_on_complete);
807 void git_async_on_complete(int err, string output)
809 GLib.debug("GOT %d : %s", err, output);
810 this.git_async_on_callback(this, err, output);