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;
22 public string git_status;
23 public string git_diff;
24 public string ahead_or_behind = "";
26 public Gee.HashMap<string,bool> ignore_files;
27 public GitBranch currentBranch;
28 public Gee.HashMap<string,GitBranch> branches; // accessed in GitBranch..
29 public RooTicket? activeTicket;
30 public Gee.HashMap<string,GitRepo> cache;
34 public static GitRepo singleton()
36 if (_GitRepo == null) {
37 _GitRepo = new GitRepo.single();
38 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
44 * index of.. matching gitpath..
46 public static int indexOf( Array<GitRepo> repos, string gitpath) {
47 // make a fake object to compare against..
48 var test_repo = GitRepo.get(gitpath);
50 for(var i =0; i < repos.length; i++) {
51 if (repos.index(i).gitdir == test_repo.gitdir) {
63 public static Array<GitRepo> list()
66 //if (GitRepo.list_cache != null) {
67 // unowned Array<GitRepo> ret = GitRepo.list_cache;
70 var cache = GitRepo.singleton().cache;
71 var list_cache = new Array<GitRepo>();
73 var dir = Environment.get_home_dir() + "/gitlive";
75 var f = File.new_for_path(dir);
76 FileEnumerator file_enum;
78 file_enum = f.enumerate_children(
79 FileAttribute.STANDARD_DISPLAY_NAME + ","+
80 FileAttribute.STANDARD_TYPE,
81 FileQueryInfoFlags.NONE,
94 next_file = file_enum.next_file(null);
95 if (next_file == null) {
100 GLib.debug("Error: %s",e.message);
104 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
106 if (next_file.get_file_type() != FileType.DIRECTORY) {
111 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
116 if (next_file.get_display_name()[0] == '.') {
120 var sp = dir+"/"+next_file.get_display_name();
122 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
124 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
128 var rep = GitRepo.get( sp );
129 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_managed()
187 // is it a roojs origin?
188 var r = this.git({ "remote" , "get-url" , "--push" , "origin"});
191 return FileUtils.test(this.gitdir + "/.gitlive-managed" , FileTest.EXISTS);
195 public bool is_autocommit ()
197 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
199 public void set_autocommit(bool val)
202 var cur = this.is_autocommit();
203 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
205 return; // no change..
208 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
211 FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" );
216 public bool is_auto_branch ()
218 return FileUtils.test(this.gitdir + "/.gitlive-enable-auto-branch" , FileTest.EXISTS);
221 public void set_auto_branch(bool val)
224 var cur = this.is_auto_branch();
225 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
228 return; // no change..
231 FileUtils.set_contents(this.gitdir + "/.gitlive-enable-auto-branch" , "x");
234 FileUtils.remove(this.gitdir + "/.gitlive-enable-auto-branch" );
238 public bool is_autopush ()
240 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
242 public void set_autopush(bool val)
245 var cur = this.is_autopush();
246 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
248 return; // no change..
251 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
254 FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" );
260 public void loadStatus()
262 var r = this.git({ "status" , "--porcelain" });
264 this.has_local_changes = r.length > 0;
266 var rs = this.git({ "status" , "-sb" });
268 this.ahead_or_behind = rs.contains("[ahead") ? "A" : (rs.contains("[behind") ? "B" : "");
271 this.git_diff = this.git({ "diff" , "HEAD", "--no-color" });
275 public void loadBranches()
277 GitBranch.loadBranches(this);
283 public string branchesToString()
286 foreach( var br in this.branches.values) {
290 ret += ret.length > 0 ? "\n" : "";
297 public static void doMerges(string action, string ticket_id, string commit_message)
299 GitMonitor.gitmonitor.stop();
303 foreach(var repo in GitRepo.singleton().cache.values) {
304 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
305 var res = repo.doMerge(action,ticket_id, commit_message);
310 commitrevs += commitrevs.length > 0 ? " " : "";
311 commitrevs += repo.currentBranch.lastrev;
314 if (sucess && action == "CLOSE") {
315 RooTicket.singleton().getById(ticket_id).close(commitrevs);
317 GitMonitor.gitmonitor.start();
321 public bool doMerge(string action, string ticket_id, string commit_message)
323 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
324 // also need to decide which branch we will merge into?
326 if (action == "CLOSE" || action == "LEAVE") {
329 var oldbranch = this.currentBranch.name;
330 this.setActiveTicket(null, "master");
331 string [] cmd = { "merge", "--squash", oldbranch };
333 cmd = { "commit", "-a" , "-m", commit_message };
336 this.loadBranches(); // updates lastrev..
338 var notification = new Notify.Notification(
339 "Merged branch %s to master".printf(oldbranch),
345 notification.set_timeout(5);
353 GitMonitor.gitmonitor.pauseError(e.message);
356 // error~?? -- show the error dialog...
359 if (action == "MASTER") {
360 // merge master into ours..
362 string[] cmd = { "merge", "master" };
364 var notification = new Notify.Notification(
365 "Merged code from master to %s".printf(this.currentBranch.name),
370 notification.set_timeout(5);
375 GitMonitor.gitmonitor.pauseError(e.message);
379 if (action == "EXIT") {
381 var oldbranch = this.currentBranch.name;
382 this.setActiveTicket(null, "master");
384 var notification = new Notify.Notification(
385 "Left branch %s".printf(oldbranch),
390 notification.set_timeout(5);
395 GitMonitor.gitmonitor.pauseError(e.message);
399 // error~?? -- show the error dialog...
405 public void loadActiveTicket()
407 this.activeTicket = null;
408 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
412 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
413 if (ticket_id.length < 1) {
416 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
423 public bool setActiveTicket(RooTicket? ticket, string branchname)
425 if (!this.createBranchNamed(branchname)) {
428 if (ticket != null) {
429 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
431 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
433 this.activeTicket = ticket;
437 public bool createBranchNamed(string branchname)
441 if (this.branches.has_key(branchname)) {
442 this.switchToExistingBranchNamed(branchname);
445 this.createNewBranchNamed(branchname);
448 var notification = new Notify.Notification(
449 "Changed to branch %s".printf(branchname),
455 notification.set_timeout(5);
459 this.loadBranches(); // update branch list...
460 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
463 bool switchToExistingBranchNamed(string branchname)
466 // this is where it get's tricky...
469 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
470 files = this.git(cmd);
471 stash = files.length> 1 ;
475 if (stash) { this.git(cmd); }
479 cmd = { "checkout", branchname };
482 GitMonitor.gitmonitor.pauseError(e.message);
486 if (branchname != "master") {
487 string[] cmd = { "merge", "master" };
494 string[] cmd = { "checkout", "master" };
496 GitMonitor.gitmonitor.pauseError(
497 "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(
505 string[] cmd = { "stash", "pop" };
508 var fl = files.split("\n");
509 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
510 foreach(var f in fl) {
511 if (f.length < 1) continue;
520 GitMonitor.gitmonitor.pauseError(ee.message);
530 bool createNewBranchNamed(string branchname)
534 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
535 var files = this.git(cmd);
536 stash = files.length> 1 ;
538 cmd = { "checkout", "-b" , branchname };
541 cmd = { "push", "-u" , "origin" ,"HEAD" };
545 var fl = files.split("\n");
546 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
547 foreach(var f in fl) {
548 if (f.length < 1) continue;
557 GitMonitor.gitmonitor.pauseError(ee.message);
568 * add files to track.
570 * @argument {Array} files the files to add.
572 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
574 // should really find out if these are untracked files each..
575 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
576 // not sure if that is how git works.. but just be certian.
578 for (var i = 0; i < files.size;i++) {
579 var f = files.get(i).vname;
581 string[] cmd = { "add", f };
584 ret += e.message + "\n";
591 public bool is_ignore(string fname) throws Error, SpawnError
593 if (fname == ".gitignore") {
594 this.ignore_files.clear();
597 if (this.ignore_files.has_key(fname)) {
598 return this.ignore_files.get(fname);
602 var ret = this.git( { "check-ignore" , fname } );
603 this.ignore_files.set(fname, ret.length > 0);
604 return ret.length > 0;
605 } catch (SpawnError e) {
606 this.ignore_files.set(fname, false);
615 * remove files to track.
617 * @argument {Array} files the files to add.
619 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
621 // this may fail if files do not exist..
622 // should really find out if these are untracked files each..
623 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
624 // not sure if that is how git works.. but just be certian.
627 for (var i = 0; i < files.size;i++) {
628 var f = files.get(i).vname;
630 string[] cmd = { "rm", "-f" , f };
633 ret += e.message + "\n";
646 * @argument {Object} cfg commit configuration
648 * @property {String} name (optional)
649 * @property {String} email (optional)
650 * @property {String} changed (date) (optional)
651 * @property {String} reason (optional)
652 * @property {Array} files - the files that have changed.
656 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
663 if (typeof(cfg.name) != 'undefined') {
665 'author' : cfg.name + ' <' + cfg.email + '>'
668 "GIT_COMMITTER_NAME" + cfg.name,
669 "GIT_COMMITTER_EMAIL" + cfg.email
673 if (typeof(cfg.changed) != 'undefined') {
674 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
678 string[] args = { "commit", "-m" };
679 args += (message.length > 0 ? message : "Changed" );
680 for (var i = 0; i< files.size ; i++ ) {
681 args += files.get(i).vname; // full path?
684 return this.git(args);
689 * Fetch and merge remote repo changes into current branch..
691 * At present we just need this to update the current working branch..
692 * -- maybe later it will have a few options and do more stuff..
695 public string pull () throws Error, SpawnError
697 // should probably hand error conditions better...
698 string[] cmd = { "pull" , "--no-edit" };
699 return this.git( cmd );
704 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
705 public void pull_async(GitAsyncCallback cb)
708 string[] cmd = { "pull" , "--no-edit" };
709 this.git_async( cmd , cb);
716 * Send local changes to remote repo(s)
718 * At present we just need this to push the current branch.
719 * -- maybe later it will have a few options and do more stuff..
722 public string push () throws Error, SpawnError
725 return this.git({ "push" });
733 * The meaty part.. run spawn.. with git..
738 public string git(string[] args_in ) throws Error, SpawnError
740 // convert arguments.
742 string[] args = { "git" };
743 //args += "--git-dir";
744 //args += this.gitdir;
745 args += "--no-pager";
748 //if (this.gitdir != this.repopath) {
749 // args += "--work-tree";
750 // args += this.repopath;
752 for (var i = 0; i < args_in.length;i++) {
756 //this.lastCmd = args.join(" ");
758 GLib.debug( "CWD=%s", this.git_working_dir );
759 GLib.debug( "cmd: %s", string.joinv (" ", args));
763 string home = "HOME=" + Environment.get_home_dir() ;
765 // do not need to set gitpath..
766 //if (File.exists(this.repo + '/.git/config')) {
767 //env.push("GITPATH=" + this.repo );
770 var cfg = new SpawnConfig(this.git_working_dir , args , env);
773 // may throw error...
774 var sp = new Spawn(cfg);
776 // diff output is a bit big..
777 if (args_in[0] != "diff") {
778 GLib.debug( "GOT: %s" , sp.output);
780 // parse output for some commands ?
784 unowned GitAsyncCallback git_async_on_callback;
785 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
787 // convert arguments.
788 this.git_async_on_callback = cb;
789 string[] args = { "git" };
790 //args += "--git-dir";
791 //args += this.gitdir;
792 args += "--no-pager";
795 //if (this.gitdir != this.repopath) {
796 // args += "--work-tree";
797 // args += this.repopath;
799 for (var i = 0; i < args_in.length;i++) {
803 //this.lastCmd = args.join(" ");
805 GLib.debug( "CWD=%s", this.git_working_dir );
806 //print( "cmd: %s\n", string.joinv (" ", args));
810 string home = "HOME=" + Environment.get_home_dir() ;
812 // do not need to set gitpath..
813 //if (File.exists(this.repo + '/.git/config')) {
814 //env.push("GITPATH=" + this.repo );
818 var cfg = new SpawnConfig(this.git_working_dir , args , env);
822 // may throw error...
823 var sp = new Spawn(cfg);
826 sp.run(this.git_async_on_complete);
830 void git_async_on_complete(int err, string output)
832 GLib.debug("GOT %d : %s", err, output);
833 this.git_async_on_callback(this, err, output);
844 public void update_async(GitAsyncCallback cb)
846 string[] cmd = { "fetch" , "--all" };
847 this.git_async( cmd , cb);
852 static uint update_all_total = 0;
853 static string update_all_after = "";
855 public static void updateAll(string after)
857 update_all_after = after;
858 var tr = GitRepo.singleton().cache;
861 update_all_total = tr.size;
862 foreach(var repo in tr.values) {
863 repo.update_async(updateAllCallback);
867 public static void updateAllCallback(GitRepo repo, int err, string res)
873 if (update_all_total > 0 ) {
876 switch (update_all_after) {
878 Clones.singleton().show();