X-Git-Url: http://git.roojs.org/?p=gitlive;a=blobdiff_plain;f=GitRepo.vala;h=a072592724890adc74579337dd4b5b9c8d71607d;hp=cf962d637f6ceeb3463cd3cb5161439bff09ca7f;hb=9e641e366549656621a3e4fc4950b62c4b395650;hpb=1ca3cefd12af4ec1e3cfa3469fbcfc1afc129723 diff --git a/GitRepo.vala b/GitRepo.vala index cf962d63..a0725927 100644 --- a/GitRepo.vala +++ b/GitRepo.vala @@ -18,12 +18,18 @@ public class GitRepo : Object public string gitdir; public string git_working_dir; public bool debug = false; + public bool has_local_changes = false; + public string git_status; + public string git_diff; public Gee.HashMap ignore_files; public GitBranch currentBranch; - + public Gee.HashMap branches; // accessed in GitBranch.. public RooTicket? activeTicket; - + public Gee.HashMap cache; + + + public static GitRepo singleton() { if (_GitRepo == null) { @@ -49,7 +55,7 @@ public class GitRepo : Object } - public Gee.HashMap cache; + @@ -168,6 +174,7 @@ public class GitRepo : Object } this.loadBranches(); this.loadActiveTicket(); + this.loadStatus(); } public bool is_wip_branch() @@ -241,29 +248,18 @@ public class GitRepo : Object } - - Gee.HashMap branches; + public void loadStatus() + { + var r = this.git({ "status" , "--porcelain" }); + this.git_status = r; + this.has_local_changes = r.length > 0; + this.git_diff = this.git({ "diff" , "HEAD", "--no-color" }); + } + public void loadBranches() { - this.branches = new Gee.HashMap(); - - string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" }; - var res = this.git( cmd ); - var lines = res.split("\n"); - for (var i = 0; i < lines.length ; i++) { - var br = new GitBranch(this); - if (!br.parseBranchListItem(lines[i])) { - continue; - } - GLib.debug("add branch %s", br.realName()); - - branches.set(br.realName(), br); - if (br.active) { - this.currentBranch = br; - } - } - + GitBranch.loadBranches(this); } @@ -276,7 +272,7 @@ public class GitRepo : Object if (br.name == "") { continue; } - ret += ret.length > 0 ? "," : ""; + ret += ret.length > 0 ? "\n" : ""; ret += br.name; } @@ -426,66 +422,12 @@ public class GitRepo : Object public bool createBranchNamed(string branchname) { - var stash = false; + if (this.branches.has_key(branchname)) { - // this is where it get's tricky... - try { - string[] cmd = { "ls-files" , "-m" }; // list the modified files.. - var ret = this.git(cmd); - stash = ret.length> 1 ; - - - cmd = { "stash" }; - if (stash) { this.git(cmd); } - - cmd = { "checkout", branchname }; - this.git(cmd); - } catch(Error e) { - GitMonitor.gitmonitor.pauseError(e.message); - return false; - } - try { - if (branchname != "master") { - string[] cmd = { "merge", "master" }; - this.git(cmd); - this.push(); - //cmd = { "commit", "-a" , "-m", "merge master changes" }; - //git chethis.git( cmd ); - } - - } catch(Error e) { - string[] cmd = { "checkout", "master" }; - this.git(cmd); - GitMonitor.gitmonitor.pauseError( - "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( - branchname) - + e.message - ); - return false; - - } - try { - string[] cmd = { "stash", "pop" }; - if (stash) { this.git(cmd); } - } catch(Error ee) { - GitMonitor.gitmonitor.pauseError(ee.message); - return false; - } - this.push(); + this.switchToExistingBranchNamed(branchname); } else { - try { - - string[] cmd = { "checkout", "-b" , branchname }; - this.git(cmd); - - cmd = { "push", "-u" , "origin" ,"HEAD" }; - this.git(cmd); - - } catch(Error ee) { - GitMonitor.gitmonitor.pauseError(ee.message); - return false; - } + this.createNewBranchNamed(branchname); } var notification = new Notify.Notification( @@ -503,6 +445,107 @@ public class GitRepo : Object //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue.. return true; } + bool switchToExistingBranchNamed(string branchname) + { + var stash = false; + // this is where it get's tricky... + string files = ""; + try { + string[] cmd = { "ls-files" , "-m" }; // list the modified files.. + files = this.git(cmd); + stash = files.length> 1 ; + + + cmd = { "stash" }; + if (stash) { this.git(cmd); } + + this.pull(); + + cmd = { "checkout", branchname }; + this.git(cmd); + } catch(Error e) { + GitMonitor.gitmonitor.pauseError(e.message); + return false; + } + try { + if (branchname != "master") { + string[] cmd = { "merge", "master" }; + this.git(cmd); + this.push(); + + } + + } catch(Error e) { + string[] cmd = { "checkout", "master" }; + this.git(cmd); + GitMonitor.gitmonitor.pauseError( + "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( + branchname) + + e.message + ); + return false; + + } + try { + string[] cmd = { "stash", "pop" }; + if (stash) { + this.git(cmd); + var fl = files.split("\n"); + cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) }; + foreach(var f in fl) { + if (f.length < 1) continue; + cmd += f; + } + this.git(cmd); + } + + + + } catch(Error ee) { + GitMonitor.gitmonitor.pauseError(ee.message); + return false; + } + this.push(); + return true; + + } + + + + bool createNewBranchNamed(string branchname) + { + var stash = false; + try { + string[] cmd = { "ls-files" , "-m" }; // list the modified files.. + var files = this.git(cmd); + stash = files.length> 1 ; + + cmd = { "checkout", "-b" , branchname }; + this.git(cmd); + + cmd = { "push", "-u" , "origin" ,"HEAD" }; + this.git(cmd); + if (stash) { + + var fl = files.split("\n"); + cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) }; + foreach(var f in fl) { + if (f.length < 1) continue; + cmd += f; + } + this.git(cmd); + this.push(); + } + + + } catch(Error ee) { + GitMonitor.gitmonitor.pauseError(ee.message); + return false; + } + return true; + + } + /** @@ -647,7 +690,7 @@ public class GitRepo : Object public void pull_async(GitAsyncCallback cb) { - string[] cmd = { "pull" , "--no-edit" }; + string[] cmd = { "pull" , "--no-edit" }; this.git_async( cmd , cb); @@ -711,7 +754,7 @@ public class GitRepo : Object var cfg = new SpawnConfig(this.git_working_dir , args , env); - + //cfg.debug = true; // may throw error... var sp = new Spawn(cfg); @@ -778,4 +821,52 @@ public class GitRepo : Object } + + + + + public void update_async(GitAsyncCallback cb) + { + string[] cmd = { "fetch" , "--all" }; + this.git_async( cmd , cb); + + } + + + static uint update_all_total = 0; + static string update_all_after = ""; + + public static void updateAll(string after) + { + update_all_after = after; + var tr = GitRepo.singleton().cache; + + + update_all_total = tr.size; + foreach(var repo in tr.values) { + repo.update_async(updateAllCallback); + } + + } + public static void updateAllCallback(GitRepo repo, int err, string res) + { + repo.loadBranches(); + repo.loadStatus(); + + update_all_total--; + if (update_all_total > 0 ) { + return; + } + switch (update_all_after) { + case "show_clones": + Clones.singleton().show(); + break; + default: + break; + } + return; + } + + + }