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 print("Error: %s\n",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 * add files to track.
143 * @argument {Array} files the files to add.
145 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
147 // should really find out if these are untracked files each..
148 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
149 // not sure if that is how git works.. but just be certian.
151 for (var i = 0; i < files.length;i++) {
152 var f = files.index(i).vname;
154 string[] cmd = { "add", f };
157 ret += e.message + "\n";
164 public bool is_ignore(string fname) throws Error, SpawnError
166 if (fname == ".gitignore") {
167 this.ignore_files.clear();
170 if (this.ignore_files.has_key(fname)) {
171 return this.ignore_files.get(fname);
175 var ret = this.git( { "check-ignore" , fname } );
176 this.ignore_files.set(fname, ret.length > 0);
177 return ret.length > 0;
178 } catch (SpawnError e) {
179 this.ignore_files.set(fname, false);
188 * remove files to track.
190 * @argument {Array} files the files to add.
192 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
194 // this may fail if files do not exist..
195 // should really find out if these are untracked files each..
196 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
197 // not sure if that is how git works.. but just be certian.
200 for (var i = 0; i < files.length;i++) {
201 var f = files.index(i).vname;
203 string[] cmd = { "rm", "-f" , f };
206 ret += e.message + "\n";
219 * @argument {Object} cfg commit configuration
221 * @property {String} name (optional)
222 * @property {String} email (optional)
223 * @property {String} changed (date) (optional)
224 * @property {String} reason (optional)
225 * @property {Array} files - the files that have changed.
229 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
236 if (typeof(cfg.name) != 'undefined') {
238 'author' : cfg.name + ' <' + cfg.email + '>'
241 "GIT_COMMITTER_NAME" + cfg.name,
242 "GIT_COMMITTER_EMAIL" + cfg.email
246 if (typeof(cfg.changed) != 'undefined') {
247 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
251 string[] args = { "commit", "-m" };
252 args += (message.length > 0 ? message : "Changed" );
253 for (var i = 0; i< files.length ; i++ ) {
254 args += files.index(i).vname; // full path?
257 return this.git(args);
262 * Fetch and merge remote repo changes into current branch..
264 * At present we just need this to update the current working branch..
265 * -- maybe later it will have a few options and do more stuff..
268 public string pull () throws Error, SpawnError
270 // should probably hand error conditions better...
271 string[] cmd = { "pull" , "--no-edit" };
272 return this.git( cmd );
278 public void pull_async(Callback cb) {
285 * Send local changes to remote repo(s)
287 * At present we just need this to push the current branch.
288 * -- maybe later it will have a few options and do more stuff..
291 public string push () throws Error, SpawnError
294 return this.git({ "push", "origin", "HEAD" });
302 * The meaty part.. run spawn.. with git..
307 public string git(string[] args_in ) throws Error, SpawnError
309 // convert arguments.
311 string[] args = { "git" };
312 //args += "--git-dir";
313 //args += this.gitdir;
314 args += "--no-pager";
317 //if (this.gitdir != this.repopath) {
318 // args += "--work-tree";
319 // args += this.repopath;
321 for (var i = 0; i < args_in.length;i++) {
325 //this.lastCmd = args.join(" ");
327 stdout.printf( "CWD=%s\n", this.git_working_dir );
328 print( "cmd: %s\n", string.joinv (" ", args));
332 string home = "HOME=" + Environment.get_home_dir() ;
334 // do not need to set gitpath..
335 //if (File.exists(this.repo + '/.git/config')) {
336 //env.push("GITPATH=" + this.repo );
340 var cfg = new SpawnConfig(this.git_working_dir , args , env);
343 // may throw error...
344 var sp = new Spawn(cfg);
347 stdout.printf( "GOT: %s\n" , sp.output);
348 // parse output for some commands ?