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;
25 public Gee.HashMap<string,bool> ignore_files;
26 public GitBranch currentBranch;
27 public Gee.HashMap<string,GitBranch> branches; // accessed in GitBranch..
28 public RooTicket? activeTicket;
29 public Gee.HashMap<string,GitRepo> cache;
33 public static GitRepo singleton()
35 if (_GitRepo == null) {
36 _GitRepo = new GitRepo.single();
37 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
43 * index of.. matching gitpath..
45 public static int indexOf( Array<GitRepo> repos, string gitpath) {
46 // make a fake object to compare against..
47 var test_repo = GitRepo.get(gitpath);
49 for(var i =0; i < repos.length; i++) {
50 if (repos.index(i).gitdir == test_repo.gitdir) {
62 public static Array<GitRepo> list()
65 //if (GitRepo.list_cache != null) {
66 // unowned Array<GitRepo> ret = GitRepo.list_cache;
69 var cache = GitRepo.singleton().cache;
70 var list_cache = new Array<GitRepo>();
72 var dir = Environment.get_home_dir() + "/gitlive";
74 var f = File.new_for_path(dir);
75 FileEnumerator file_enum;
77 file_enum = f.enumerate_children(
78 FileAttribute.STANDARD_DISPLAY_NAME + ","+
79 FileAttribute.STANDARD_TYPE,
80 FileQueryInfoFlags.NONE,
93 next_file = file_enum.next_file(null);
94 if (next_file == null) {
99 GLib.debug("Error: %s",e.message);
103 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
105 if (next_file.get_file_type() != FileType.DIRECTORY) {
110 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
115 if (next_file.get_display_name()[0] == '.') {
119 var sp = dir+"/"+next_file.get_display_name();
121 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
123 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
127 var rep = GitRepo.get( sp );
128 list_cache.append_val(rep);
138 public static GitRepo get(string path)
140 var cache = GitRepo.singleton().cache;
141 if (cache.has_key(path)) {
142 return cache.get(path);
144 return new GitRepo(path);
147 private GitRepo.single() {
148 // used to create the signleton
153 * @param {Object} cfg - Configuration
154 * (basically repopath is currently only critical one.)
158 private GitRepo(string path) {
160 this.name = File.new_for_path(path).get_basename();
161 this.ignore_files = new Gee.HashMap<string,bool>();
163 this.git_working_dir = path;
164 this.gitdir = path + "/.git";
165 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
166 this.gitdir = path; // naked...
168 this.cmds = new Gee.ArrayList<GitMonitorQueue> ();
170 var cache = GitRepo.singleton().cache;
171 //Repo.superclass.constructor.call(this,cfg);
172 if ( !cache.has_key(path) ) {
173 cache.set( path, this);
176 this.loadActiveTicket();
180 public bool is_wip_branch()
182 return this.currentBranch.name.has_prefix("wip_");
186 public bool is_autocommit ()
188 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
190 public void set_autocommit(bool val)
193 var cur = this.is_autocommit();
194 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
196 return; // no change..
199 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
202 FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" );
207 public bool is_auto_branch ()
209 return FileUtils.test(this.gitdir + "/.gitlive-enable-auto-branch" , FileTest.EXISTS);
212 public void set_auto_branch(bool val)
215 var cur = this.is_auto_branch();
216 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
219 return; // no change..
222 FileUtils.set_contents(this.gitdir + "/.gitlive-enable-auto-branch" , "x");
225 FileUtils.remove(this.gitdir + "/.gitlive-enable-auto-branch" );
229 public bool is_autopush ()
231 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
233 public void set_autopush(bool val)
236 var cur = this.is_autopush();
237 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
239 return; // no change..
242 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
245 FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" );
251 public void loadStatus()
253 var r = this.git({ "status" , "--porcelain" });
255 this.has_local_changes = r.length > 0;
256 this.git_diff = this.git({ "diff" , "HEAD", "--no-color" });
260 public void loadBranches()
262 GitBranch.loadBranches(this);
268 public string branchesToString()
271 foreach( var br in this.branches.values) {
275 ret += ret.length > 0 ? "\n" : "";
282 public static void doMerges(string action, string ticket_id, string commit_message)
284 GitMonitor.gitmonitor.stop();
288 foreach(var repo in GitRepo.singleton().cache.values) {
289 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
290 var res = repo.doMerge(action,ticket_id, commit_message);
295 commitrevs += commitrevs.length > 0 ? " " : "";
296 commitrevs += repo.currentBranch.lastrev;
299 if (sucess && action == "CLOSE") {
300 RooTicket.singleton().getById(ticket_id).close(commitrevs);
302 GitMonitor.gitmonitor.start();
306 public bool doMerge(string action, string ticket_id, string commit_message)
308 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
309 // also need to decide which branch we will merge into?
311 if (action == "CLOSE" || action == "LEAVE") {
314 var oldbranch = this.currentBranch.name;
315 this.setActiveTicket(null, "master");
316 string [] cmd = { "merge", "--squash", oldbranch };
318 cmd = { "commit", "-a" , "-m", commit_message };
321 this.loadBranches(); // updates lastrev..
323 var notification = new Notify.Notification(
324 "Merged branch %s to master".printf(oldbranch),
330 notification.set_timeout(5);
338 GitMonitor.gitmonitor.pauseError(e.message);
341 // error~?? -- show the error dialog...
344 if (action == "MASTER") {
345 // merge master into ours..
347 string[] cmd = { "merge", "master" };
349 var notification = new Notify.Notification(
350 "Merged code from master to %s".printf(this.currentBranch.name),
355 notification.set_timeout(5);
360 GitMonitor.gitmonitor.pauseError(e.message);
364 if (action == "EXIT") {
366 var oldbranch = this.currentBranch.name;
367 this.setActiveTicket(null, "master");
369 var notification = new Notify.Notification(
370 "Left branch %s".printf(oldbranch),
375 notification.set_timeout(5);
380 GitMonitor.gitmonitor.pauseError(e.message);
384 // error~?? -- show the error dialog...
390 public void loadActiveTicket()
392 this.activeTicket = null;
393 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
397 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
398 if (ticket_id.length < 1) {
401 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
408 public bool setActiveTicket(RooTicket? ticket, string branchname)
410 if (!this.createBranchNamed(branchname)) {
413 if (ticket != null) {
414 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
416 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
418 this.activeTicket = ticket;
422 public bool createBranchNamed(string branchname)
426 if (this.branches.has_key(branchname)) {
427 this.switchToExistingBranchNamed(branchname);
430 this.createNewBranchNamed(branchname);
433 var notification = new Notify.Notification(
434 "Changed to branch %s".printf(branchname),
440 notification.set_timeout(5);
444 this.loadBranches(); // update branch list...
445 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
448 bool switchToExistingBranchNamed(string branchname)
451 // this is where it get's tricky...
454 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
455 files = this.git(cmd);
456 stash = files.length> 1 ;
460 if (stash) { this.git(cmd); }
464 cmd = { "checkout", branchname };
467 GitMonitor.gitmonitor.pauseError(e.message);
471 if (branchname != "master") {
472 string[] cmd = { "merge", "master" };
479 string[] cmd = { "checkout", "master" };
481 GitMonitor.gitmonitor.pauseError(
482 "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(
490 string[] cmd = { "stash", "pop" };
493 var fl = files.split("\n");
494 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
495 foreach(var f in fl) {
496 if (f.length < 1) continue;
505 GitMonitor.gitmonitor.pauseError(ee.message);
515 bool createNewBranchNamed(string branchname)
519 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
520 var files = this.git(cmd);
521 stash = files.length> 1 ;
523 cmd = { "checkout", "-b" , branchname };
526 cmd = { "push", "-u" , "origin" ,"HEAD" };
530 var fl = files.split("\n");
531 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
532 foreach(var f in fl) {
533 if (f.length < 1) continue;
542 GitMonitor.gitmonitor.pauseError(ee.message);
553 * add files to track.
555 * @argument {Array} files the files to add.
557 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
559 // should really find out if these are untracked files each..
560 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
561 // not sure if that is how git works.. but just be certian.
563 for (var i = 0; i < files.size;i++) {
564 var f = files.get(i).vname;
566 string[] cmd = { "add", f };
569 ret += e.message + "\n";
576 public bool is_ignore(string fname) throws Error, SpawnError
578 if (fname == ".gitignore") {
579 this.ignore_files.clear();
582 if (this.ignore_files.has_key(fname)) {
583 return this.ignore_files.get(fname);
587 var ret = this.git( { "check-ignore" , fname } );
588 this.ignore_files.set(fname, ret.length > 0);
589 return ret.length > 0;
590 } catch (SpawnError e) {
591 this.ignore_files.set(fname, false);
600 * remove files to track.
602 * @argument {Array} files the files to add.
604 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
606 // this may fail if files do not exist..
607 // should really find out if these are untracked files each..
608 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
609 // not sure if that is how git works.. but just be certian.
612 for (var i = 0; i < files.size;i++) {
613 var f = files.get(i).vname;
615 string[] cmd = { "rm", "-f" , f };
618 ret += e.message + "\n";
631 * @argument {Object} cfg commit configuration
633 * @property {String} name (optional)
634 * @property {String} email (optional)
635 * @property {String} changed (date) (optional)
636 * @property {String} reason (optional)
637 * @property {Array} files - the files that have changed.
641 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
648 if (typeof(cfg.name) != 'undefined') {
650 'author' : cfg.name + ' <' + cfg.email + '>'
653 "GIT_COMMITTER_NAME" + cfg.name,
654 "GIT_COMMITTER_EMAIL" + cfg.email
658 if (typeof(cfg.changed) != 'undefined') {
659 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
663 string[] args = { "commit", "-m" };
664 args += (message.length > 0 ? message : "Changed" );
665 for (var i = 0; i< files.size ; i++ ) {
666 args += files.get(i).vname; // full path?
669 return this.git(args);
674 * Fetch and merge remote repo changes into current branch..
676 * At present we just need this to update the current working branch..
677 * -- maybe later it will have a few options and do more stuff..
680 public string pull () throws Error, SpawnError
682 // should probably hand error conditions better...
683 string[] cmd = { "pull" , "--no-edit" };
684 return this.git( cmd );
689 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
690 public void pull_async(GitAsyncCallback cb)
693 string[] cmd = { "pull" , "--no-edit" };
694 this.git_async( cmd , cb);
701 * Send local changes to remote repo(s)
703 * At present we just need this to push the current branch.
704 * -- maybe later it will have a few options and do more stuff..
707 public string push () throws Error, SpawnError
710 return this.git({ "push" });
718 * The meaty part.. run spawn.. with git..
723 public string git(string[] args_in ) throws Error, SpawnError
725 // convert arguments.
727 string[] args = { "git" };
728 //args += "--git-dir";
729 //args += this.gitdir;
730 args += "--no-pager";
733 //if (this.gitdir != this.repopath) {
734 // args += "--work-tree";
735 // args += this.repopath;
737 for (var i = 0; i < args_in.length;i++) {
741 //this.lastCmd = args.join(" ");
743 GLib.debug( "CWD=%s", this.git_working_dir );
744 GLib.debug( "cmd: %s", string.joinv (" ", args));
748 string home = "HOME=" + Environment.get_home_dir() ;
750 // do not need to set gitpath..
751 //if (File.exists(this.repo + '/.git/config')) {
752 //env.push("GITPATH=" + this.repo );
755 var cfg = new SpawnConfig(this.git_working_dir , args , env);
758 // may throw error...
759 var sp = new Spawn(cfg);
761 // diff output is a bit big..
762 if (args_in[0] != "diff") {
763 GLib.debug( "GOT: %s" , sp.output);
765 // parse output for some commands ?
769 unowned GitAsyncCallback git_async_on_callback;
770 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
772 // convert arguments.
773 this.git_async_on_callback = cb;
774 string[] args = { "git" };
775 //args += "--git-dir";
776 //args += this.gitdir;
777 args += "--no-pager";
780 //if (this.gitdir != this.repopath) {
781 // args += "--work-tree";
782 // args += this.repopath;
784 for (var i = 0; i < args_in.length;i++) {
788 //this.lastCmd = args.join(" ");
790 GLib.debug( "CWD=%s", this.git_working_dir );
791 //print( "cmd: %s\n", string.joinv (" ", args));
795 string home = "HOME=" + Environment.get_home_dir() ;
797 // do not need to set gitpath..
798 //if (File.exists(this.repo + '/.git/config')) {
799 //env.push("GITPATH=" + this.repo );
803 var cfg = new SpawnConfig(this.git_working_dir , args , env);
807 // may throw error...
808 var sp = new Spawn(cfg);
811 sp.run(this.git_async_on_complete);
815 void git_async_on_complete(int err, string output)
817 GLib.debug("GOT %d : %s", err, output);
818 this.git_async_on_callback(this, err, output);
829 public void update_async(GitAsyncCallback cb)
831 string[] cmd = { "fetch" , "--all" };
832 this.git_async( cmd , cb);
837 static uint update_all_total = 0;
838 static string update_all_after = "";
840 public static void updateAll(string after)
842 update_all_after = after;
843 var tr = GitRepo.singleton().cache;
846 update_all_total = tr.size;
847 foreach(var repo in tr.values) {
848 repo.update_async(updateAllCallback);
852 public static void updateAllCallback(GitRepo repo, int err, string res)
858 if (update_all_total > 0 ) {
861 switch (update_all_after) {
863 Clones.singleton().show();