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) {
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 print("Error: %s\n",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 * add files to track.
144 * @argument {Array} files the files to add.
146 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
148 // should really find out if these are untracked files each..
149 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
150 // not sure if that is how git works.. but just be certian.
152 for (var i = 0; i < files.length;i++) {
153 var f = files.index(i).vname;
155 string[] cmd = { "add", f };
158 ret += e.message + "\n";
165 public bool is_ignore(string fname) throws Error, SpawnError
168 var ret = this.git( { "check-ignore" , fname } );
170 } catch (SpawnError e) {
179 * remove files to track.
181 * @argument {Array} files the files to add.
183 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
185 // this may fail if files do not exist..
186 // should really find out if these are untracked files each..
187 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
188 // not sure if that is how git works.. but just be certian.
191 for (var i = 0; i < files.length;i++) {
192 var f = files.index(i).vname;
194 string[] cmd = { "rm", "-f" , f };
197 ret += e.message + "\n";
209 * @argument {Object} cfg commit configuration
211 * @property {String} name (optional)
212 * @property {String} email (optional)
213 * @property {String} changed (date) (optional)
214 * @property {String} reason (optional)
215 * @property {Array} files - the files that have changed.
219 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
226 if (typeof(cfg.name) != 'undefined') {
228 'author' : cfg.name + ' <' + cfg.email + '>'
231 "GIT_COMMITTER_NAME" + cfg.name,
232 "GIT_COMMITTER_EMAIL" + cfg.email
236 if (typeof(cfg.changed) != 'undefined') {
237 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
241 string[] args = { "commit", "-m" };
242 args += (message.length > 0 ? message : "Changed" );
243 for (var i = 0; i< files.length ; i++ ) {
244 args += files.index(i).vname; // full path?
247 return this.git(args);
252 * Fetch and merge remote repo changes into current branch..
254 * At present we just need this to update the current working branch..
255 * -- maybe later it will have a few options and do more stuff..
258 public string pull () throws Error, SpawnError
260 // should probably hand error conditions better...
261 string[] cmd = { "pull" , "--no-edit" };
262 return this.git( cmd );
269 * Send local changes to remote repo(s)
271 * At present we just need this to push the current branch.
272 * -- maybe later it will have a few options and do more stuff..
275 public string push () throws Error, SpawnError
278 return this.git({ "push", "origin", "HEAD" });
286 * The meaty part.. run spawn.. with git..
291 public string git(string[] args_in ) throws Error, SpawnError
293 // convert arguments.
295 string[] args = { "git" };
296 //args += "--git-dir";
297 //args += this.gitdir;
298 args += "--no-pager";
301 //if (this.gitdir != this.repopath) {
302 // args += "--work-tree";
303 // args += this.repopath;
305 for (var i = 0; i < args_in.length;i++) {
309 //this.lastCmd = args.join(" ");
311 stdout.printf( "CWD=%s\n", this.git_working_dir );
312 print( "cmd: %s\n", string.joinv (" ", args));
316 string home = "HOME=" + Environment.get_home_dir() ;
318 // do not need to set gitpath..
319 //if (File.exists(this.repo + '/.git/config')) {
320 //env.push("GITPATH=" + this.repo );
324 var cfg = new SpawnConfig(this.git_working_dir , args , env);
327 // may throw error...
328 var sp = new Spawn(cfg);
331 stdout.printf( "GOT: %s\n" , sp.output);
332 // parse output for some commands ?