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;
23 * index of.. matching gitpath..
25 public static int indexOf( Array<GitRepo> repos, string gitpath) {
26 // make a fake object to compare against..
27 var test_repo = new GitRepo(gitpath);
29 for(var i =0; i < repos.length; i++) {
30 if (repos.index(i).gitdir == test_repo.gitdir) {
39 public static Array<GitRepo> list()
42 //if (GitRepo.list_cache != null) {
43 // unowned Array<GitRepo> ret = GitRepo.list_cache;
47 var list_cache = new Array<GitRepo>();
49 var dir = Environment.get_home_dir() + "/gitlive";
51 var f = File.new_for_path(dir);
52 FileEnumerator file_enum;
54 file_enum = f.enumerate_children(
55 FileAttribute.STANDARD_DISPLAY_NAME + ","+
56 FileAttribute.STANDARD_TYPE,
57 FileQueryInfoFlags.NONE,
70 next_file = file_enum.next_file(null);
71 if (next_file == null) {
76 GLib.debug("Error: %s",e.message);
80 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
82 if (next_file.get_file_type() != FileType.DIRECTORY) {
87 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
92 if (next_file.get_display_name()[0] == '.') {
96 var sp = dir+"/"+next_file.get_display_name();
98 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
100 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
104 list_cache.append_val(new GitRepo( sp )) ;
120 * @param {Object} cfg - Configuration
121 * (basically repopath is currently only critical one.)
125 public GitRepo(string path) {
127 this.name = File.new_for_path(path).get_basename();
128 this.ignore_files = new Gee.HashMap<string,bool>();
130 this.git_working_dir = path;
131 this.gitdir = path + "/.git";
132 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
133 this.gitdir = path; // naked...
135 this.cmds = new Array<GitMonitorQueue> ();
136 //Repo.superclass.constructor.call(this,cfg);
141 public bool is_autocommit ()
143 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
145 public bool is_autopush ()
147 return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
150 Gee.HashMap<string,GitBranch> branches;
152 public void loadBranches()
154 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
155 var res = this.git( cmd );
156 var lines = res.split("\n");
157 for (var i = 0; i < lines.length ; i++) {
158 var br = new GitBranch(this);
159 if (!br.parseBranchListItem(lines[i])) {
162 branches.set(br.realName(), br);
164 this.currentBranch = br;
172 * add files to track.
174 * @argument {Array} files the files to add.
176 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
178 // should really find out if these are untracked files each..
179 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
180 // not sure if that is how git works.. but just be certian.
182 for (var i = 0; i < files.length;i++) {
183 var f = files.index(i).vname;
185 string[] cmd = { "add", f };
188 ret += e.message + "\n";
195 public bool is_ignore(string fname) throws Error, SpawnError
197 if (fname == ".gitignore") {
198 this.ignore_files.clear();
201 if (this.ignore_files.has_key(fname)) {
202 return this.ignore_files.get(fname);
206 var ret = this.git( { "check-ignore" , fname } );
207 this.ignore_files.set(fname, ret.length > 0);
208 return ret.length > 0;
209 } catch (SpawnError e) {
210 this.ignore_files.set(fname, false);
219 * remove files to track.
221 * @argument {Array} files the files to add.
223 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
225 // this may fail if files do not exist..
226 // should really find out if these are untracked files each..
227 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
228 // not sure if that is how git works.. but just be certian.
231 for (var i = 0; i < files.length;i++) {
232 var f = files.index(i).vname;
234 string[] cmd = { "rm", "-f" , f };
237 ret += e.message + "\n";
250 * @argument {Object} cfg commit configuration
252 * @property {String} name (optional)
253 * @property {String} email (optional)
254 * @property {String} changed (date) (optional)
255 * @property {String} reason (optional)
256 * @property {Array} files - the files that have changed.
260 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
267 if (typeof(cfg.name) != 'undefined') {
269 'author' : cfg.name + ' <' + cfg.email + '>'
272 "GIT_COMMITTER_NAME" + cfg.name,
273 "GIT_COMMITTER_EMAIL" + cfg.email
277 if (typeof(cfg.changed) != 'undefined') {
278 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
282 string[] args = { "commit", "-m" };
283 args += (message.length > 0 ? message : "Changed" );
284 for (var i = 0; i< files.length ; i++ ) {
285 args += files.index(i).vname; // full path?
288 return this.git(args);
293 * Fetch and merge remote repo changes into current branch..
295 * At present we just need this to update the current working branch..
296 * -- maybe later it will have a few options and do more stuff..
299 public string pull () throws Error, SpawnError
301 // should probably hand error conditions better...
302 string[] cmd = { "pull" , "--no-edit" };
303 return this.git( cmd );
308 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
309 public void pull_async(GitAsyncCallback cb)
312 string[] cmd = { "pull" , "--no-edit" };
313 this.git_async( cmd , cb);
320 * Send local changes to remote repo(s)
322 * At present we just need this to push the current branch.
323 * -- maybe later it will have a few options and do more stuff..
326 public string push () throws Error, SpawnError
329 return this.git({ "push", "origin", "HEAD" });
337 * The meaty part.. run spawn.. with git..
342 public string git(string[] args_in ) throws Error, SpawnError
344 // convert arguments.
346 string[] args = { "git" };
347 //args += "--git-dir";
348 //args += this.gitdir;
349 args += "--no-pager";
352 //if (this.gitdir != this.repopath) {
353 // args += "--work-tree";
354 // args += this.repopath;
356 for (var i = 0; i < args_in.length;i++) {
360 //this.lastCmd = args.join(" ");
362 GLib.debug( "CWD=%s", this.git_working_dir );
363 GLib.debug( "cmd: %s", string.joinv (" ", args));
367 string home = "HOME=" + Environment.get_home_dir() ;
369 // do not need to set gitpath..
370 //if (File.exists(this.repo + '/.git/config')) {
371 //env.push("GITPATH=" + this.repo );
375 var cfg = new SpawnConfig(this.git_working_dir , args , env);
378 // may throw error...
379 var sp = new Spawn(cfg);
382 GLib.debug( "GOT: %s" , sp.output);
383 // parse output for some commands ?
387 unowned GitAsyncCallback git_async_on_callback;
388 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
390 // convert arguments.
391 this.git_async_on_callback = cb;
392 string[] args = { "git" };
393 //args += "--git-dir";
394 //args += this.gitdir;
395 args += "--no-pager";
398 //if (this.gitdir != this.repopath) {
399 // args += "--work-tree";
400 // args += this.repopath;
402 for (var i = 0; i < args_in.length;i++) {
406 //this.lastCmd = args.join(" ");
408 GLib.debug( "CWD=%s", this.git_working_dir );
409 //print( "cmd: %s\n", string.joinv (" ", args));
413 string home = "HOME=" + Environment.get_home_dir() ;
415 // do not need to set gitpath..
416 //if (File.exists(this.repo + '/.git/config')) {
417 //env.push("GITPATH=" + this.repo );
421 var cfg = new SpawnConfig(this.git_working_dir , args , env);
425 // may throw error...
426 var sp = new Spawn(cfg);
429 sp.run(this.git_async_on_complete);
433 void git_async_on_complete(int err, string output)
435 GLib.debug("GOT %d : %s", err, output);
436 this.git_async_on_callback(this, err, output);