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" );
250 public void loadStatus()
252 var r = this.git({ "status" , "--porcelain" });
253 this.has_local_changes = r.length > 0;
258 public void loadBranches()
260 GitBranch.loadBranches(this);
266 public string branchesToString()
269 foreach( var br in this.branches.values) {
273 ret += ret.length > 0 ? "\n" : "";
280 public static void doMerges(string action, string ticket_id, string commit_message)
282 GitMonitor.gitmonitor.stop();
286 foreach(var repo in GitRepo.singleton().cache.values) {
287 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
288 var res = repo.doMerge(action,ticket_id, commit_message);
293 commitrevs += commitrevs.length > 0 ? " " : "";
294 commitrevs += repo.currentBranch.lastrev;
297 if (sucess && action == "CLOSE") {
298 RooTicket.singleton().getById(ticket_id).close(commitrevs);
300 GitMonitor.gitmonitor.start();
304 public bool doMerge(string action, string ticket_id, string commit_message)
306 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
307 // also need to decide which branch we will merge into?
309 if (action == "CLOSE" || action == "LEAVE") {
312 var oldbranch = this.currentBranch.name;
313 this.setActiveTicket(null, "master");
314 string [] cmd = { "merge", "--squash", oldbranch };
316 cmd = { "commit", "-a" , "-m", commit_message };
319 this.loadBranches(); // updates lastrev..
321 var notification = new Notify.Notification(
322 "Merged branch %s to master".printf(oldbranch),
328 notification.set_timeout(5);
336 GitMonitor.gitmonitor.pauseError(e.message);
339 // error~?? -- show the error dialog...
342 if (action == "MASTER") {
343 // merge master into ours..
345 string[] cmd = { "merge", "master" };
347 var notification = new Notify.Notification(
348 "Merged code from master to %s".printf(this.currentBranch.name),
353 notification.set_timeout(5);
358 GitMonitor.gitmonitor.pauseError(e.message);
362 if (action == "EXIT") {
364 var oldbranch = this.currentBranch.name;
365 this.setActiveTicket(null, "master");
367 var notification = new Notify.Notification(
368 "Left branch %s".printf(oldbranch),
373 notification.set_timeout(5);
378 GitMonitor.gitmonitor.pauseError(e.message);
382 // error~?? -- show the error dialog...
388 public void loadActiveTicket()
390 this.activeTicket = null;
391 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
395 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
396 if (ticket_id.length < 1) {
399 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
406 public bool setActiveTicket(RooTicket? ticket, string branchname)
408 if (!this.createBranchNamed(branchname)) {
411 if (ticket != null) {
412 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
414 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
416 this.activeTicket = ticket;
420 public bool createBranchNamed(string branchname)
424 if (this.branches.has_key(branchname)) {
425 this.switchToExistingBranchNamed(branchname);
428 this.createNewBranchNamed(branchname);
431 var notification = new Notify.Notification(
432 "Changed to branch %s".printf(branchname),
438 notification.set_timeout(5);
442 this.loadBranches(); // update branch list...
443 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
446 bool switchToExistingBranchNamed(string branchname)
449 // this is where it get's tricky...
452 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
453 files = this.git(cmd);
454 stash = files.length> 1 ;
458 if (stash) { this.git(cmd); }
462 cmd = { "checkout", branchname };
465 GitMonitor.gitmonitor.pauseError(e.message);
469 if (branchname != "master") {
470 string[] cmd = { "merge", "master" };
477 string[] cmd = { "checkout", "master" };
479 GitMonitor.gitmonitor.pauseError(
480 "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(
488 string[] cmd = { "stash", "pop" };
491 var fl = files.split("\n");
492 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
493 foreach(var f in fl) {
494 if (f.length < 1) continue;
503 GitMonitor.gitmonitor.pauseError(ee.message);
513 bool createNewBranchNamed(string branchname)
517 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
518 var files = this.git(cmd);
519 stash = files.length> 1 ;
521 cmd = { "checkout", "-b" , branchname };
524 cmd = { "push", "-u" , "origin" ,"HEAD" };
528 var fl = files.split("\n");
529 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
530 foreach(var f in fl) {
531 if (f.length < 1) continue;
540 GitMonitor.gitmonitor.pauseError(ee.message);
551 * add files to track.
553 * @argument {Array} files the files to add.
555 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
557 // should really find out if these are untracked files each..
558 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
559 // not sure if that is how git works.. but just be certian.
561 for (var i = 0; i < files.size;i++) {
562 var f = files.get(i).vname;
564 string[] cmd = { "add", f };
567 ret += e.message + "\n";
574 public bool is_ignore(string fname) throws Error, SpawnError
576 if (fname == ".gitignore") {
577 this.ignore_files.clear();
580 if (this.ignore_files.has_key(fname)) {
581 return this.ignore_files.get(fname);
585 var ret = this.git( { "check-ignore" , fname } );
586 this.ignore_files.set(fname, ret.length > 0);
587 return ret.length > 0;
588 } catch (SpawnError e) {
589 this.ignore_files.set(fname, false);
598 * remove files to track.
600 * @argument {Array} files the files to add.
602 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
604 // this may fail if files do not exist..
605 // should really find out if these are untracked files each..
606 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
607 // not sure if that is how git works.. but just be certian.
610 for (var i = 0; i < files.size;i++) {
611 var f = files.get(i).vname;
613 string[] cmd = { "rm", "-f" , f };
616 ret += e.message + "\n";
629 * @argument {Object} cfg commit configuration
631 * @property {String} name (optional)
632 * @property {String} email (optional)
633 * @property {String} changed (date) (optional)
634 * @property {String} reason (optional)
635 * @property {Array} files - the files that have changed.
639 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
646 if (typeof(cfg.name) != 'undefined') {
648 'author' : cfg.name + ' <' + cfg.email + '>'
651 "GIT_COMMITTER_NAME" + cfg.name,
652 "GIT_COMMITTER_EMAIL" + cfg.email
656 if (typeof(cfg.changed) != 'undefined') {
657 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
661 string[] args = { "commit", "-m" };
662 args += (message.length > 0 ? message : "Changed" );
663 for (var i = 0; i< files.size ; i++ ) {
664 args += files.get(i).vname; // full path?
667 return this.git(args);
672 * Fetch and merge remote repo changes into current branch..
674 * At present we just need this to update the current working branch..
675 * -- maybe later it will have a few options and do more stuff..
678 public string pull () throws Error, SpawnError
680 // should probably hand error conditions better...
681 string[] cmd = { "pull" , "--no-edit" };
682 return this.git( cmd );
687 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
688 public void pull_async(GitAsyncCallback cb)
691 string[] cmd = { "pull" , "--no-edit" };
692 this.git_async( cmd , cb);
699 * Send local changes to remote repo(s)
701 * At present we just need this to push the current branch.
702 * -- maybe later it will have a few options and do more stuff..
705 public string push () throws Error, SpawnError
708 return this.git({ "push" });
716 * The meaty part.. run spawn.. with git..
721 public string git(string[] args_in ) throws Error, SpawnError
723 // convert arguments.
725 string[] args = { "git" };
726 //args += "--git-dir";
727 //args += this.gitdir;
728 args += "--no-pager";
731 //if (this.gitdir != this.repopath) {
732 // args += "--work-tree";
733 // args += this.repopath;
735 for (var i = 0; i < args_in.length;i++) {
739 //this.lastCmd = args.join(" ");
741 GLib.debug( "CWD=%s", this.git_working_dir );
742 GLib.debug( "cmd: %s", string.joinv (" ", args));
746 string home = "HOME=" + Environment.get_home_dir() ;
748 // do not need to set gitpath..
749 //if (File.exists(this.repo + '/.git/config')) {
750 //env.push("GITPATH=" + this.repo );
754 var cfg = new SpawnConfig(this.git_working_dir , args , env);
757 // may throw error...
758 var sp = new Spawn(cfg);
761 GLib.debug( "GOT: %s" , sp.output);
762 // parse output for some commands ?
766 unowned GitAsyncCallback git_async_on_callback;
767 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
769 // convert arguments.
770 this.git_async_on_callback = cb;
771 string[] args = { "git" };
772 //args += "--git-dir";
773 //args += this.gitdir;
774 args += "--no-pager";
777 //if (this.gitdir != this.repopath) {
778 // args += "--work-tree";
779 // args += this.repopath;
781 for (var i = 0; i < args_in.length;i++) {
785 //this.lastCmd = args.join(" ");
787 GLib.debug( "CWD=%s", this.git_working_dir );
788 //print( "cmd: %s\n", string.joinv (" ", args));
792 string home = "HOME=" + Environment.get_home_dir() ;
794 // do not need to set gitpath..
795 //if (File.exists(this.repo + '/.git/config')) {
796 //env.push("GITPATH=" + this.repo );
800 var cfg = new SpawnConfig(this.git_working_dir , args , env);
804 // may throw error...
805 var sp = new Spawn(cfg);
808 sp.run(this.git_async_on_complete);
812 void git_async_on_complete(int err, string output)
814 GLib.debug("GOT %d : %s", err, output);
815 this.git_async_on_callback(this, err, output);
826 public void update_async(GitAsyncCallback cb)
828 string[] cmd = { "fetch" , "--all" };
829 this.git_async( cmd , cb);
834 static uint update_all_total = 0;
835 static string update_all_after = "";
837 public static void updateAll(string after)
839 update_all_after = after;
840 var tr = GitRepo.singleton().cache;
843 update_all_total = tr.size;
844 foreach(var repo in tr.values) {
847 repo.update_rsync(updateAllCallback);
848 // do not care if it's already in sycn..
857 public static void updateAllCallback(GitRepo repo, int err, string res)
863 if (update_all_total > 0 ) {
866 switch (update_all_after) {
868 // show the clones dialog..