10 static GitRepo _GitRepo;
12 public class GitRepo : Object
15 public Array<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 Array<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);
173 public bool is_autocommit ()
175 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
177 public bool is_autopush ()
179 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
182 Gee.HashMap<string,GitBranch> branches;
184 public void loadBranches()
186 this.branches = new Gee.HashMap<string,GitBranch>();
188 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
189 var res = this.git( cmd );
190 var lines = res.split("\n");
191 for (var i = 0; i < lines.length ; i++) {
192 var br = new GitBranch(this);
193 if (!br.parseBranchListItem(lines[i])) {
196 GLib.debug("add branch %s", br.realName());
198 branches.set(br.realName(), br);
200 this.currentBranch = br;
209 public string branchesToString()
212 foreach( var br in this.branches.values) {
216 ret += ret.length > 0 ? "," : "";
223 RooTicket? ticket = null;
225 public void setActiveTicket(RooTicket ticket, string branchname)
227 this.createBranchNamed(branchname);
228 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
229 this.activeTicket = ticket;
232 public void createBranchNamed(string branchname)
234 string[] cmd = { "checkout", "-b" , branchname };
236 this.loadBranches(); // update branch list...
242 * add files to track.
244 * @argument {Array} files the files to add.
246 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
248 // should really find out if these are untracked files each..
249 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
250 // not sure if that is how git works.. but just be certian.
252 for (var i = 0; i < files.length;i++) {
253 var f = files.index(i).vname;
255 string[] cmd = { "add", f };
258 ret += e.message + "\n";
265 public bool is_ignore(string fname) throws Error, SpawnError
267 if (fname == ".gitignore") {
268 this.ignore_files.clear();
271 if (this.ignore_files.has_key(fname)) {
272 return this.ignore_files.get(fname);
276 var ret = this.git( { "check-ignore" , fname } );
277 this.ignore_files.set(fname, ret.length > 0);
278 return ret.length > 0;
279 } catch (SpawnError e) {
280 this.ignore_files.set(fname, false);
289 * remove files to track.
291 * @argument {Array} files the files to add.
293 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
295 // this may fail if files do not exist..
296 // should really find out if these are untracked files each..
297 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
298 // not sure if that is how git works.. but just be certian.
301 for (var i = 0; i < files.length;i++) {
302 var f = files.index(i).vname;
304 string[] cmd = { "rm", "-f" , f };
307 ret += e.message + "\n";
320 * @argument {Object} cfg commit configuration
322 * @property {String} name (optional)
323 * @property {String} email (optional)
324 * @property {String} changed (date) (optional)
325 * @property {String} reason (optional)
326 * @property {Array} files - the files that have changed.
330 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
337 if (typeof(cfg.name) != 'undefined') {
339 'author' : cfg.name + ' <' + cfg.email + '>'
342 "GIT_COMMITTER_NAME" + cfg.name,
343 "GIT_COMMITTER_EMAIL" + cfg.email
347 if (typeof(cfg.changed) != 'undefined') {
348 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
352 string[] args = { "commit", "-m" };
353 args += (message.length > 0 ? message : "Changed" );
354 for (var i = 0; i< files.length ; i++ ) {
355 args += files.index(i).vname; // full path?
358 return this.git(args);
363 * Fetch and merge remote repo changes into current branch..
365 * At present we just need this to update the current working branch..
366 * -- maybe later it will have a few options and do more stuff..
369 public string pull () throws Error, SpawnError
371 // should probably hand error conditions better...
372 string[] cmd = { "pull" , "--no-edit" };
373 return this.git( cmd );
378 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
379 public void pull_async(GitAsyncCallback cb)
382 string[] cmd = { "pull" , "--no-edit" };
383 this.git_async( cmd , cb);
390 * Send local changes to remote repo(s)
392 * At present we just need this to push the current branch.
393 * -- maybe later it will have a few options and do more stuff..
396 public string push () throws Error, SpawnError
399 return this.git({ "push", "origin", "HEAD" });
407 * The meaty part.. run spawn.. with git..
412 public string git(string[] args_in ) throws Error, SpawnError
414 // convert arguments.
416 string[] args = { "git" };
417 //args += "--git-dir";
418 //args += this.gitdir;
419 args += "--no-pager";
422 //if (this.gitdir != this.repopath) {
423 // args += "--work-tree";
424 // args += this.repopath;
426 for (var i = 0; i < args_in.length;i++) {
430 //this.lastCmd = args.join(" ");
432 GLib.debug( "CWD=%s", this.git_working_dir );
433 GLib.debug( "cmd: %s", string.joinv (" ", args));
437 string home = "HOME=" + Environment.get_home_dir() ;
439 // do not need to set gitpath..
440 //if (File.exists(this.repo + '/.git/config')) {
441 //env.push("GITPATH=" + this.repo );
445 var cfg = new SpawnConfig(this.git_working_dir , args , env);
448 // may throw error...
449 var sp = new Spawn(cfg);
452 GLib.debug( "GOT: %s" , sp.output);
453 // parse output for some commands ?
457 unowned GitAsyncCallback git_async_on_callback;
458 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
460 // convert arguments.
461 this.git_async_on_callback = cb;
462 string[] args = { "git" };
463 //args += "--git-dir";
464 //args += this.gitdir;
465 args += "--no-pager";
468 //if (this.gitdir != this.repopath) {
469 // args += "--work-tree";
470 // args += this.repopath;
472 for (var i = 0; i < args_in.length;i++) {
476 //this.lastCmd = args.join(" ");
478 GLib.debug( "CWD=%s", this.git_working_dir );
479 //print( "cmd: %s\n", string.joinv (" ", args));
483 string home = "HOME=" + Environment.get_home_dir() ;
485 // do not need to set gitpath..
486 //if (File.exists(this.repo + '/.git/config')) {
487 //env.push("GITPATH=" + this.repo );
491 var cfg = new SpawnConfig(this.git_working_dir , args , env);
495 // may throw error...
496 var sp = new Spawn(cfg);
499 sp.run(this.git_async_on_complete);
503 void git_async_on_complete(int err, string output)
505 GLib.debug("GOT %d : %s", err, output);
506 this.git_async_on_callback(this, err, output);