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;
22 public Gee.HashMap<string,bool> ignore_files;
23 public GitBranch currentBranch;
25 public RooTicket? activeTicket;
27 public static GitRepo singleton()
29 if (_GitRepo == null) {
30 _GitRepo = new GitRepo.single();
31 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
37 * index of.. matching gitpath..
39 public static int indexOf( Array<GitRepo> repos, string gitpath) {
40 // make a fake object to compare against..
41 var test_repo = GitRepo.get(gitpath);
43 for(var i =0; i < repos.length; i++) {
44 if (repos.index(i).gitdir == test_repo.gitdir) {
52 public Gee.HashMap<string,GitRepo> cache;
56 public static Array<GitRepo> list()
59 //if (GitRepo.list_cache != null) {
60 // unowned Array<GitRepo> ret = GitRepo.list_cache;
63 var cache = GitRepo.singleton().cache;
64 var list_cache = new Array<GitRepo>();
66 var dir = Environment.get_home_dir() + "/gitlive";
68 var f = File.new_for_path(dir);
69 FileEnumerator file_enum;
71 file_enum = f.enumerate_children(
72 FileAttribute.STANDARD_DISPLAY_NAME + ","+
73 FileAttribute.STANDARD_TYPE,
74 FileQueryInfoFlags.NONE,
87 next_file = file_enum.next_file(null);
88 if (next_file == null) {
93 GLib.debug("Error: %s",e.message);
97 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
99 if (next_file.get_file_type() != FileType.DIRECTORY) {
104 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
109 if (next_file.get_display_name()[0] == '.') {
113 var sp = dir+"/"+next_file.get_display_name();
115 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
117 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
121 var rep = GitRepo.get( sp );
122 list_cache.append_val(rep);
132 public static GitRepo get(string path)
134 var cache = GitRepo.singleton().cache;
135 if (cache.has_key(path)) {
136 return cache.get(path);
138 return new GitRepo(path);
141 private GitRepo.single() {
142 // used to create the signleton
147 * @param {Object} cfg - Configuration
148 * (basically repopath is currently only critical one.)
152 private GitRepo(string path) {
154 this.name = File.new_for_path(path).get_basename();
155 this.ignore_files = new Gee.HashMap<string,bool>();
157 this.git_working_dir = path;
158 this.gitdir = path + "/.git";
159 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
160 this.gitdir = path; // naked...
162 this.cmds = new Gee.ArrayList<GitMonitorQueue> ();
164 var cache = GitRepo.singleton().cache;
165 //Repo.superclass.constructor.call(this,cfg);
166 if ( !cache.has_key(path) ) {
167 cache.set( path, this);
172 public bool is_wip_branch()
174 return this.currentBranch.name.has_prefix("wip_");
178 public bool is_autocommit ()
180 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
182 public bool is_autopush ()
184 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
187 Gee.HashMap<string,GitBranch> branches;
189 public void loadBranches()
191 this.branches = new Gee.HashMap<string,GitBranch>();
193 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
194 var res = this.git( cmd );
195 var lines = res.split("\n");
196 for (var i = 0; i < lines.length ; i++) {
197 var br = new GitBranch(this);
198 if (!br.parseBranchListItem(lines[i])) {
201 GLib.debug("add branch %s", br.realName());
203 branches.set(br.realName(), br);
205 this.currentBranch = br;
214 public string branchesToString()
217 foreach( var br in this.branches.values) {
221 ret += ret.length > 0 ? "," : "";
228 RooTicket? ticket = null;
230 public void setActiveTicket(RooTicket ticket, string branchname)
232 // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
233 // also need to decide which branch we will merge into?
235 if (action == "CLOSE" || action == "LEAVE") {
239 var oldbranch = this.currentBranch.name;
240 this.setActiveTicket(null, "master");
241 string [] cmd = { "merge", "--squash", oldbranch };
243 cmd = { "commit", "--m", commit_message };
245 this.loadBranches(); // updates lastrev..
247 var notification = new Notify.Notification(
248 "Merged branch %s to master".printf(oldbranch),
254 notification.set_timeout(5);
262 GitMonitor.gitmonitor.pauseError(e.message);
265 // error~?? -- show the error dialog...
268 if (action == "MASTER") {
269 // merge master into ours..
271 string[] cmd = { "merge", "master" };
273 var notification = new Notify.Notification(
274 "Merged code from master to %s".printf(this.currentBranch.name),
279 notification.set_timeout(5);
284 GitMonitor.gitmonitor.pauseError(e.message);
288 if (action == "EXIT") {
290 var oldbranch = this.currentBranch.name;
291 this.setActiveTicket(null, "master");
293 var notification = new Notify.Notification(
294 "Left branch %s".printf(oldbranch),
299 notification.set_timeout(5);
304 GitMonitor.gitmonitor.pauseError(e.message);
308 // error~?? -- show the error dialog...
315 public bool setActiveTicket(RooTicket ticket, string branchname)
317 if (!this.createBranchNamed(branchname)) {
320 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
321 this.activeTicket = ticket;
325 public bool createBranchNamed(string branchname)
327 string[] cmd = { "checkout", "-b" , branchname };
329 this.loadBranches(); // update branch list...
330 GitMonitor.gitmonitor.runQueue(); // commit any outstanding...
337 * add files to track.
339 * @argument {Array} files the files to add.
341 public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
343 // should really find out if these are untracked files each..
344 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
345 // not sure if that is how git works.. but just be certian.
347 for (var i = 0; i < files.size;i++) {
348 var f = files.get(i).vname;
350 string[] cmd = { "add", f };
353 ret += e.message + "\n";
360 public bool is_ignore(string fname) throws Error, SpawnError
362 if (fname == ".gitignore") {
363 this.ignore_files.clear();
366 if (this.ignore_files.has_key(fname)) {
367 return this.ignore_files.get(fname);
371 var ret = this.git( { "check-ignore" , fname } );
372 this.ignore_files.set(fname, ret.length > 0);
373 return ret.length > 0;
374 } catch (SpawnError e) {
375 this.ignore_files.set(fname, false);
384 * remove files to track.
386 * @argument {Array} files the files to add.
388 public string remove ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
390 // this may fail if files do not exist..
391 // should really find out if these are untracked files each..
392 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
393 // not sure if that is how git works.. but just be certian.
396 for (var i = 0; i < files.size;i++) {
397 var f = files.get(i).vname;
399 string[] cmd = { "rm", "-f" , f };
402 ret += e.message + "\n";
415 * @argument {Object} cfg commit configuration
417 * @property {String} name (optional)
418 * @property {String} email (optional)
419 * @property {String} changed (date) (optional)
420 * @property {String} reason (optional)
421 * @property {Array} files - the files that have changed.
425 public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
432 if (typeof(cfg.name) != 'undefined') {
434 'author' : cfg.name + ' <' + cfg.email + '>'
437 "GIT_COMMITTER_NAME" + cfg.name,
438 "GIT_COMMITTER_EMAIL" + cfg.email
442 if (typeof(cfg.changed) != 'undefined') {
443 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
447 string[] args = { "commit", "-m" };
448 args += (message.length > 0 ? message : "Changed" );
449 for (var i = 0; i< files.size ; i++ ) {
450 args += files.get(i).vname; // full path?
453 return this.git(args);
458 * Fetch and merge remote repo changes into current branch..
460 * At present we just need this to update the current working branch..
461 * -- maybe later it will have a few options and do more stuff..
464 public string pull () throws Error, SpawnError
466 // should probably hand error conditions better...
467 string[] cmd = { "pull" , "--no-edit" };
468 return this.git( cmd );
473 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
474 public void pull_async(GitAsyncCallback cb)
477 string[] cmd = { "pull" , "--no-edit" };
478 this.git_async( cmd , cb);
485 * Send local changes to remote repo(s)
487 * At present we just need this to push the current branch.
488 * -- maybe later it will have a few options and do more stuff..
491 public string push () throws Error, SpawnError
494 return this.git({ "push", "origin", "HEAD" });
502 * The meaty part.. run spawn.. with git..
507 public string git(string[] args_in ) throws Error, SpawnError
509 // convert arguments.
511 string[] args = { "git" };
512 //args += "--git-dir";
513 //args += this.gitdir;
514 args += "--no-pager";
517 //if (this.gitdir != this.repopath) {
518 // args += "--work-tree";
519 // args += this.repopath;
521 for (var i = 0; i < args_in.length;i++) {
525 //this.lastCmd = args.join(" ");
527 GLib.debug( "CWD=%s", this.git_working_dir );
528 GLib.debug( "cmd: %s", string.joinv (" ", args));
532 string home = "HOME=" + Environment.get_home_dir() ;
534 // do not need to set gitpath..
535 //if (File.exists(this.repo + '/.git/config')) {
536 //env.push("GITPATH=" + this.repo );
540 var cfg = new SpawnConfig(this.git_working_dir , args , env);
543 // may throw error...
544 var sp = new Spawn(cfg);
547 GLib.debug( "GOT: %s" , sp.output);
548 // parse output for some commands ?
552 unowned GitAsyncCallback git_async_on_callback;
553 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
555 // convert arguments.
556 this.git_async_on_callback = cb;
557 string[] args = { "git" };
558 //args += "--git-dir";
559 //args += this.gitdir;
560 args += "--no-pager";
563 //if (this.gitdir != this.repopath) {
564 // args += "--work-tree";
565 // args += this.repopath;
567 for (var i = 0; i < args_in.length;i++) {
571 //this.lastCmd = args.join(" ");
573 GLib.debug( "CWD=%s", this.git_working_dir );
574 //print( "cmd: %s\n", string.joinv (" ", args));
578 string home = "HOME=" + Environment.get_home_dir() ;
580 // do not need to set gitpath..
581 //if (File.exists(this.repo + '/.git/config')) {
582 //env.push("GITPATH=" + this.repo );
586 var cfg = new SpawnConfig(this.git_working_dir , args , env);
590 // may throw error...
591 var sp = new Spawn(cfg);
594 sp.run(this.git_async_on_complete);
598 void git_async_on_complete(int err, string output)
600 GLib.debug("GOT %d : %s", err, output);
601 this.git_async_on_callback(this, err, output);