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);
139 public static GitRepo get(string path)
141 var cache = GitRepo.singleton().cache;
142 if (cache.has_key(path)) {
143 return cache.get(path);
145 return new GitRepo(path);
148 private GitRepo.single() {
149 // used to create the signleton
154 * @param {Object} cfg - Configuration
155 * (basically repopath is currently only critical one.)
159 private GitRepo(string path) {
161 this.name = File.new_for_path(path).get_basename();
162 this.ignore_files = new Gee.HashMap<string,bool>();
164 this.git_working_dir = path;
165 this.gitdir = path + "/.git";
166 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
167 this.gitdir = path; // naked...
169 this.cmds = new Gee.ArrayList<GitMonitorQueue> ();
171 var cache = GitRepo.singleton().cache;
172 //Repo.superclass.constructor.call(this,cfg);
173 if ( !cache.has_key(path) ) {
174 cache.set( path, this);
177 this.loadActiveTicket();
181 public bool is_wip_branch()
183 return this.currentBranch.name.has_prefix("wip_");
187 public bool is_autocommit ()
189 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
191 public void set_autocommit(bool val)
194 var cur = this.is_autocommit();
195 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
197 return; // no change..
200 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
203 FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" );
208 public bool is_auto_branch ()
210 return FileUtils.test(this.gitdir + "/.gitlive-enable-auto-branch" , FileTest.EXISTS);
213 public void set_auto_branch(bool val)
216 var cur = this.is_auto_branch();
217 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
220 return; // no change..
223 FileUtils.set_contents(this.gitdir + "/.gitlive-enable-auto-branch" , "x");
226 FileUtils.remove(this.gitdir + "/.gitlive-enable-auto-branch" );
230 public bool is_autopush ()
232 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
234 public void set_autopush(bool val)
237 var cur = this.is_autopush();
238 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF", cur ? "ON" : "OFF");
240 return; // no change..
243 FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
246 FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" );
252 public void loadStatus()
254 var r = this.git({ "status" , "--porcelain" });
256 this.has_local_changes = r.length > 0;
258 var rs = this.git({ "status" , "-sb" });
260 this.ahead_or_behind = rs.contains("[ahead") ? "A" : (rs.contains("[behind") ? "B" : "");
263 this.git_diff = this.git({ "diff" , "HEAD", "--no-color" });
267 public void loadBranches()
269 GitBranch.loadBranches(this);
275 public string branchesToString()
278 foreach( var br in this.branches.values) {
282 ret += ret.length > 0 ? "\n" : "";
289 public static void doMerges(string action, string ticket_id, string commit_message)
291 GitMonitor.gitmonitor.stop();
295 foreach(var repo in GitRepo.singleton().cache.values) {
296 if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
297 var res = repo.doMerge(action,ticket_id, commit_message);
302 commitrevs += commitrevs.length > 0 ? " " : "";
303 commitrevs += repo.currentBranch.lastrev;
306 if (sucess && action == "CLOSE") {
307 RooTicket.singleton().getById(ticket_id).close(commitrevs);
309 GitMonitor.gitmonitor.start();
313 public bool doMerge(string action, string ticket_id, string commit_message)
315 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
316 // also need to decide which branch we will merge into?
318 if (action == "CLOSE" || action == "LEAVE") {
321 var oldbranch = this.currentBranch.name;
322 this.setActiveTicket(null, "master");
323 string [] cmd = { "merge", "--squash", oldbranch };
325 cmd = { "commit", "-a" , "-m", commit_message };
328 this.loadBranches(); // updates lastrev..
330 var notification = new Notify.Notification(
331 "Merged branch %s to master".printf(oldbranch),
337 notification.set_timeout(5);
345 GitMonitor.gitmonitor.pauseError(e.message);
348 // error~?? -- show the error dialog...
351 if (action == "MASTER") {
352 // merge master into ours..
354 string[] cmd = { "merge", "master" };
356 var notification = new Notify.Notification(
357 "Merged code from master to %s".printf(this.currentBranch.name),
362 notification.set_timeout(5);
367 GitMonitor.gitmonitor.pauseError(e.message);
371 if (action == "EXIT") {
373 var oldbranch = this.currentBranch.name;
374 this.setActiveTicket(null, "master");
376 var notification = new Notify.Notification(
377 "Left branch %s".printf(oldbranch),
382 notification.set_timeout(5);
387 GitMonitor.gitmonitor.pauseError(e.message);
391 // error~?? -- show the error dialog...
397 public void loadActiveTicket()
399 this.activeTicket = null;
400 if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
404 FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);
405 if (ticket_id.length < 1) {
408 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
415 public bool setActiveTicket(RooTicket? ticket, string branchname)
417 if (!this.createBranchNamed(branchname)) {
420 if (ticket != null) {
421 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
423 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
425 this.activeTicket = ticket;
429 public bool createBranchNamed(string branchname)
433 if (this.branches.has_key(branchname)) {
434 this.switchToExistingBranchNamed(branchname);
437 this.createNewBranchNamed(branchname);
440 var notification = new Notify.Notification(
441 "Changed to branch %s".printf(branchname),
447 notification.set_timeout(5);
451 this.loadBranches(); // update branch list...
452 //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
455 bool switchToExistingBranchNamed(string branchname)
458 // this is where it get's tricky...
461 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
462 files = this.git(cmd);
463 stash = files.length> 1 ;
467 if (stash) { this.git(cmd); }
471 cmd = { "checkout", branchname };
474 GitMonitor.gitmonitor.pauseError(e.message);
478 if (branchname != "master") {
479 string[] cmd = { "merge", "master" };
486 string[] cmd = { "checkout", "master" };
488 GitMonitor.gitmonitor.pauseError(
489 "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(
497 string[] cmd = { "stash", "pop" };
500 var fl = files.split("\n");
501 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
502 foreach(var f in fl) {
503 if (f.length < 1) continue;
512 GitMonitor.gitmonitor.pauseError(ee.message);
522 bool createNewBranchNamed(string branchname)
526 string[] cmd = { "ls-files" , "-m" }; // list the modified files..
527 var files = this.git(cmd);
528 stash = files.length> 1 ;
530 cmd = { "checkout", "-b" , branchname };
533 cmd = { "push", "-u" , "origin" ,"HEAD" };
537 var fl = files.split("\n");
538 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
539 foreach(var f in fl) {
540 if (f.length < 1) continue;
549 GitMonitor.gitmonitor.pauseError(ee.message);
560 * add files to track.
562 * @argument {Array} files the files to add.
564 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
566 // should really find out if these are untracked files each..
567 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
568 // not sure if that is how git works.. but just be certian.
570 for (var i = 0; i < files.size;i++) {
571 var f = files.get(i).vname;
573 string[] cmd = { "add", f };
576 ret += e.message + "\n";
583 public bool is_ignore(string fname) throws Error, SpawnError
585 if (fname == ".gitignore") {
586 this.ignore_files.clear();
589 if (this.ignore_files.has_key(fname)) {
590 return this.ignore_files.get(fname);
594 var ret = this.git( { "check-ignore" , fname } );
595 this.ignore_files.set(fname, ret.length > 0);
596 return ret.length > 0;
597 } catch (SpawnError e) {
598 this.ignore_files.set(fname, false);
607 * remove files to track.
609 * @argument {Array} files the files to add.
611 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
613 // this may fail if files do not exist..
614 // should really find out if these are untracked files each..
615 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
616 // not sure if that is how git works.. but just be certian.
619 for (var i = 0; i < files.size;i++) {
620 var f = files.get(i).vname;
622 string[] cmd = { "rm", "-f" , f };
625 ret += e.message + "\n";
638 * @argument {Object} cfg commit configuration
640 * @property {String} name (optional)
641 * @property {String} email (optional)
642 * @property {String} changed (date) (optional)
643 * @property {String} reason (optional)
644 * @property {Array} files - the files that have changed.
648 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
655 if (typeof(cfg.name) != 'undefined') {
657 'author' : cfg.name + ' <' + cfg.email + '>'
660 "GIT_COMMITTER_NAME" + cfg.name,
661 "GIT_COMMITTER_EMAIL" + cfg.email
665 if (typeof(cfg.changed) != 'undefined') {
666 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
670 string[] args = { "commit", "-m" };
671 args += (message.length > 0 ? message : "Changed" );
672 for (var i = 0; i< files.size ; i++ ) {
673 args += files.get(i).vname; // full path?
676 return this.git(args);
681 * Fetch and merge remote repo changes into current branch..
683 * At present we just need this to update the current working branch..
684 * -- maybe later it will have a few options and do more stuff..
687 public string pull () throws Error, SpawnError
689 // should probably hand error conditions better...
690 string[] cmd = { "pull" , "--no-edit" };
691 return this.git( cmd );
696 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
697 public void pull_async(GitAsyncCallback cb)
700 string[] cmd = { "pull" , "--no-edit" };
701 this.git_async( cmd , cb);
708 * Send local changes to remote repo(s)
710 * At present we just need this to push the current branch.
711 * -- maybe later it will have a few options and do more stuff..
714 public string push () throws Error, SpawnError
717 return this.git({ "push" });
725 * The meaty part.. run spawn.. with git..
730 public string git(string[] args_in ) throws Error, SpawnError
732 // convert arguments.
734 string[] args = { "git" };
735 //args += "--git-dir";
736 //args += this.gitdir;
737 args += "--no-pager";
740 //if (this.gitdir != this.repopath) {
741 // args += "--work-tree";
742 // args += this.repopath;
744 for (var i = 0; i < args_in.length;i++) {
748 //this.lastCmd = args.join(" ");
750 GLib.debug( "CWD=%s", this.git_working_dir );
751 GLib.debug( "cmd: %s", string.joinv (" ", args));
755 string home = "HOME=" + Environment.get_home_dir() ;
757 // do not need to set gitpath..
758 //if (File.exists(this.repo + '/.git/config')) {
759 //env.push("GITPATH=" + this.repo );
762 var cfg = new SpawnConfig(this.git_working_dir , args , env);
765 // may throw error...
766 var sp = new Spawn(cfg);
768 // diff output is a bit big..
769 if (args_in[0] != "diff") {
770 GLib.debug( "GOT: %s" , sp.output);
772 // parse output for some commands ?
776 unowned GitAsyncCallback git_async_on_callback;
777 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
779 // convert arguments.
780 this.git_async_on_callback = cb;
781 string[] args = { "git" };
782 //args += "--git-dir";
783 //args += this.gitdir;
784 args += "--no-pager";
787 //if (this.gitdir != this.repopath) {
788 // args += "--work-tree";
789 // args += this.repopath;
791 for (var i = 0; i < args_in.length;i++) {
795 //this.lastCmd = args.join(" ");
797 GLib.debug( "CWD=%s", this.git_working_dir );
798 //print( "cmd: %s\n", string.joinv (" ", args));
802 string home = "HOME=" + Environment.get_home_dir() ;
804 // do not need to set gitpath..
805 //if (File.exists(this.repo + '/.git/config')) {
806 //env.push("GITPATH=" + this.repo );
810 var cfg = new SpawnConfig(this.git_working_dir , args , env);
814 // may throw error...
815 var sp = new Spawn(cfg);
818 sp.run(this.git_async_on_complete);
822 void git_async_on_complete(int err, string output)
824 GLib.debug("GOT %d : %s", err, output);
825 this.git_async_on_callback(this, err, output);
836 public void update_async(GitAsyncCallback cb)
838 string[] cmd = { "fetch" , "--all" };
839 this.git_async( cmd , cb);
844 static uint update_all_total = 0;
845 static string update_all_after = "";
847 public static void updateAll(string after)
849 update_all_after = after;
850 var tr = GitRepo.singleton().cache;
853 update_all_total = tr.size;
854 foreach(var repo in tr.values) {
855 repo.update_async(updateAllCallback);
859 public static void updateAllCallback(GitRepo repo, int err, string res)
865 if (update_all_total > 0 ) {
868 switch (update_all_after) {
870 Clones.singleton().show();