10 public class GitRepo : Object
13 public Array<GitMonitorQueue> cmds;
17 public string git_working_dir;
18 public bool debug = false;
21 * index of.. matching gitpath..
23 public static int indexOf( Array<GitRepo> repos, string gitpath) {
24 // make a fake object to compare against..
25 var test_repo = new GitRepo(gitpath);
27 for(var i =0; i < repos.length; i++) {
28 if (repos.index(i).gitdir == test_repo.gitdir) {
38 public static Array<GitRepo> list()
41 //if (GitRepo.list_cache != null) {
42 // unowned Array<GitRepo> ret = GitRepo.list_cache;
46 var list_cache = new Array<GitRepo>();
48 var dir = Environment.get_home_dir() + "/gitlive";
50 var f = File.new_for_path(dir);
51 FileEnumerator file_enum;
53 file_enum = f.enumerate_children(
54 FileAttribute.STANDARD_DISPLAY_NAME + ","+
55 FileAttribute.STANDARD_TYPE,
56 FileQueryInfoFlags.NONE,
69 next_file = file_enum.next_file(null);
70 if (next_file == null) {
75 print("Error: %s\n",e.message);
79 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
81 if (next_file.get_file_type() != FileType.DIRECTORY) {
86 if (next_file.get_file_type() == FileType.SYMBOLIC_LINK) {
91 if (next_file.get_display_name()[0] == '.') {
95 var sp = dir+"/"+next_file.get_display_name();
97 var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
99 if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
103 list_cache.append_val(new GitRepo( sp )) ;
119 * @param {Object} cfg - Configuration
120 * (basically repopath is currently only critical one.)
124 public GitRepo(string path) {
126 this.name = File.new_for_path(path).get_basename();
129 this.git_working_dir = path;
130 this.gitdir = path + "/.git";
131 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
132 this.gitdir = path; // naked...
134 this.cmds = new Array<GitMonitorQueue> ();
135 //Repo.superclass.constructor.call(this,cfg);
140 * add files to track.
142 * @argument {Array} files the files to add.
144 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
146 // should really find out if these are untracked files each..
147 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
148 // not sure if that is how git works.. but just be certian.
150 for (var i = 0; i < files.length;i++) {
151 var f = files.index(i).vname;
153 string[] cmd = { "add", f };
156 ret += e.message + "\n";
163 public bool is_ignore(string fname) throws Error, SpawnError
166 var ret = this.git( { "check-ignore" , fname } );
168 } catch (SpawnError e) {
177 * remove files to track.
179 * @argument {Array} files the files to add.
181 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
183 // this may fail if files do not exist..
184 // should really find out if these are untracked files each..
185 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
186 // not sure if that is how git works.. but just be certian.
189 for (var i = 0; i < files.length;i++) {
190 var f = files.index(i).vname;
192 string[] cmd = { "rm", "-f" , f };
195 ret += e.message + "\n";
207 * @argument {Object} cfg commit configuration
209 * @property {String} name (optional)
210 * @property {String} email (optional)
211 * @property {String} changed (date) (optional)
212 * @property {String} reason (optional)
213 * @property {Array} files - the files that have changed.
217 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
224 if (typeof(cfg.name) != 'undefined') {
226 'author' : cfg.name + ' <' + cfg.email + '>'
229 "GIT_COMMITTER_NAME" + cfg.name,
230 "GIT_COMMITTER_EMAIL" + cfg.email
234 if (typeof(cfg.changed) != 'undefined') {
235 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
239 string[] args = { "commit", "-m" };
240 args += (message.length > 0 ? message : "Changed" );
241 for (var i = 0; i< files.length ; i++ ) {
242 args += files.index(i).vname; // full path?
245 return this.git(args);
250 * Fetch and merge remote repo changes into current branch..
252 * At present we just need this to update the current working branch..
253 * -- maybe later it will have a few options and do more stuff..
256 public string pull () throws Error, SpawnError
258 // should probably hand error conditions better...
259 string[] cmd = { "pull" , "--no-edit" };
260 return this.git( cmd );
267 * Send local changes to remote repo(s)
269 * At present we just need this to push the current branch.
270 * -- maybe later it will have a few options and do more stuff..
273 public string push () throws Error, SpawnError
276 return this.git({ "push", "origin", "HEAD" });
284 * The meaty part.. run spawn.. with git..
289 public string git(string[] args_in ) throws Error, SpawnError
291 // convert arguments.
293 string[] args = { "git" };
294 //args += "--git-dir";
295 //args += this.gitdir;
296 args += "--no-pager";
299 //if (this.gitdir != this.repopath) {
300 // args += "--work-tree";
301 // args += this.repopath;
303 for (var i = 0; i < args_in.length;i++) {
307 //this.lastCmd = args.join(" ");
309 stdout.printf( "CWD=%s\n", this.git_working_dir );
310 print( "cmd: %s\n", string.joinv (" ", args));
314 string home = "HOME=" + Environment.get_home_dir() ;
316 // do not need to set gitpath..
317 //if (File.exists(this.repo + '/.git/config')) {
318 //env.push("GITPATH=" + this.repo );
322 var cfg = new SpawnConfig(this.git_working_dir , args , env);
325 // may throw error...
326 var sp = new Spawn(cfg);
329 stdout.printf( "GOT: %s\n" , sp.output);
330 // parse output for some commands ?