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) {
36 public static Array<GitRepo> list_cache = null;
38 public static Array<GitRepo> list()
41 if (GitRepo.list_cache !== null) {
42 return GitRepo.list_cache;
45 GitRepo.list_cache = new Array<GitRepo>();
47 var dir = Enviroment.get_home_dir() + '/gitlive';
50 var ar = File.list(dir );
51 print(JSON.stringify(ar));
52 ar.forEach(function(f) {
53 if (File.exists(dir + '/' + f +'/.git')) {
54 Repo._list.push(new imports.Scm.Git.Repo.Repo( {
55 repopath : dir +'/' + f,
70 * @param {Object} cfg - Configuration
71 * (basically repopath is currently only critical one.)
75 public GitRepo(string path) {
77 this.git_working_dir = path;
78 this.gitdir = path + "/.git";
79 if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
80 this.gitdir = path; // naked...
82 this.cmds = new Array<GitMonitorQueue> ();
83 //Repo.superclass.constructor.call(this,cfg);
90 * @argument {Array} files the files to add.
92 public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
94 // should really find out if these are untracked files each..
95 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
96 // not sure if that is how git works.. but just be certian.
98 for (var i = 0; i < files.length;i++) {
99 var f = files.index(i).vname;
101 string[] cmd = { "add", f };
104 ret += e.message + "\n";
113 * remove files to track.
115 * @argument {Array} files the files to add.
117 public string remove ( Array<GitMonitorQueue> files ) throws Error, SpawnError
119 // this may fail if files do not exist..
120 // should really find out if these are untracked files each..
121 // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
122 // not sure if that is how git works.. but just be certian.
125 for (var i = 0; i < files.length;i++) {
126 var f = files.index(i).vname;
128 string[] cmd = { "rm", "-f" , f };
131 ret += e.message + "\n";
143 * @argument {Object} cfg commit configuration
145 * @property {String} name (optional)
146 * @property {String} email (optional)
147 * @property {String} changed (date) (optional)
148 * @property {String} reason (optional)
149 * @property {Array} files - the files that have changed.
153 public string commit ( string message, Array<GitMonitorQueue> files ) throws Error, SpawnError
160 if (typeof(cfg.name) != 'undefined') {
162 'author' : cfg.name + ' <' + cfg.email + '>'
165 "GIT_COMMITTER_NAME" + cfg.name,
166 "GIT_COMMITTER_EMAIL" + cfg.email
170 if (typeof(cfg.changed) != 'undefined') {
171 env.push("GIT_AUTHOR_DATE= " + cfg.changed )
175 string[] args = { "commit", "-m" };
176 args += (message.length > 0 ? message : "Changed" );
177 for (var i = 0; i< files.length ; i++ ) {
178 args += files.index(i).vname; // full path?
181 return this.git(args);
186 * Fetch and merge remote repo changes into current branch..
188 * At present we just need this to update the current working branch..
189 * -- maybe later it will have a few options and do more stuff..
192 public string pull () throws Error, SpawnError
194 // should probably hand error conditions better...
195 string[] cmd = { "pull" };
196 return this.git( cmd );
203 * Send local changes to remote repo(s)
205 * At present we just need this to push the current branch.
206 * -- maybe later it will have a few options and do more stuff..
209 public string push () throws Error, SpawnError
212 return this.git({ "push" });
217 * The meaty part.. run spawn.. with git..
222 public string git(string[] args_in ) throws Error, SpawnError
224 // convert arguments.
227 string[] args = { "git" };
228 //args += "--git-dir";
229 //args += this.gitdir;
230 args += "--no-pager";
233 //if (this.gitdir != this.repopath) {
234 // args += "--work-tree";
235 // args += this.repopath;
237 for (var i = 0; i < args_in.length;i++) {
241 //this.lastCmd = args.join(" ");
243 stdout.printf( "CWD=%s\n", this.git_working_dir );
244 print( string.joinv (" ", args));
248 string home = "HOME=" + Environment.get_home_dir() ;
250 // do not need to set gitpath..
251 //if (File.exists(this.repo + '/.git/config')) {
252 //env.push("GITPATH=" + this.repo );
257 var cfg = new SpawnConfig(this.git_working_dir , args , env);
260 // may throw error...
261 var sp = new Spawn(cfg);
263 stdout.printf( "GOT: %s\n" , sp.output);
264 // parse output for some commands ?