import-from-git._pike
Go to the documentation of this file.
1 /* array steam_history = get_steam_versions(to);
2  o->write("STEAM HISTORY\n%O\n",steam_history);
3  if(options->bestoption)
4  {
5  string best = show_bestoption(from, to, steam_history, num_versions);
6  write("Best option : "+best+"\n");
7  }
8  else
9  {
10  if(options->append)
11  a = handle_append(from, to, steam_history, num_versions);
12  else if(options->force)
13  a = handle_force(from, to, num_versions);
14  else
15  a = handle_normal(from, to, 1, steam_history, num_versions);
16 
17  if(a)
18  {
19  write("Succesfully imported\n");
20  }
21  else
22  {
23  write("import failed\n");
24  }
25  }
26 */
27 class import-from-git {
28 public:
29 
30  }
31 }
32 
33 void handle_to_import(mapping toimport)
34 {
35  foreach(toimport; string steampath; mapping gitarr)
36  {
37  if(!OBJ(steampath))
38  {
39  check_steam_path(dirname(steampath)); //only creates containers
40  int a = create_document(basename(steampath), dirname(steampath));
41  if(!a)
42  {
43  write("SOME ERROR WHILE CREATING DOCUMENT INSIDE "+dirname(steampath)+".\n");
44  break;
45  }
46  }
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");
50  }
51  to_import = ([ ]); //this one is the global mapping set to empty
52 }
53 
54 
55 array get_steam_versions(string steampath)
56 {
57  object obj = OBJ(steampath);
58  mapping versions = obj->query_attribute("DOC_VERSIONS");
59  if (!sizeof(versions))
60  {
61  versions = ([ 1:obj ]);
62  }
63  array this_history = ({});
64  foreach(versions; int nr; object version)
65  {
66  this_history += ({ ([ "obj":version, "version":nr, "time":version->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
67  }
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") ]) });
71  return this_history;
72 }
73 
74 
75 int check_from_path(string path)
76 {
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();
82  if(sizeof(result)==1)
83  return 1;
84  else
85  return 0;
86  return 0;
87 /*
88  string dir,filename;
89 
90  write("Came inside check_from_path\n");
91  if(Stdio.exist(path))
92  {
93  write("from path exists\n");
94  dir = dirname(path);
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();
99  if(result)
100  {
101  write("output is : "+result+"\n"+"returning 1\n");
102  return 1;
103  }
104  else
105  {
106  write("output is :"+result+"\n"+"returning 0\n");
107  return 0;
108  }
109  }
110  return 0;
111 */
112 }
113 
114 
115 int check_steam_path(string path)
116 {
117  if(path=="/")
118  return 1;
119  if(path[-1]=='/') //remove last "/"
120  path=path[ .. (sizeof(path)-2)];
121  int j=0;
122  int i=0;
123  string cur_path;
124  array test_parts;
125  object cont;
126  int num_objects_create=0;
127  int res=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]=="")
131 
132 /* DONT NEED THIS CHECK AS PATHS ARE GOING TO CHANGE
133 -----
134  if(sizeof(parts)==3 && parts[1]=="home") // /home/ and /home/somefile not allowed
135  {
136  if(path!="/home/" && OBJ(path)->get_class()!="Room")
137  return 0;
138  else if(path=="/home/")
139  return 0;
140  }
141  else if(sizeof(parts)==2 && parts[1]=="home") // /home not allowed
142  return 0;
143 -----
144 */
145  parts = parts-({""}); //removing all blanks out
146  cur_path = path;
147 
148  for(j=(sizeof(parts)-1); j>=0; j--)
149  {
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
153  {
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/
156  return 0;
157 
158  test_parts=(cur_path/"/")-({""});
159  if(test_parts!=parts)
160  {
161  num_objects_create = sizeof(parts)-sizeof(test_parts);
162  res=create_object(cur_path, parts, num_objects_create);
163  }
164  return 1;
165  }
166  else
167  {
168  if((sizeof(parts)==1)&&(parts[0]!="home")) //FIXME home should be taken out
169  {
170  res = create_object("/", parts, 1);
171  return 1;
172  }
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
175  }
176  }
177  return 0;
178 }
179 
180 int create_object(string path, array parts, int num)
181 {
182  if(path[-1]=='/' && sizeof(path)!=1) //remove last "/", but take care for only '/' (root room)
183  path=path[ .. (sizeof(path)-2)];
184  int i=0;
185 
186 
187  for(i=num; i>0; i--)
188  {
189 /* CHANGE Dont need to create document for last part. TO path will always be a folder, no matter what.
190 
191  if(i==1 && !fromfolder) //CHANGE need to have a fromfolder var for specifyinh whether from path is a folder.
192  {
193  string name = parts[i*-1];
194  mapping map = (["url":path+"/"+name, "mimetype":"text/plain"]);
195  object doc = document_factory->execute(map);
196  if(doc)
197  {
198  doc->set_attribute("OBJ_DESC", "from import-from-git");
199  return 1;
200  }
201  else
202  return 0;
203  }
204 
205  else
206  {
207 */
208  string container_name = parts[i*-1];
209  int check = create_container(container_name, path);
210  if(check==1)
211  path=path+"/"+parts[i*-1];
212  else
213  return 0;
214 // }
215  }
216  return 1;
217 }
218 
219 int exists(string steampath) //checks a steam path exists or not
220 {
221  mixed error = catch{
222  if(OBJ(steampath))
223  return 1;
224  else
225  return 0;
226  };
227  return 0;
228 }
229 
230 string get_steam_content(string steampath, int ver) //get the steam file's specific version's content
231 {
232  array steam_h=({});
233  object obj = OBJ(steampath);
234  if(!steam_file_vers[steampath])
235  {
236  mapping versions = obj->query_attribute("DOC_VERSIONS");
237  if (!sizeof(versions))
238  {
239  versions = ([ 1:obj ]);
240  }
241  array this_history = ({});
242  foreach(versions; int nr; object version)
243  {
244  this_history += ({ ([ "obj":version, "version":nr, "time":version->query_attribute("DOC_LAST_MODIFIED"), "path":obj->query_attribute("OBJ_PATH") ]) });
245  }
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;
252  }
253  else
254  {
255  steam_h = steam_file_vers[steampath];
256  }
257  return steam_h[ver-1]->obj->get_content();
258 }
259 //Look here
260 array get_all_affected(string gitpath, int count)
261 {
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)-({""});
268  return arr;
269 }
270 
271 array get_commit_files(string gitpath, int ver) //returns list of files associated with a commit
272 {
273  if(gitpath[-1]=='/') //remove last "/"
274  gitpath=gitpath[ .. (sizeof(gitpath)-2)];
275 
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")-({""});
282 }
283 
284 int create_document(string name, string path) //only for text/plain right now; path is where the object has to be created
285 {
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);
291  if(!doc)
292  return 0;
293  doc->set_attribute("OBJ_DESC", "from import-from-git");
294  return 1;
295 }
296 
297 int create_container(string name, string path)
298 {
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]));
304  if(mycont)
305  {
306  mycont->move(moveloc);
307  mycont->set_attribute("OBJ_DESC","from import-from-git");
308  }
309  else
310  return 0;
311  return 1;
312 }
313 
314 int get_num_versions(string path)
315 {
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();
322  return (int)result;
323 }
324 
325 string tempcontent = " ";
326 int check_content(string steampath, string gitpath, int wasempty)
327 {
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])
333  {
334  to_import = to_import + ([ steampath:(["gitpath":gitpath, "commitcount":commitcount, "empty":wasempty]) ]);
335  return 2;
336  }
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);
342 
343  if(gitcontent == steamcontent)
344  {
345  if(version==steam_number_vers[steampath])
346  return 2;
347 // o->write("versions["+steampath+"] is "+versions[steampath]+". Incrementing now\n");
348 // versions[steampath] = versions[steampath] + 1;
349  return 1;
350  }
351  return 0;
352 }
353 
354 int get_version(string steampath)
355 {
356  if(versions[steampath])
357  return versions[steampath];
358  versions = versions + ([steampath : 0]);
359  return 0;
360 }
361 int diff = 0;
362 string git_version_content(string path, int version, int wasempty)
363 {
364 //FIXME empty commit handling 2
365  if(wasempty)
366  {
367  write("GIT EMPTY COMMIT encountered. checking against last content - "+path+"\n");
368  diff=diff+1;
369  version = version - diff;
370  }
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
378  if(path[-1]=='/')
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();
385  return result;
386 }
387 /*
388 int handle_append(string from, string to, array steam_history, int num_git_versions)
389 {
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)
396  {
397  int i=0;
398  for(i=2;i<=num_git_versions;i++)
399  {
400  string content = git_version_content(from,(string)i, num_git_versions);
401  OBJ(to)->set_content(content);
402  }
403  return 1;
404  }
405  return 0;
406 }
407 
408 
409 int handle_force(string from, string to, int num_git_versions)
410 {
411  int i=0;
412  for(i=1;i<=num_git_versions;i++)
413  {
414  string content = git_version_content(from,(string)(i), num_git_versions);
415  OBJ(to)->set_content(content);
416  }
417  return 1;
418 }
419 
420 int handle_normal(string from, string to, int num, array steam_history, int num_git_versions)
421 {
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);
425 
426 
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).
428  {
429  o->write("Start adding now\n");
430  int i=0;
431  for(i=num;i<=num_git_versions;i++)
432  {
433  string content = git_version_content(from,(string)(i), num_git_versions);
434  OBJ(to)->set_content(content);
435  }
436  return 1;
437  }
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)
444  {
445  write("Equal\n");
446  o->write("Equal\n");
447  return handle_normal(from, to, num+1, steam_history, num_git_versions);
448  }
449  else
450  {
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");
454  return 0;
455  }
456 }
457 
458 string show_bestoption(string from, string to, array steam_history, int num_git_versions)
459 {
460  //CHECKING FOR NORMAL
461  int i=0,flag=0;
462  string scontent,gcontent;
463  for(i=1;i<=sizeof(steam_history);i++)
464  {
465  scontent = steam_history[i-1]->obj->get_content();
466  gcontent = git_version_content(from,(string)i,num_git_versions);
467  if(scontent!=gcontent)
468  {
469  flag=1;
470  break;
471  }
472  }
473  if(flag==0)
474  return "normal";
475 
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)
480  return "append(-A)";
481  else //OTHERWISE FORCE OPTION
482  return "force(-F)";
483 }
484 */
485 
486 
487 };