]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
5bbc1746b417ae0dd2720dbb4c575a4d8076e637
[lyx.git] / src / VCBackend.cpp
1 /**
2  * \file VCBackend.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Pavel Sanda
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "VCBackend.h"
15 #include "Buffer.h"
16 #include "LyX.h"
17 #include "FuncRequest.h"
18
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
21
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
29
30 #include <boost/regex.hpp>
31
32 #include <fstream>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 using boost::regex;
38 using boost::regex_match;
39 using boost::smatch;
40
41 namespace lyx {
42
43
44 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
45 {
46         LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
47         Systemcall one;
48         support::PathChanger p(path);
49         return one.startscript(Systemcall::Wait, cmd, false);
50 }
51
52
53 int VCS::doVCCommand(string const & cmd, FileName const & path)
54 {
55         if (owner_)
56                 owner_->setBusy(true);
57
58         int const ret = doVCCommandCall(cmd, path);
59
60         if (owner_)
61                 owner_->setBusy(false);
62         if (ret)
63                 frontend::Alert::error(_("Revision control error."),
64                         bformat(_("Some problem occured while running the command:\n"
65                                   "'%1$s'."),
66                         from_utf8(cmd)));
67         return ret;
68 }
69
70
71 /////////////////////////////////////////////////////////////////////
72 //
73 // RCS
74 //
75 /////////////////////////////////////////////////////////////////////
76
77 RCS::RCS(FileName const & m)
78 {
79         master_ = m;
80         scanMaster();
81 }
82
83
84 FileName const RCS::findFile(FileName const & file)
85 {
86         // Check if *,v exists.
87         FileName tmp(file.absFilename() + ",v");
88         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
89         if (tmp.isReadableFile()) {
90                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
91                 return tmp;
92         }
93
94         // Check if RCS/*,v exists.
95         tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
96         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
97         if (tmp.isReadableFile()) {
98                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
99                 return tmp;
100         }
101
102         return FileName();
103 }
104
105
106 void RCS::retrieve(FileName const & file)
107 {
108         LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
109         doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
110                          FileName());
111 }
112
113
114 void RCS::scanMaster()
115 {
116         if (master_.empty())
117                 return;
118
119         LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
120
121         ifstream ifs(master_.toFilesystemEncoding().c_str());
122
123         string token;
124         bool read_enough = false;
125
126         while (!read_enough && ifs >> token) {
127                 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
128                         << token << '\'');
129
130                 if (token.empty())
131                         continue;
132                 else if (token == "head") {
133                         // get version here
134                         string tmv;
135                         ifs >> tmv;
136                         tmv = rtrim(tmv, ";");
137                         version_ = tmv;
138                         LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
139                 } else if (contains(token, "access")
140                            || contains(token, "symbols")
141                            || contains(token, "strict")) {
142                         // nothing
143                 } else if (contains(token, "locks")) {
144                         // get locker here
145                         if (contains(token, ';')) {
146                                 locker_ = "Unlocked";
147                                 vcstatus = UNLOCKED;
148                                 continue;
149                         }
150                         string tmpt;
151                         string s1;
152                         string s2;
153                         do {
154                                 ifs >> tmpt;
155                                 s1 = rtrim(tmpt, ";");
156                                 // tmp is now in the format <user>:<version>
157                                 s1 = split(s1, s2, ':');
158                                 // s2 is user, and s1 is version
159                                 if (s1 == version_) {
160                                         locker_ = s2;
161                                         vcstatus = LOCKED;
162                                         break;
163                                 }
164                         } while (!contains(tmpt, ';'));
165
166                 } else if (token == "comment") {
167                         // we don't need to read any further than this.
168                         read_enough = true;
169                 } else {
170                         // unexpected
171                         LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
172                 }
173         }
174 }
175
176
177 void RCS::registrer(string const & msg)
178 {
179         string cmd = "ci -q -u -i -t-\"";
180         cmd += msg;
181         cmd += "\" ";
182         cmd += quoteName(onlyFilename(owner_->absFileName()));
183         doVCCommand(cmd, FileName(owner_->filePath()));
184 }
185
186
187 string RCS::checkIn(string const & msg)
188 {
189         int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
190                     + quoteName(onlyFilename(owner_->absFileName())),
191                     FileName(owner_->filePath()));
192         return ret ? string() : "RCS: Proceeded";
193 }
194
195
196 bool RCS::checkInEnabled()
197 {
198         return owner_ && !owner_->isReadonly();
199 }
200
201
202 string RCS::checkOut()
203 {
204         owner_->markClean();
205         int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
206                     FileName(owner_->filePath()));
207         return ret ? string() : "RCS: Proceeded";
208 }
209
210
211 bool RCS::checkOutEnabled()
212 {
213         return owner_ && owner_->isReadonly();
214 }
215
216
217 string RCS::repoUpdate()
218 {
219         lyxerr << "Sorry, not implemented." << endl;
220         return string();
221 }
222
223
224 bool RCS::repoUpdateEnabled()
225 {
226         return false;
227 }
228
229
230 string RCS::lockingToggle()
231 {
232         lyxerr << "Sorry, not implemented." << endl;
233         return string();
234 }
235
236
237 bool RCS::lockingToggleEnabled()
238 {
239         return false;
240 }
241
242
243 void RCS::revert()
244 {
245         doVCCommand("co -f -u" + version_ + " "
246                     + quoteName(onlyFilename(owner_->absFileName())),
247                     FileName(owner_->filePath()));
248         // We ignore changes and just reload!
249         owner_->markClean();
250 }
251
252
253 void RCS::undoLast()
254 {
255         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
256         doVCCommand("rcs -o" + version_ + " "
257                     + quoteName(onlyFilename(owner_->absFileName())),
258                     FileName(owner_->filePath()));
259 }
260
261
262 bool RCS::undoLastEnabled()
263 {
264         return true;
265 }
266
267
268 void RCS::getLog(FileName const & tmpf)
269 {
270         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
271                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
272                     FileName(owner_->filePath()));
273 }
274
275
276 bool RCS::toggleReadOnlyEnabled()
277 {
278         // This got broken somewhere along lfuns dispatch reorganization.
279         // reloadBuffer would be needed after this, but thats problematic
280         // since we are inside Buffer::dispatch.
281         // return true;
282         return false;
283 }
284
285 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
286 {
287         if (info == LyXVC::File)
288                 return version_;
289         return string();
290 }
291
292
293 bool RCS::prepareFileRevision(string const &revis, string & f)
294 {
295         string rev = revis;
296
297         if (isStrInt(rev)) {
298                 int back = convert<int>(rev);
299                 if (back > 0)
300                         return false;
301                 if (back == 0)
302                         rev = version_;
303                 // we care about the last number from revision string
304                 // in case of backward indexing
305                 if (back < 0) {
306                         string cur, base;
307                         cur = rsplit(version_, base , '.' );
308                         if (!isStrInt(cur))
309                                 return false;
310                         int want = convert<int>(cur) + back;
311                         if (want <= 0)
312                                 return false;
313
314                         rev = base + "." + convert<string>(want);
315                 }
316         }
317
318         FileName tmpf = FileName::tempName("lyxvcrev");
319         if (tmpf.empty()) {
320                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
321                 return N_("Error: Could not generate logfile.");
322         }
323
324         doVCCommand("co -p" + rev + " "
325                       + quoteName(onlyFilename(owner_->absFileName()))
326                       + " > " + quoteName(tmpf.toFilesystemEncoding()),
327                 FileName(owner_->filePath()));
328         if (tmpf.isFileEmpty())
329                 return false;
330
331         f = tmpf.absFilename();
332         return true;
333 }
334
335
336 bool RCS::prepareFileRevisionEnabled()
337 {
338         return true;
339 }
340
341
342 /////////////////////////////////////////////////////////////////////
343 //
344 // CVS
345 //
346 /////////////////////////////////////////////////////////////////////
347
348 CVS::CVS(FileName const & m, FileName const & f)
349 {
350         master_ = m;
351         file_ = f;
352         scanMaster();
353 }
354
355
356 FileName const CVS::findFile(FileName const & file)
357 {
358         // First we look for the CVS/Entries in the same dir
359         // where we have file.
360         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
361         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
362         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
363                              << "' for `" << tmpf << '\'');
364         if (entries.isReadableFile()) {
365                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
366                 // and see if we can find this file. We do a fast and
367                 // dirty parse here.
368                 ifstream ifs(entries.toFilesystemEncoding().c_str());
369                 string line;
370                 while (getline(ifs, line)) {
371                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
372                         if (contains(line, tmpf))
373                                 return entries;
374                 }
375         }
376         return FileName();
377 }
378
379
380 void CVS::scanMaster()
381 {
382         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
383         // Ok now we do the real scan...
384         ifstream ifs(master_.toFilesystemEncoding().c_str());
385         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
386         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
387         string line;
388         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
389         while (getline(ifs, line)) {
390                 LYXERR(Debug::LYXVC, "\t  line: " << line);
391                 if (contains(line, tmpf)) {
392                         // Ok extract the fields.
393                         smatch sm;
394
395                         regex_match(line, sm, reg);
396
397                         //sm[0]; // whole matched string
398                         //sm[1]; // filename
399                         version_ = sm.str(2);
400                         string const file_date = sm.str(3);
401
402                         //sm[4]; // options
403                         //sm[5]; // tag or tagdate
404                         // FIXME: must double check file is stattable/existing
405                         time_t mod = file_.lastModified();
406                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
407                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
408                                 << "'\nModification date of file: `" << mod_date << '\'');
409                         //FIXME this whole locking bussiness is not working under cvs and the machinery
410                         // conforms to the ci usage, not cvs.
411                         if (file_date == mod_date) {
412                                 locker_ = "Unlocked";
413                                 vcstatus = UNLOCKED;
414                         } else {
415                                 // Here we should also do some more checking
416                                 // to see if there are conflicts or not.
417                                 locker_ = "Locked";
418                                 vcstatus = LOCKED;
419                         }
420                         break;
421                 }
422         }
423 }
424
425
426 void CVS::registrer(string const & msg)
427 {
428         doVCCommand("cvs -q add -m \"" + msg + "\" "
429                     + quoteName(onlyFilename(owner_->absFileName())),
430                     FileName(owner_->filePath()));
431 }
432
433
434 string CVS::checkIn(string const & msg)
435 {
436         int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
437                     + quoteName(onlyFilename(owner_->absFileName())),
438                     FileName(owner_->filePath()));
439         return ret ? string() : "CVS: Proceeded";
440 }
441
442
443 bool CVS::checkInEnabled()
444 {
445         return true;
446 }
447
448
449 string CVS::checkOut()
450 {
451         // cvs update or perhaps for cvs this should be a noop
452         // we need to detect conflict (eg "C" in output)
453         // before we can do this.
454         lyxerr << "Sorry, not implemented." << endl;
455         return string();
456 }
457
458
459 bool CVS::checkOutEnabled()
460 {
461         return false;
462 }
463
464
465 string CVS::repoUpdate()
466 {
467         lyxerr << "Sorry, not implemented." << endl;
468         return string();
469 }
470
471
472 bool CVS::repoUpdateEnabled()
473 {
474         return false;
475 }
476
477
478 string CVS::lockingToggle()
479 {
480         lyxerr << "Sorry, not implemented." << endl;
481         return string();
482 }
483
484
485 bool CVS::lockingToggleEnabled()
486 {
487         return false;
488 }
489
490
491 void CVS::revert()
492 {
493         // Reverts to the version in CVS repository and
494         // gets the updated version from the repository.
495         string const fil = quoteName(onlyFilename(owner_->absFileName()));
496         // This is sensitive operation, so at lest some check about
497         // existence of cvs program and its file
498         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
499                 return;
500         FileName f(owner_->absFileName());
501         f.removeFile();
502         doVCCommand("cvs update " + fil,
503                     FileName(owner_->filePath()));
504         owner_->markClean();
505 }
506
507
508 void CVS::undoLast()
509 {
510         // merge the current with the previous version
511         // in a reverse patch kind of way, so that the
512         // result is to revert the last changes.
513         lyxerr << "Sorry, not implemented." << endl;
514 }
515
516
517 bool CVS::undoLastEnabled()
518 {
519         return false;
520 }
521
522
523 void CVS::getLog(FileName const & tmpf)
524 {
525         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
526                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
527                     FileName(owner_->filePath()));
528 }
529
530
531 bool CVS::toggleReadOnlyEnabled()
532 {
533         return false;
534 }
535
536
537 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
538 {
539         if (info == LyXVC::File)
540                 return version_;
541         return string();
542 }
543
544
545 bool CVS::prepareFileRevision(string const &, string &)
546 {
547         return false;
548 }
549
550
551 bool CVS::prepareFileRevisionEnabled()
552 {
553         return false;
554 }
555
556
557 /////////////////////////////////////////////////////////////////////
558 //
559 // SVN
560 //
561 /////////////////////////////////////////////////////////////////////
562
563 SVN::SVN(FileName const & m, FileName const & f)
564 {
565         owner_ = 0;
566         master_ = m;
567         file_ = f;
568         locked_mode_ = 0;
569         scanMaster();
570 }
571
572
573 FileName const SVN::findFile(FileName const & file)
574 {
575         // First we look for the .svn/entries in the same dir
576         // where we have file.
577         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
578         string const tmpf = onlyFilename(file.absFilename());
579         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
580                              << "' for `" << tmpf << '\'');
581         if (entries.isReadableFile()) {
582                 // Ok we are at least in a SVN dir. Parse the .svn/entries
583                 // and see if we can find this file. We do a fast and
584                 // dirty parse here.
585                 ifstream ifs(entries.toFilesystemEncoding().c_str());
586                 string line, oldline;
587                 while (getline(ifs, line)) {
588                         if (line == "dir" || line == "file")
589                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
590                         if (oldline == tmpf && line == "file")
591                                 return entries;
592                         oldline = line;
593                 }
594         }
595         return FileName();
596 }
597
598
599 void SVN::scanMaster()
600 {
601         // vcstatus code is somewhat superflous, until we want
602         // to implement read-only toggle for svn.
603         vcstatus = NOLOCKING;
604         if (checkLockMode()) {
605                 if (isLocked()) {
606                         vcstatus = LOCKED;
607                 } else {
608                         vcstatus = UNLOCKED;
609                 }
610         }
611 }
612
613
614 bool SVN::checkLockMode()
615 {
616         FileName tmpf = FileName::tempName("lyxvcout");
617         if (tmpf.empty()){
618                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
619                 return N_("Error: Could not generate logfile.");
620         }
621
622         LYXERR(Debug::LYXVC, "Detecting locking mode...");
623         if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
624                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
625                     file_.onlyPath()))
626                 return false;
627
628         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
629         string line;
630         bool ret = false;
631
632         while (ifs) {
633                 getline(ifs, line);
634                 LYXERR(Debug::LYXVC, line);
635                 if (contains(line, "svn:needs-lock"))
636                         ret = true;
637         }
638         LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
639         ifs.close();
640         locked_mode_ = ret;
641         return ret;
642
643 }
644
645
646 bool SVN::isLocked() const
647 {
648         file_.refresh();
649         return !file_.isReadOnly();
650 }
651
652
653 void SVN::registrer(string const & /*msg*/)
654 {
655         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
656                     FileName(owner_->filePath()));
657 }
658
659
660 string SVN::checkIn(string const & msg)
661 {
662         FileName tmpf = FileName::tempName("lyxvcout");
663         if (tmpf.empty()){
664                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
665                 return N_("Error: Could not generate logfile.");
666         }
667
668         doVCCommand("svn commit -m \"" + msg + "\" "
669                     + quoteName(onlyFilename(owner_->absFileName()))
670                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
671                     FileName(owner_->filePath()));
672
673         string log;
674         string res = scanLogFile(tmpf, log);
675         if (!res.empty())
676                 frontend::Alert::error(_("Revision control error."),
677                                 _("Error when committing to repository.\n"
678                                 "You have to manually resolve the problem.\n"
679                                 "LyX will reopen the document after you press OK."));
680         else
681                 fileLock(false, tmpf, log);
682
683         tmpf.erase();
684         return log.empty() ? string() : "SVN: " + log;
685 }
686
687
688 bool SVN::checkInEnabled()
689 {
690         if (locked_mode_)
691                 return isLocked();
692         else
693                 return true;
694 }
695
696
697 // FIXME Correctly return code should be checked instead of this.
698 // This would need another solution than just plain startscript.
699 // Hint from Andre': QProcess::readAllStandardError()...
700 string SVN::scanLogFile(FileName const & f, string & status)
701 {
702         ifstream ifs(f.toFilesystemEncoding().c_str());
703         string line;
704
705         while (ifs) {
706                 getline(ifs, line);
707                 LYXERR(Debug::LYXVC, line << "\n");
708                 if (!line.empty()) 
709                         status += line + "; ";
710                 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
711                                          || contains(line, "Commit failed")) {
712                         ifs.close();
713                         return line;
714                 }
715                 if (contains(line, "svn:needs-lock")) {
716                         ifs.close();
717                         return line;
718                 }
719         }
720         ifs.close();
721         return string();
722 }
723
724
725 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
726 {
727         if (!locked_mode_ || (isLocked() == lock))
728                 return;
729
730         string const arg = lock ? "lock " : "unlock ";
731         doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
732                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
733                     FileName(owner_->filePath()));
734
735         // Lock error messages go unfortunately on stderr and are unreachible this way.
736         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
737         string line;
738         while (ifs) {
739                 getline(ifs, line);
740                 if (!line.empty()) status += line + "; ";
741         }
742         ifs.close();
743
744         if (!isLocked() && lock)
745                 frontend::Alert::error(_("Revision control error."),
746                         _("Error while acquiring write lock.\n"
747                         "Another user is most probably editing\n"
748                         "the current document now!\n"
749                         "Also check the access to the repository."));
750         if (isLocked() && !lock)
751                 frontend::Alert::error(_("Revision control error."),
752                         _("Error while releasing write lock.\n"
753                         "Check the access to the repository."));
754 }
755
756
757 string SVN::checkOut()
758 {
759         FileName tmpf = FileName::tempName("lyxvcout");
760         if (tmpf.empty()) {
761                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
762                 return N_("Error: Could not generate logfile.");
763         }
764
765         doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
766                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
767                     FileName(owner_->filePath()));
768
769         string log;
770         string const res = scanLogFile(tmpf, log);
771         if (!res.empty())
772                 frontend::Alert::error(_("Revision control error."),
773                         bformat(_("Error when updating from repository.\n"
774                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
775                                 "After pressing OK, LyX will try to reopen the resolved document."),
776                         from_local8bit(res)));
777
778         fileLock(true, tmpf, log);
779
780         tmpf.erase();
781         return log.empty() ? string() : "SVN: " + log;
782 }
783
784
785 bool SVN::checkOutEnabled()
786 {
787         if (locked_mode_)
788                 return !isLocked();
789         else
790                 return true;
791 }
792
793
794 string SVN::repoUpdate()
795 {
796         FileName tmpf = FileName::tempName("lyxvcout");
797         if (tmpf.empty()) {
798                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
799                 return N_("Error: Could not generate logfile.");
800         }
801
802         doVCCommand("svn diff " + quoteName(owner_->filePath())
803                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
804                 FileName(owner_->filePath()));
805         docstring res = tmpf.fileContents("UTF-8");
806         if (!res.empty()) {
807                 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
808                 docstring const file = from_utf8(owner_->filePath());
809                 docstring text = bformat(_("There were detected changes "
810                                 "in the working directory:\n%1$s\n\n"
811                                 "In case of file conflict version of the local directory files "
812                                 "will be preferred."
813                                 "\n\nContinue?"), file);
814                 int ret = frontend::Alert::prompt(_("Changes detected"),
815                                 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
816                 if (ret == 2 ) {
817                         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
818                         ret = frontend::Alert::prompt(_("Changes detected"),
819                                 text, 0, 1, _("&Yes"), _("&No"));
820                         hideDialogs("file", 0);
821                 }
822                 if (ret == 1 ) {
823                         tmpf.erase();
824                         return string();
825                 }
826         }
827
828         // Reverting looks too harsh, see bug #6255.
829         // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
830         // + " > " + quoteName(tmpf.toFilesystemEncoding()),
831         // FileName(owner_->filePath()));
832         // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
833         doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
834                 + " > " + quoteName(tmpf.toFilesystemEncoding()),
835         FileName(owner_->filePath()));
836         res += "Update log:\n" + tmpf.fileContents("UTF-8");
837
838         LYXERR(Debug::LYXVC, res);
839         tmpf.erase();
840         return to_utf8(res);
841 }
842
843
844 bool SVN::repoUpdateEnabled()
845 {
846         return true;
847 }
848
849
850 string SVN::lockingToggle()
851 {
852         FileName tmpf = FileName::tempName("lyxvcout");
853         if (tmpf.empty()) {
854                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
855                 return N_("Error: Could not generate logfile.");
856         }
857
858         int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
859                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
860                     FileName(owner_->filePath()));
861         if (ret)
862                 return string();
863
864         string log;
865         string res = scanLogFile(tmpf, log);
866         bool locking = contains(res, "svn:needs-lock");
867         if (!locking)
868                 ret = doVCCommand("svn propset svn:needs-lock ON "
869                     + quoteName(onlyFilename(owner_->absFileName()))
870                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
871                     FileName(owner_->filePath()));
872         else
873                 ret = doVCCommand("svn propdel svn:needs-lock "
874                     + quoteName(onlyFilename(owner_->absFileName()))
875                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
876                     FileName(owner_->filePath()));
877         if (ret)
878                 return string();
879
880         tmpf.erase();
881         frontend::Alert::warning(_("VCN File Locking"),
882                 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
883                 + _("Do not forget to commit the locking property into the repository."),
884                 true);
885
886         return string("SVN: ") +  N_("Locking property set.");
887 }
888
889
890 bool SVN::lockingToggleEnabled()
891 {
892         return true;
893 }
894
895
896 void SVN::revert()
897 {
898         // Reverts to the version in CVS repository and
899         // gets the updated version from the repository.
900         string const fil = quoteName(onlyFilename(owner_->absFileName()));
901
902         doVCCommand("svn revert -q " + fil,
903                     FileName(owner_->filePath()));
904         owner_->markClean();
905 }
906
907
908 void SVN::undoLast()
909 {
910         // merge the current with the previous version
911         // in a reverse patch kind of way, so that the
912         // result is to revert the last changes.
913         lyxerr << "Sorry, not implemented." << endl;
914 }
915
916
917 bool SVN::undoLastEnabled()
918 {
919         return false;
920 }
921
922
923 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
924 {
925         if (info == LyXVC::Tree) {
926                         if (rev_tree_cache_.empty())
927                                 if (!getTreeRevisionInfo())
928                                         rev_tree_cache_ = "?";
929                         if (rev_tree_cache_ == "?")
930                                 return string();
931
932                         return rev_tree_cache_;
933         }
934
935         // fill the rest of the attributes for a single file
936         if (rev_file_cache_.empty())
937                 if (!getFileRevisionInfo())
938                         rev_file_cache_ = "?";
939
940         switch (info) {
941                 case LyXVC::File:
942                         if (rev_file_cache_ == "?")
943                                 return string();
944                         return rev_file_cache_;
945                 case LyXVC::Author:
946                         return rev_author_cache_;
947                 case LyXVC::Date:
948                         return rev_date_cache_;
949                 case LyXVC::Time:
950                         return rev_time_cache_;
951                 default: ;
952
953         }
954
955         return string();
956 }
957
958
959 bool SVN::getFileRevisionInfo()
960 {
961         FileName tmpf = FileName::tempName("lyxvcout");
962         if (tmpf.empty()) {
963                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
964                 return N_("Error: Could not generate logfile.");
965         }
966
967         doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
968                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
969                     FileName(owner_->filePath()));
970
971         if (tmpf.empty())
972                 return false;
973
974         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
975         string line;
976         // commit log part
977         bool c = false;
978         string rev;
979
980         while (ifs) {
981                 getline(ifs, line);
982                 LYXERR(Debug::LYXVC, line);
983                 if (prefixIs(line, "<commit"))
984                         c = true;
985                 if (c && prefixIs(line, "   revision=\"") && suffixIs(line, "\">")) {
986                         string l1 = subst(line, "revision=\"", "");
987                         string l2 = trim(subst(l1, "\">", ""));
988                         if (isStrInt(l2))
989                                 rev_file_cache_ = rev = l2;
990                 }
991                 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
992                         string l1 = subst(line, "<author>", "");
993                         string l2 = subst(l1, "</author>", "");
994                         rev_author_cache_ = l2;
995                 }
996                 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
997                         string l1 = subst(line, "<date>", "");
998                         string l2 = subst(l1, "</date>", "");
999                         l2 = split(l2, l1, 'T');
1000                         rev_date_cache_ = l1;
1001                         l2 = split(l2, l1, '.');
1002                         rev_time_cache_ = l1;
1003                 }
1004         }
1005
1006         ifs.close();
1007         tmpf.erase();
1008         return !rev.empty();
1009 }
1010
1011
1012 bool SVN::getTreeRevisionInfo()
1013 {
1014         FileName tmpf = FileName::tempName("lyxvcout");
1015         if (tmpf.empty()) {
1016                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1017                 return N_("Error: Could not generate logfile.");
1018         }
1019
1020         doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1021                     FileName(owner_->filePath()));
1022
1023         if (tmpf.empty())
1024                 return false;
1025
1026         // only first line in case something bad happens.
1027         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1028         string line;
1029         getline(ifs, line);
1030         ifs.close();
1031         tmpf.erase();
1032
1033         rev_tree_cache_ = line;
1034         return !line.empty();
1035 }
1036
1037
1038 void SVN::getLog(FileName const & tmpf)
1039 {
1040         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
1041                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1042                     FileName(owner_->filePath()));
1043 }
1044
1045
1046 bool SVN::prepareFileRevision(string const & revis, string & f)
1047 {
1048         if (!isStrInt(revis))
1049                 return false;
1050
1051         int rev = convert<int>(revis);
1052         if (rev <= 0)
1053                 if (!getFileRevisionInfo())
1054                         return false;
1055         if (rev == 0)
1056                 rev = convert<int>(rev_file_cache_);
1057         // go back for minus rev
1058         else if (rev < 0) {
1059                 rev = rev + convert<int>(rev_file_cache_);
1060                 if (rev < 1)
1061                         return false;
1062         }
1063
1064         FileName tmpf = FileName::tempName("lyxvcrev");
1065         if (tmpf.empty()) {
1066                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1067                 return N_("Error: Could not generate logfile.");
1068         }
1069
1070         doVCCommand("svn cat -r " + convert<string>(rev) + " "
1071                       + quoteName(onlyFilename(owner_->absFileName()))
1072                       + " > " + quoteName(tmpf.toFilesystemEncoding()),
1073                 FileName(owner_->filePath()));
1074         if (tmpf.isFileEmpty())
1075                 return false;
1076
1077         f = tmpf.absFilename();
1078         return true;
1079 }
1080
1081
1082 bool SVN::prepareFileRevisionEnabled()
1083 {
1084         return true;
1085 }
1086
1087
1088
1089 bool SVN::toggleReadOnlyEnabled()
1090 {
1091         return false;
1092 }
1093
1094
1095 } // namespace lyx