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";
165 * remove files to track.
167 * @argument {Array} files the files to add.
169 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
171 // this may fail if files do not exist..
172 // should really find out if these are untracked files each..
173 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
174 // not sure if that is how git works.. but just be certian.
177 for (var i = 0; i < files.length;i++) {
178 var f = files.index(i).vname;
180 string[] cmd = { "rm", "-f" , f };
183 ret += e.message + "\n";
195 * @argument {Object} cfg commit configuration
197 * @property {String} name (optional)
198 * @property {String} email (optional)
199 * @property {String} changed (date) (optional)
200 * @property {String} reason (optional)
201 * @property {Array} files - the files that have changed.
205 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
212 if (typeof(cfg.name) != 'undefined') {
214 'author' : cfg.name + ' <' + cfg.email + '>'
217 "GIT_COMMITTER_NAME" + cfg.name,
218 "GIT_COMMITTER_EMAIL" + cfg.email
222 if (typeof(cfg.changed) != 'undefined') {
223 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
227 string[] args = { "commit", "-m" };
228 args += (message.length > 0 ? message : "Changed" );
229 for (var i = 0; i< files.length ; i++ ) {
230 args += files.index(i).vname; // full path?
233 return this.git(args);
238 * Fetch and merge remote repo changes into current branch..
240 * At present we just need this to update the current working branch..
241 * -- maybe later it will have a few options and do more stuff..
244 public string pull () throws Error, SpawnError
246 // should probably hand error conditions better...
247 string[] cmd = { "pull" , "--no-edit" };
248 return this.git( cmd );
255 * Send local changes to remote repo(s)
257 * At present we just need this to push the current branch.
258 * -- maybe later it will have a few options and do more stuff..
261 public string push () throws Error, SpawnError
264 return this.git({ "push", "origin", "HEAD" });
269 * The meaty part.. run spawn.. with git..
274 public string git(string[] args_in ) throws Error, SpawnError
276 // convert arguments.
278 string[] args = { "git" };
279 //args += "--git-dir";
280 //args += this.gitdir;
281 args += "--no-pager";
284 //if (this.gitdir != this.repopath) {
285 // args += "--work-tree";
286 // args += this.repopath;
288 for (var i = 0; i < args_in.length;i++) {
292 //this.lastCmd = args.join(" ");
294 stdout.printf( "CWD=%s\n", this.git_working_dir );
295 print( "cmd: %s\n", string.joinv (" ", args));
299 string home = "HOME=" + Environment.get_home_dir() ;
301 // do not need to set gitpath..
302 //if (File.exists(this.repo + '/.git/config')) {
303 //env.push("GITPATH=" + this.repo );
307 var cfg = new SpawnConfig(this.git_working_dir , args , env);
310 // may throw error...
311 var sp = new Spawn(cfg);
314 stdout.printf( "GOT: %s\n" , sp.output);
315 // parse output for some commands ?