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 = { "add", f };
155 var res = this.git( cmd );
161 * add files to track.
163 * @argument {Array} files the files to add.
165 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
167 // should really find out if these are untracked files each..
168 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
169 // not sure if that is how git works.. but just be certian.
171 for (var i = 0; i < files.length;i++) {
172 var f = files.index(i).vname;
174 string[] cmd = { "add", f };
177 ret += e.message + "\n";
184 public bool is_ignore(string fname) throws Error, SpawnError
186 if (fname == ".gitignore") {
187 this.ignore_files.clear();
190 if (this.ignore_files.has_key(fname)) {
191 return this.ignore_files.get(fname);
195 var ret = this.git( { "check-ignore" , fname } );
196 this.ignore_files.set(fname, ret.length > 0);
197 return ret.length > 0;
198 } catch (SpawnError e) {
199 this.ignore_files.set(fname, false);
208 * remove files to track.
210 * @argument {Array} files the files to add.
212 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
214 // this may fail if files do not exist..
215 // should really find out if these are untracked files each..
216 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
217 // not sure if that is how git works.. but just be certian.
220 for (var i = 0; i < files.length;i++) {
221 var f = files.index(i).vname;
223 string[] cmd = { "rm", "-f" , f };
226 ret += e.message + "\n";
239 * @argument {Object} cfg commit configuration
241 * @property {String} name (optional)
242 * @property {String} email (optional)
243 * @property {String} changed (date) (optional)
244 * @property {String} reason (optional)
245 * @property {Array} files - the files that have changed.
249 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
256 if (typeof(cfg.name) != 'undefined') {
258 'author' : cfg.name + ' <' + cfg.email + '>'
261 "GIT_COMMITTER_NAME" + cfg.name,
262 "GIT_COMMITTER_EMAIL" + cfg.email
266 if (typeof(cfg.changed) != 'undefined') {
267 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
271 string[] args = { "commit", "-m" };
272 args += (message.length > 0 ? message : "Changed" );
273 for (var i = 0; i< files.length ; i++ ) {
274 args += files.index(i).vname; // full path?
277 return this.git(args);
282 * Fetch and merge remote repo changes into current branch..
284 * At present we just need this to update the current working branch..
285 * -- maybe later it will have a few options and do more stuff..
288 public string pull () throws Error, SpawnError
290 // should probably hand error conditions better...
291 string[] cmd = { "pull" , "--no-edit" };
292 return this.git( cmd );
297 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
298 public void pull_async(GitAsyncCallback cb)
301 string[] cmd = { "pull" , "--no-edit" };
302 this.git_async( cmd , cb);
309 * Send local changes to remote repo(s)
311 * At present we just need this to push the current branch.
312 * -- maybe later it will have a few options and do more stuff..
315 public string push () throws Error, SpawnError
318 return this.git({ "push", "origin", "HEAD" });
326 * The meaty part.. run spawn.. with git..
331 public string git(string[] args_in ) throws Error, SpawnError
333 // convert arguments.
335 string[] args = { "git" };
336 //args += "--git-dir";
337 //args += this.gitdir;
338 args += "--no-pager";
341 //if (this.gitdir != this.repopath) {
342 // args += "--work-tree";
343 // args += this.repopath;
345 for (var i = 0; i < args_in.length;i++) {
349 //this.lastCmd = args.join(" ");
351 GLib.debug( "CWD=%s", this.git_working_dir );
352 GLib.debug( "cmd: %s", string.joinv (" ", args));
356 string home = "HOME=" + Environment.get_home_dir() ;
358 // do not need to set gitpath..
359 //if (File.exists(this.repo + '/.git/config')) {
360 //env.push("GITPATH=" + this.repo );
364 var cfg = new SpawnConfig(this.git_working_dir , args , env);
367 // may throw error...
368 var sp = new Spawn(cfg);
371 GLib.debug( "GOT: %s" , sp.output);
372 // parse output for some commands ?
376 unowned GitAsyncCallback git_async_on_callback;
377 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
379 // convert arguments.
380 this.git_async_on_callback = cb;
381 string[] args = { "git" };
382 //args += "--git-dir";
383 //args += this.gitdir;
384 args += "--no-pager";
387 //if (this.gitdir != this.repopath) {
388 // args += "--work-tree";
389 // args += this.repopath;
391 for (var i = 0; i < args_in.length;i++) {
395 //this.lastCmd = args.join(" ");
397 GLib.debug( "CWD=%s", this.git_working_dir );
398 //print( "cmd: %s\n", string.joinv (" ", args));
402 string home = "HOME=" + Environment.get_home_dir() ;
404 // do not need to set gitpath..
405 //if (File.exists(this.repo + '/.git/config')) {
406 //env.push("GITPATH=" + this.repo );
410 var cfg = new SpawnConfig(this.git_working_dir , args , env);
414 // may throw error...
415 var sp = new Spawn(cfg);
418 sp.run(this.git_async_on_complete);
422 void git_async_on_complete(int err, string output)
424 GLib.debug("GOT %d : %s", err, output);
425 this.git_async_on_callback(this, err, output);