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;
26 public static GitRepo singleton()
28 if (_GitRepo == null) {
29 _GitRepo = new GitRepo.single();
30 _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
36 * index of.. matching gitpath..
38 public static int indexOf( Array<GitRepo> repos, string gitpath) {
39 // make a fake object to compare against..
40 var test_repo = GitRepo.get(gitpath);
42 for(var i =0; i < repos.length; i++) {
43 if (repos.index(i).gitdir == test_repo.gitdir) {
51 public Gee.HashMap<string,GitRepo> cache;
55 public static Array<GitRepo> list()
58 //if (GitRepo.list_cache != null) {
59 // unowned Array<GitRepo> ret = GitRepo.list_cache;
62 var cache = GitRepo.singleton().cache;
63 var list_cache = new Array<GitRepo>();
65 var dir = Environment.get_home_dir() + "/gitlive";
67 var f = File.new_for_path(dir);
68 FileEnumerator file_enum;
70 file_enum = f.enumerate_children(
71 FileAttribute.STANDARD_DISPLAY_NAME + ","+
72 FileAttribute.STANDARD_TYPE,
73 FileQueryInfoFlags.NONE,
86 next_file = file_enum.next_file(null);
87 if (next_file == null) {
92 GLib.debug("Error: %s",e.message);
96 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
98 if (next_file.get_file_type() != FileType.DIRECTORY) {
103 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
108 if (next_file.get_display_name()[0] == '.') {
112 var sp = dir+"/"+next_file.get_display_name();
114 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
116 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
120 var rep = GitRepo.get( sp );
121 list_cache.append_val(rep);
131 public static GitRepo get(string path)
133 var cache = GitRepo.singleton().cache;
134 if (cache.has_key(path)) {
135 return cache.get(path);
137 return new GitRepo(path);
140 private GitRepo.single() {
141 // used to create the signleton
146 * @param {Object} cfg - Configuration
147 * (basically repopath is currently only critical one.)
151 private GitRepo(string path) {
153 this.name = File.new_for_path(path).get_basename();
154 this.ignore_files = new Gee.HashMap<string,bool>();
156 this.git_working_dir = path;
157 this.gitdir = path + "/.git";
158 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
159 this.gitdir = path; // naked...
161 this.cmds = new Array<GitMonitorQueue> ();
163 var cache = GitRepo.singleton().cache;
164 //Repo.superclass.constructor.call(this,cfg);
165 if ( !cache.has_key(path) ) {
166 cache.set( path, this);
171 public bool is_autocommit ()
173 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
175 public bool is_autopush ()
177 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
180 Gee.HashMap<string,GitBranch> branches;
182 public void loadBranches()
184 this.branches = new Gee.HashMap<string,GitBranch>();
186 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
187 var res = this.git( cmd );
188 var lines = res.split("\n");
189 for (var i = 0; i < lines.length ; i++) {
190 var br = new GitBranch(this);
191 if (!br.parseBranchListItem(lines[i])) {
194 GLib.debug("add branch %s", br.realName());
196 branches.set(br.realName(), br);
198 this.currentBranch = br;
203 public string branchesToString()
206 foreach( var br in this.branches.values) {
210 ret += ret.length > 0 ? "," : "";
217 RooTicket? ticket = null;
219 public void setActiveTicket(RooTicket ticket, string branchname)
221 this.createBranchNamed(branchname);
222 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
223 this.activeTicket = ticket;
226 public void createBranchNamed(string branchname)
228 string[] cmd = { "checkout", "-b" , branchname };
230 this.loadBranches(); // update branch list...
236 * add files to track.
238 * @argument {Array} files the files to add.
240 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
242 // should really find out if these are untracked files each..
243 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
244 // not sure if that is how git works.. but just be certian.
246 for (var i = 0; i < files.length;i++) {
247 var f = files.index(i).vname;
249 string[] cmd = { "add", f };
252 ret += e.message + "\n";
259 public bool is_ignore(string fname) throws Error, SpawnError
261 if (fname == ".gitignore") {
262 this.ignore_files.clear();
265 if (this.ignore_files.has_key(fname)) {
266 return this.ignore_files.get(fname);
270 var ret = this.git( { "check-ignore" , fname } );
271 this.ignore_files.set(fname, ret.length > 0);
272 return ret.length > 0;
273 } catch (SpawnError e) {
274 this.ignore_files.set(fname, false);
283 * remove files to track.
285 * @argument {Array} files the files to add.
287 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
289 // this may fail if files do not exist..
290 // should really find out if these are untracked files each..
291 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
292 // not sure if that is how git works.. but just be certian.
295 for (var i = 0; i < files.length;i++) {
296 var f = files.index(i).vname;
298 string[] cmd = { "rm", "-f" , f };
301 ret += e.message + "\n";
314 * @argument {Object} cfg commit configuration
316 * @property {String} name (optional)
317 * @property {String} email (optional)
318 * @property {String} changed (date) (optional)
319 * @property {String} reason (optional)
320 * @property {Array} files - the files that have changed.
324 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
331 if (typeof(cfg.name) != 'undefined') {
333 'author' : cfg.name + ' <' + cfg.email + '>'
336 "GIT_COMMITTER_NAME" + cfg.name,
337 "GIT_COMMITTER_EMAIL" + cfg.email
341 if (typeof(cfg.changed) != 'undefined') {
342 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
346 string[] args = { "commit", "-m" };
347 args += (message.length > 0 ? message : "Changed" );
348 for (var i = 0; i< files.length ; i++ ) {
349 args += files.index(i).vname; // full path?
352 return this.git(args);
357 * Fetch and merge remote repo changes into current branch..
359 * At present we just need this to update the current working branch..
360 * -- maybe later it will have a few options and do more stuff..
363 public string pull () throws Error, SpawnError
365 // should probably hand error conditions better...
366 string[] cmd = { "pull" , "--no-edit" };
367 return this.git( cmd );
372 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
373 public void pull_async(GitAsyncCallback cb)
376 string[] cmd = { "pull" , "--no-edit" };
377 this.git_async( cmd , cb);
384 * Send local changes to remote repo(s)
386 * At present we just need this to push the current branch.
387 * -- maybe later it will have a few options and do more stuff..
390 public string push () throws Error, SpawnError
393 return this.git({ "push", "origin", "HEAD" });
401 * The meaty part.. run spawn.. with git..
406 public string git(string[] args_in ) throws Error, SpawnError
408 // convert arguments.
410 string[] args = { "git" };
411 //args += "--git-dir";
412 //args += this.gitdir;
413 args += "--no-pager";
416 //if (this.gitdir != this.repopath) {
417 // args += "--work-tree";
418 // args += this.repopath;
420 for (var i = 0; i < args_in.length;i++) {
424 //this.lastCmd = args.join(" ");
426 GLib.debug( "CWD=%s", this.git_working_dir );
427 GLib.debug( "cmd: %s", string.joinv (" ", args));
431 string home = "HOME=" + Environment.get_home_dir() ;
433 // do not need to set gitpath..
434 //if (File.exists(this.repo + '/.git/config')) {
435 //env.push("GITPATH=" + this.repo );
439 var cfg = new SpawnConfig(this.git_working_dir , args , env);
442 // may throw error...
443 var sp = new Spawn(cfg);
446 GLib.debug( "GOT: %s" , sp.output);
447 // parse output for some commands ?
451 unowned GitAsyncCallback git_async_on_callback;
452 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
454 // convert arguments.
455 this.git_async_on_callback = cb;
456 string[] args = { "git" };
457 //args += "--git-dir";
458 //args += this.gitdir;
459 args += "--no-pager";
462 //if (this.gitdir != this.repopath) {
463 // args += "--work-tree";
464 // args += this.repopath;
466 for (var i = 0; i < args_in.length;i++) {
470 //this.lastCmd = args.join(" ");
472 GLib.debug( "CWD=%s", this.git_working_dir );
473 //print( "cmd: %s\n", string.joinv (" ", args));
477 string home = "HOME=" + Environment.get_home_dir() ;
479 // do not need to set gitpath..
480 //if (File.exists(this.repo + '/.git/config')) {
481 //env.push("GITPATH=" + this.repo );
485 var cfg = new SpawnConfig(this.git_working_dir , args , env);
489 // may throw error...
490 var sp = new Spawn(cfg);
493 sp.run(this.git_async_on_complete);
497 void git_async_on_complete(int err, string output)
499 GLib.debug("GOT %d : %s", err, output);
500 this.git_async_on_callback(this, err, output);