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);
169 * add files to track.
171 * @argument {Array} files the files to add.
173 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
175 // should really find out if these are untracked files each..
176 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
177 // not sure if that is how git works.. but just be certian.
179 for (var i = 0; i < files.length;i++) {
180 var f = files.index(i).vname;
182 string[] cmd = { "add", f };
185 ret += e.message + "\n";
192 public bool is_ignore(string fname) throws Error, SpawnError
194 if (fname == ".gitignore") {
195 this.ignore_files.clear();
198 if (this.ignore_files.has_key(fname)) {
199 return this.ignore_files.get(fname);
203 var ret = this.git( { "check-ignore" , fname } );
204 this.ignore_files.set(fname, ret.length > 0);
205 return ret.length > 0;
206 } catch (SpawnError e) {
207 this.ignore_files.set(fname, false);
216 * remove files to track.
218 * @argument {Array} files the files to add.
220 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
222 // this may fail if files do not exist..
223 // should really find out if these are untracked files each..
224 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
225 // not sure if that is how git works.. but just be certian.
228 for (var i = 0; i < files.length;i++) {
229 var f = files.index(i).vname;
231 string[] cmd = { "rm", "-f" , f };
234 ret += e.message + "\n";
247 * @argument {Object} cfg commit configuration
249 * @property {String} name (optional)
250 * @property {String} email (optional)
251 * @property {String} changed (date) (optional)
252 * @property {String} reason (optional)
253 * @property {Array} files - the files that have changed.
257 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
264 if (typeof(cfg.name) != 'undefined') {
266 'author' : cfg.name + ' <' + cfg.email + '>'
269 "GIT_COMMITTER_NAME" + cfg.name,
270 "GIT_COMMITTER_EMAIL" + cfg.email
274 if (typeof(cfg.changed) != 'undefined') {
275 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
279 string[] args = { "commit", "-m" };
280 args += (message.length > 0 ? message : "Changed" );
281 for (var i = 0; i< files.length ; i++ ) {
282 args += files.index(i).vname; // full path?
285 return this.git(args);
290 * Fetch and merge remote repo changes into current branch..
292 * At present we just need this to update the current working branch..
293 * -- maybe later it will have a few options and do more stuff..
296 public string pull () throws Error, SpawnError
298 // should probably hand error conditions better...
299 string[] cmd = { "pull" , "--no-edit" };
300 return this.git( cmd );
305 public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
306 public void pull_async(GitAsyncCallback cb)
309 string[] cmd = { "pull" , "--no-edit" };
310 this.git_async( cmd , cb);
317 * Send local changes to remote repo(s)
319 * At present we just need this to push the current branch.
320 * -- maybe later it will have a few options and do more stuff..
323 public string push () throws Error, SpawnError
326 return this.git({ "push", "origin", "HEAD" });
334 * The meaty part.. run spawn.. with git..
339 public string git(string[] args_in ) throws Error, SpawnError
341 // convert arguments.
343 string[] args = { "git" };
344 //args += "--git-dir";
345 //args += this.gitdir;
346 args += "--no-pager";
349 //if (this.gitdir != this.repopath) {
350 // args += "--work-tree";
351 // args += this.repopath;
353 for (var i = 0; i < args_in.length;i++) {
357 //this.lastCmd = args.join(" ");
359 GLib.debug( "CWD=%s", this.git_working_dir );
360 GLib.debug( "cmd: %s", string.joinv (" ", args));
364 string home = "HOME=" + Environment.get_home_dir() ;
366 // do not need to set gitpath..
367 //if (File.exists(this.repo + '/.git/config')) {
368 //env.push("GITPATH=" + this.repo );
372 var cfg = new SpawnConfig(this.git_working_dir , args , env);
375 // may throw error...
376 var sp = new Spawn(cfg);
379 GLib.debug( "GOT: %s" , sp.output);
380 // parse output for some commands ?
384 unowned GitAsyncCallback git_async_on_callback;
385 public void git_async( string[] args_in, GitAsyncCallback cb ) throws Error, SpawnError
387 // convert arguments.
388 this.git_async_on_callback = cb;
389 string[] args = { "git" };
390 //args += "--git-dir";
391 //args += this.gitdir;
392 args += "--no-pager";
395 //if (this.gitdir != this.repopath) {
396 // args += "--work-tree";
397 // args += this.repopath;
399 for (var i = 0; i < args_in.length;i++) {
403 //this.lastCmd = args.join(" ");
405 GLib.debug( "CWD=%s", this.git_working_dir );
406 //print( "cmd: %s\n", string.joinv (" ", args));
410 string home = "HOME=" + Environment.get_home_dir() ;
412 // do not need to set gitpath..
413 //if (File.exists(this.repo + '/.git/config')) {
414 //env.push("GITPATH=" + this.repo );
418 var cfg = new SpawnConfig(this.git_working_dir , args , env);
422 // may throw error...
423 var sp = new Spawn(cfg);
426 sp.run(this.git_async_on_complete);
430 void git_async_on_complete(int err, string output)
432 GLib.debug("GOT %d : %s", err, output);
433 this.git_async_on_callback(this, err, output);