10 public class GitRepo : Object
13 public Array<GitMonitorQueue> cmds;
17 public string git_working_dir;
18 public bool debug = false;
20 public Gee.HashMap<string,bool> ignore_files;
21 public GitBranch currentBranch;
24 * index of.. matching gitpath..
26 public static int indexOf( Array<GitRepo> repos, string gitpath) {
27 // make a fake object to compare against..
28 var test_repo = new GitRepo(gitpath);
30 for(var i =0; i < repos.length; i++) {
31 if (repos.index(i).gitdir == test_repo.gitdir) {
40 public static Array<GitRepo> list()
43 //if (GitRepo.list_cache != null) {
44 // unowned Array<GitRepo> ret = GitRepo.list_cache;
48 var list_cache = new Array<GitRepo>();
50 var dir = Environment.get_home_dir() + "/gitlive";
52 var f = File.new_for_path(dir);
53 FileEnumerator file_enum;
55 file_enum = f.enumerate_children(
56 FileAttribute.STANDARD_DISPLAY_NAME + ","+
57 FileAttribute.STANDARD_TYPE,
58 FileQueryInfoFlags.NONE,
71 next_file = file_enum.next_file(null);
72 if (next_file == null) {
77 GLib.debug("Error: %s",e.message);
81 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
83 if (next_file.get_file_type() != FileType.DIRECTORY) {
88 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
93 if (next_file.get_display_name()[0] == '.') {
97 var sp = dir+"/"+next_file.get_display_name();
99 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
101 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
105 list_cache.append_val(new GitRepo( sp )) ;
121 * @param {Object} cfg - Configuration
122 * (basically repopath is currently only critical one.)
126 public GitRepo(string path) {
128 this.name = File.new_for_path(path).get_basename();
129 this.ignore_files = new Gee.HashMap<string,bool>();
131 this.git_working_dir = path;
132 this.gitdir = path + "/.git";
133 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
134 this.gitdir = path; // naked...
136 this.cmds = new Array<GitMonitorQueue> ();
137 //Repo.superclass.constructor.call(this,cfg);
142 public bool is_autocommit ()
144 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
146 public bool is_autopush ()
148 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
151 Gee.HashMap<string,GitBranch> branches;
153 public void loadBranches()
155 this.branches = new Gee.HashMap<string,GitBranch>();
157 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
158 var res = this.git( cmd );
159 var lines = res.split("\n");
160 for (var i = 0; i < lines.length ; i++) {
161 var br = new GitBranch(this);
162 if (!br.parseBranchListItem(lines[i])) {
165 GLib.debug("add branch %s", br.realName());
167 branches.set(br.realName(), br);
169 this.currentBranch = br;
174 public string branchesToString()
177 foreach( var br in this.branches.values) {
181 ret += ret.length > 0 ? "," : "";
189 public void setActiveTicket(RooTicket ticket, string branchname)
195 public void createBranchNamed(string branchname)
197 string[] cmd = { "checkout", "-b" , branchname };
199 this.loadBranches(); // update branch list...
205 * add files to track.
207 * @argument {Array} files the files to add.
209 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
211 // should really find out if these are untracked files each..
212 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
213 // not sure if that is how git works.. but just be certian.
215 for (var i = 0; i < files.length;i++) {
216 var f = files.index(i).vname;
218 string[] cmd = { "add", f };
221 ret += e.message + "\n";
228 public bool is_ignore(string fname) throws Error, SpawnError
230 if (fname == ".gitignore") {
231 this.ignore_files.clear();
234 if (this.ignore_files.has_key(fname)) {
235 return this.ignore_files.get(fname);
239 var ret = this.git( { "check-ignore" , fname } );
240 this.ignore_files.set(fname, ret.length > 0);
241 return ret.length > 0;
242 } catch (SpawnError e) {
243 this.ignore_files.set(fname, false);
252 * remove files to track.
254 * @argument {Array} files the files to add.
256 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
258 // this may fail if files do not exist..
259 // should really find out if these are untracked files each..
260 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
261 // not sure if that is how git works.. but just be certian.
264 for (var i = 0; i < files.length;i++) {
265 var f = files.index(i).vname;
267 string[] cmd = { "rm", "-f" , f };
270 ret += e.message + "\n";
283 * @argument {Object} cfg commit configuration
285 * @property {String} name (optional)
286 * @property {String} email (optional)
287 * @property {String} changed (date) (optional)
288 * @property {String} reason (optional)
289 * @property {Array} files - the files that have changed.
293 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
300 if (typeof(cfg.name) != 'undefined') {
302 'author' : cfg.name + ' <' + cfg.email + '>'
305 "GIT_COMMITTER_NAME" + cfg.name,
306 "GIT_COMMITTER_EMAIL" + cfg.email
310 if (typeof(cfg.changed) != 'undefined') {
311 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
315 string[] args = { "commit", "-m" };
316 args += (message.length > 0 ? message : "Changed" );
317 for (var i = 0; i< files.length ; i++ ) {
318 args += files.index(i).vname; // full path?
321 return this.git(args);
326 * Fetch and merge remote repo changes into current branch..
328 * At present we just need this to update the current working branch..
329 * -- maybe later it will have a few options and do more stuff..
332 public string pull () throws Error, SpawnError
334 // should probably hand error conditions better...
335 string[] cmd = { "pull" , "--no-edit" };
336 return this.git( cmd );
341 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
342 public void pull_async(GitAsyncCallback cb)
345 string[] cmd = { "pull" , "--no-edit" };
346 this.git_async( cmd , cb);
353 * Send local changes to remote repo(s)
355 * At present we just need this to push the current branch.
356 * -- maybe later it will have a few options and do more stuff..
359 public string push () throws Error, SpawnError
362 return this.git({ "push", "origin", "HEAD" });
370 * The meaty part.. run spawn.. with git..
375 public string git(string[] args_in ) throws Error, SpawnError
377 // convert arguments.
379 string[] args = { "git" };
380 //args += "--git-dir";
381 //args += this.gitdir;
382 args += "--no-pager";
385 //if (this.gitdir != this.repopath) {
386 // args += "--work-tree";
387 // args += this.repopath;
389 for (var i = 0; i < args_in.length;i++) {
393 //this.lastCmd = args.join(" ");
395 GLib.debug( "CWD=%s", this.git_working_dir );
396 GLib.debug( "cmd: %s", string.joinv (" ", args));
400 string home = "HOME=" + Environment.get_home_dir() ;
402 // do not need to set gitpath..
403 //if (File.exists(this.repo + '/.git/config')) {
404 //env.push("GITPATH=" + this.repo );
408 var cfg = new SpawnConfig(this.git_working_dir , args , env);
411 // may throw error...
412 var sp = new Spawn(cfg);
415 GLib.debug( "GOT: %s" , sp.output);
416 // parse output for some commands ?
420 unowned GitAsyncCallback git_async_on_callback;
421 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
423 // convert arguments.
424 this.git_async_on_callback = cb;
425 string[] args = { "git" };
426 //args += "--git-dir";
427 //args += this.gitdir;
428 args += "--no-pager";
431 //if (this.gitdir != this.repopath) {
432 // args += "--work-tree";
433 // args += this.repopath;
435 for (var i = 0; i < args_in.length;i++) {
439 //this.lastCmd = args.join(" ");
441 GLib.debug( "CWD=%s", this.git_working_dir );
442 //print( "cmd: %s\n", string.joinv (" ", args));
446 string home = "HOME=" + Environment.get_home_dir() ;
448 // do not need to set gitpath..
449 //if (File.exists(this.repo + '/.git/config')) {
450 //env.push("GITPATH=" + this.repo );
454 var cfg = new SpawnConfig(this.git_working_dir , args , env);
458 // may throw error...
459 var sp = new Spawn(cfg);
462 sp.run(this.git_async_on_complete);
466 void git_async_on_complete(int err, string output)
468 GLib.debug("GOT %d : %s", err, output);
469 this.git_async_on_callback(this, err, output);