1 /* array steam_history = get_steam_versions(to);
2 o->write("STEAM HISTORY\n%O\n",steam_history);
3 if(options->bestoption)
5 string best = show_bestoption(from, to, steam_history, num_versions);
6 write("Best option : "+best+"\n");
11 a = handle_append(from, to, steam_history, num_versions);
12 else if(options->force)
13 a = handle_force(from, to, num_versions);
15 a = handle_normal(from, to, 1, steam_history, num_versions);
19 write("Succesfully imported\n");
23 write("import failed\n");
27 class import-from-git {
33 void handle_to_import(mapping toimport)
35 foreach(toimport; string steampath; mapping gitarr)
39 check_steam_path(dirname(steampath)); //only creates containers
40 int a = create_document(basename(steampath), dirname(steampath));
43 write("SOME ERROR WHILE CREATING DOCUMENT INSIDE "+dirname(steampath)+".\n");
47 object steamobj = OBJ(steampath);
48 steamobj->set_content(git_version_content(gitarr->gitpath, gitarr->commitcount, gitarr->empty));
49 write("setting content for "+steampath+"\n");
51 to_import = ([ ]); //this one is the global mapping set to empty
55 array get_steam_versions(string steampath)
57 object obj = OBJ(steampath);
58 mapping versions = obj->query_attribute("DOC_VERSIONS");
59 if (!sizeof(versions))
61 versions = ([ 1:obj ]);
63 array this_history = ({});
64 foreach(versions; int nr; object version)
66 this_history += ({ ([ "obj":version, "version":nr, "time":version->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
68 sort(this_history->version, this_history);
69 if(sizeof(versions)>1)
70 this_history += ({ ([ "obj":obj, "version":this_history[-1]->version+1, "time":obj->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
75 int check_from_path(string path)
77 if(path[-1]=='/') //remove last "/"
78 path=path[ .. (sizeof(path)-2)];
79 Stdio.File output = Stdio.File();
80 Process.create_process(({"git", "rev-parse", "--show-cdup"}), ([ "env":getenv(), "cwd":path , "stdout":output->pipe() ]))->wait();
81 string result = output->read();
90 write("Came inside check_from_path\n");
93 write("from path exists\n");
95 filename = basename(path);
96 Stdio.File output = Stdio.File();
97 Process.create_process(({ "git", "rev-parse", "--is-inside-work-tree"}), ([ "env":getenv(), "cwd":dir , "stdout":output->pipe() ]))->wait();
98 string result = output->read();
101 write("output is : "+result+"\n"+"returning 1\n");
106 write("output is :"+result+"\n"+"returning 0\n");
115 int check_steam_path(string path)
119 if(path[-1]=='/') //remove last "/"
120 path=path[ .. (sizeof(path)-2)];
126 int num_objects_create=0;
128 array parts = path/"/";
129 // parts = parts-({""}); //removing all blanks out
130 //dont need it mostly if(sizeof(parts)==4 && parts[1]=="home" && parts[3]=="")
132 /* DONT NEED THIS CHECK AS PATHS ARE GOING TO CHANGE
134 if(sizeof(parts)==3 && parts[1]=="home") // /home/ and /home/somefile not allowed
136 if(path!="/home/" && OBJ(path)->get_class()!="Room")
138 else if(path=="/home/")
141 else if(sizeof(parts)==2 && parts[1]=="home") // /home not allowed
145 parts = parts-({""}); //removing all blanks out
148 for(j=(sizeof(parts)-1); j>=0; j--)
150 write("Checking path : "+cur_path+"\n");
151 cont= _Server->get_module("filepath:tree")->path_to_object(cur_path,true);
152 if(cont||(cur_path=="")) //cur_path check for root-room
154 //CHANGE checking for /home/
155 if((OBJ(cur_path)->get_object_class() & CLASS_MODULE) && (sizeof(parts)==1||sizeof(parts)==2)) //checking whether inside /home/
158 test_parts=(cur_path/"/")-({""});
159 if(test_parts!=parts)
161 num_objects_create = sizeof(parts)-sizeof(test_parts);
162 res=create_object(cur_path, parts, num_objects_create);
168 if((sizeof(parts)==1)&&(parts[0]!="home")) //FIXME home should be taken out
170 res = create_object("/", parts, 1);
173 // cur_path = cur_path-("/"+parts[j]); //this would fail for "/x12/x1"-"/x1"
174 cur_path = cur_path[ ..sizeof(cur_path)-1-sizeof(parts[j])]; //replaces the above
180 int create_object(string path, array parts, int num)
182 if(path[-1]=='/' && sizeof(path)!=1) //remove last "/", but take care for only '/' (root room)
183 path=path[ .. (sizeof(path)-2)];
189 /* CHANGE Dont need to create document for last part. TO path will always be a folder, no matter what.
191 if(i==1 && !fromfolder) //CHANGE need to have a fromfolder var for specifyinh whether from path is a folder.
193 string name = parts[i*-1];
194 mapping map = (["url":path+"/"+name, "mimetype":"text/plain"]);
195 object doc = document_factory->execute(map);
198 doc->set_attribute("OBJ_DESC", "from import-from-git");
208 string container_name = parts[i*-1];
209 int check = create_container(container_name, path);
211 path=path+"/"+parts[i*-1];
219 int exists(string steampath) //checks a steam path exists or not
230 string get_steam_content(string steampath, int ver) //get the steam file's specific version's content
233 object obj = OBJ(steampath);
234 if(!steam_file_vers[steampath])
236 mapping versions = obj->query_attribute("DOC_VERSIONS");
237 if (!sizeof(versions))
239 versions = ([ 1:obj ]);
241 array this_history = ({});
242 foreach(versions; int nr; object version)
244 this_history += ({ ([ "obj":version, "version":nr, "time":version->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
246 sort(this_history->version, this_history);
247 if(sizeof(versions)>=1)
248 this_history += ({ ([ "obj":obj, "version":this_history[-1]->version+1, "time":obj->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
249 steam_file_vers = steam_file_vers + ([ steampath:this_history ]);
250 steam_number_vers = steam_number_vers + ([ steampath:obj->query_attribute("DOC_VERSION") ]);
251 steam_h = this_history;
255 steam_h = steam_file_vers[steampath];
257 return steam_h[ver-1]->obj->get_content();
260 array get_all_affected(string gitpath, int count)
262 Stdio.File output = Stdio.File();
263 // Process.create_process(({ "git", "show", sprintf("%s=%s","--pretty","format:"), "--name-only","-"+(string)count }), ([ "env":getenv(), "cwd":gitpath , "stdout":output->pipe() ]))->wait();
264 Process.create_process(({"git", "ls-files" }), (["env":getenv(), "cwd":gitpath , "stdout":output->pipe() ]))->wait();
265 string result = output->read();
266 array arr = result/"\n";
267 arr = Array.uniq(arr)-({""});
271 array get_commit_files(string gitpath, int ver) //returns list of files associated with a commit
273 if(gitpath[-1]=='/') //remove last "/"
274 gitpath=gitpath[ .. (sizeof(gitpath)-2)];
276 int total = get_num_versions(options->src);
277 string vers=(string)(total-(ver-1)-1);
278 Stdio.File output = Stdio.File();
279 Process.create_process(({ "git", "show", sprintf("%s=%s","--pretty","format:"), "--name-only","HEAD~"+vers }), ([ "env":getenv(), "cwd":gitpath , "stdout":output->pipe() ]))->wait();
280 string result = output->read();
281 return (result/"\n")-({""});
284 int create_document(string name, string path) //only for text/plain right now; path is where the object has to be created
286 if(path[-1]=='/' && sizeof(path)!=1) //remove last "/"
287 path=path[ .. (sizeof(path)-2)];
288 object document_factory = _Server->get_factory("Document");
289 mapping map = (["url":path+"/"+name, "mimetype":"text/plain"]);
290 object doc = document_factory->execute(map);
293 doc->set_attribute("OBJ_DESC", "from import-from-git");
297 int create_container(string name, string path)
299 if(path[-1]=='/' && sizeof(path)!=1) //remove last "/"
300 path=path[ .. (sizeof(path)-2)];
301 object container_factory = _Server->get_factory("Container");
302 object moveloc = OBJ(path);
303 object mycont = container_factory->execute((["name":name]));
306 mycont->move(moveloc);
307 mycont->set_attribute("OBJ_DESC","from import-from-git");
314 int get_num_versions(string path)
316 if(path[-1]=='/') //remove last "/"
317 path=path[ .. (sizeof(path)-2)];
318 Stdio.File output = Stdio.File();
319 // write("filename for get_num_versions is "+filename+"\n");
320 Process.create_process(({ "git", "rev-list", "HEAD", "--count" }), ([ "env":getenv(), "cwd":path , "stdout":output->pipe() ]))->wait();
321 string result = output->read();
325 string tempcontent = " ";
326 int check_content(string steampath, string gitpath, int wasempty)
328 int version = get_version(steampath);
329 versions[steampath] = versions[steampath] + 1;
330 version = get_version(steampath);
331 if(steam_number_vers[steampath])
332 if(version>steam_number_vers[steampath])
334 to_import = to_import + ([ steampath:(["gitpath":gitpath, "commitcount":commitcount, "empty":wasempty]) ]);
337 string steamcontent = get_steam_content(steampath, version);
338 if(tempcontent == steamcontent && !wasempty)
339 return 3; //empty commit not present but same file content in sTeam.
340 tempcontent = steamcontent;
341 string gitcontent = git_version_content(gitpath, commitcount, wasempty);
343 if(gitcontent == steamcontent)
345 if(version==steam_number_vers[steampath])
347 // o->write("versions["+steampath+"] is "+versions[steampath]+". Incrementing now\n");
348 // versions[steampath] = versions[steampath] + 1;
354 int get_version(string steampath)
356 if(versions[steampath])
357 return versions[steampath];
358 versions = versions + ([steampath : 0]);
362 string git_version_content(string path, int version, int wasempty)
364 //FIXME empty commit handling 2
367 write("GIT EMPTY COMMIT encountered. checking against last content - "+path+"\n");
369 version = version - diff;
371 //----------------------------
372 //FIXME MIGHT WANT TO ADD the "/" check on options->src
373 string ver = (string)version;
374 // write("total passed is : "+total+"\n");
375 int total = get_num_versions(options->src);
376 ver=(string)(total-((int)ver-1)-1);
377 //all empty commits above should be added to HEAD~ver so that it points to the right commit
379 path=path[ .. (sizeof(path)-2)];
380 string dir = dirname(path);
381 string filename = basename(path);
382 Stdio.File output = Stdio.File();
383 Process.create_process(({ "git", "show", "HEAD~"+ver+":./"+filename }), ([ "env":getenv(), "cwd":dir , "stdout":output->pipe() ]))->wait();
384 string result = output->read();
388 int handle_append(string from, string to, array steam_history, int num_git_versions)
390 write("inside handle append\n");
391 string scontent = steam_history[sizeof(steam_history)-1]->obj->get_content();
392 string gcontent = git_version_content(from,(string)1,num_git_versions);
393 write("scontent : "+scontent+"\n");
394 write("gcontent : "+gcontent+"\n");
395 if(scontent==gcontent)
398 for(i=2;i<=num_git_versions;i++)
400 string content = git_version_content(from,(string)i, num_git_versions);
401 OBJ(to)->set_content(content);
409 int handle_force(string from, string to, int num_git_versions)
412 for(i=1;i<=num_git_versions;i++)
414 string content = git_version_content(from,(string)(i), num_git_versions);
415 OBJ(to)->set_content(content);
420 int handle_normal(string from, string to, int num, array steam_history, int num_git_versions)
422 string scontent = steam_history[num-1]->obj->get_content();
423 // write("STEAM HISTORY IS : %O\n and number is %d",steam_history,num-1);
424 string gcontent = git_version_content(from,(string)(num),num_git_versions);
427 if((num>sizeof(steam_history))||((num==1)&&!scontent)) //after successful history, add it to steam. (second condition for object in sTeam with no versions).
429 o->write("Start adding now\n");
431 for(i=num;i<=num_git_versions;i++)
433 string content = git_version_content(from,(string)(i), num_git_versions);
434 OBJ(to)->set_content(content);
438 write("Comparing\n");
439 write("scontent : "+scontent+"\n");
440 o->write("scontent : "+scontent+"\n");
441 write("gcontent : "+gcontent+"\n");
442 o->write("gcontent : "+gcontent+"\n");
443 if(scontent==gcontent)
447 return handle_normal(from, to, num+1, steam_history, num_git_versions);
451 write("Not Equal\n");
452 write("Exiting from script. Commits and versions dont match\n");
453 o->write("Not equal\nCommits and versions dont match\n");
458 string show_bestoption(string from, string to, array steam_history, int num_git_versions)
460 //CHECKING FOR NORMAL
462 string scontent,gcontent;
463 for(i=1;i<=sizeof(steam_history);i++)
465 scontent = steam_history[i-1]->obj->get_content();
466 gcontent = git_version_content(from,(string)i,num_git_versions);
467 if(scontent!=gcontent)
476 //CHECKING FOR APPEND
477 scontent = steam_history[sizeof(steam_history)-1]->obj->get_content();
478 gcontent = git_version_content(from,(string)1,num_git_versions);
479 if(scontent==gcontent)
481 else //OTHERWISE FORCE OPTION