/
SessionSVN.cpp
867 lines (720 loc) · 21.4 KB
/
SessionSVN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/*
* SessionSVN.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#include "SessionSVN.hpp"
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#endif
#include <boost/bind.hpp>
#include <boost/regex.hpp>
#include <core/rapidxml/rapidxml.hpp>
#include <core/system/Environment.hpp>
#include <core/system/Process.hpp>
#include <core/system/ShellUtils.hpp>
#include <core/Exec.hpp>
#include <core/http/Header.hpp>
#include <session/projects/SessionProjects.hpp>
#include <session/SessionModuleContext.hpp>
#include <session/SessionOptions.hpp>
#include <session/SessionUserSettings.hpp>
#include <r/RExec.hpp>
#include "vcs/SessionVCSUtils.hpp"
#include "SessionConsoleProcess.hpp"
using namespace core;
using namespace core::shell_utils;
using namespace session::modules::vcs_utils;
using namespace session::modules::console_process;
namespace session {
namespace modules {
namespace svn {
namespace {
// svn exe which we detect at startup. note that if the svn exe
// is already in the path then this will be empty
std::string s_svnExePath;
/** GLOBAL STATE **/
FilePath s_workingDir;
FilePath resolveAliasedPath(const std::string& path)
{
if (boost::algorithm::starts_with(path, "~/"))
return module_context::resolveAliasedPath(path);
else
return s_workingDir.childPath(path);
}
std::vector<FilePath> resolveAliasedPaths(const json::Array& paths,
bool includeRenameOld = false,
bool includeRenameNew = true)
{
std::vector<FilePath> results;
for (json::Array::const_iterator it = paths.begin();
it != paths.end();
it++)
{
results.push_back(resolveAliasedPath(it->get_str()));
}
return results;
}
core::system::ProcessOptions procOptions()
{
core::system::ProcessOptions options;
// detach the session so there is no terminal
#ifndef _WIN32
options.detachSession = true;
#endif
// get current environment for modification prior to passing to child
core::system::Options childEnv;
core::system::environment(&childEnv);
// add postback directory to PATH
FilePath postbackDir = session::options().rpostbackPath().parent();
core::system::addToPath(&childEnv, postbackDir.absolutePath());
if (!s_workingDir.empty())
options.workingDir = s_workingDir;
else
options.workingDir = projects::projectContext().directory();
// on windows set HOME to USERPROFILE
#ifdef _WIN32
std::string userProfile = core::system::getenv(childEnv, "USERPROFILE");
core::system::setenv(&childEnv, "HOME", userProfile);
#endif
// set custom environment
options.environment = childEnv;
return options;
}
#ifndef _WIN32
ShellCommand svn()
{
if (!s_svnExePath.empty())
{
FilePath exePath(s_svnExePath);
return ShellCommand(exePath);
}
else
return ShellCommand("svn");
}
#endif
#ifdef _WIN32
std::string svnBin()
{
if (!s_svnExePath.empty())
{
return FilePath(s_svnExePath).absolutePathNative();
}
else
return "svn.exe";
}
#endif
Error runSvn(const ShellArgs& args,
core::system::ProcessResult* pResult,
bool redirectStdErrToStdOut=false)
{
core::system::ProcessOptions options = procOptions();
options.redirectStdErrToStdOut = redirectStdErrToStdOut;
#ifdef _WIN32
options.detachProcess = true;
#endif
#ifdef _WIN32
Error error = core::system::runProgram(svnBin(),
args.args(),
std::string(),
options,
pResult);
#else
Error error = core::system::runCommand(svn() << args.args(),
options,
pResult);
#endif
return error;
}
Error runSvn(const ShellArgs& args,
std::string* pStdOut=NULL,
std::string* pStdErr=NULL,
int* pExitCode=NULL)
{
core::system::ProcessResult result;
Error error = runSvn(args, &result);
if (error)
return error;
if (pStdOut)
*pStdOut = result.stdOut;
if (pStdErr)
*pStdErr = result.stdErr;
if (pExitCode)
*pExitCode = result.exitStatus;
return Success();
}
std::vector<std::string> globalArgs(
const std::string* const pUsername=NULL,
const std::string* const pPassword=NULL,
bool cacheCredentials=false)
{
std::vector<std::string> args;
args.push_back("--non-interactive");
//args.push_back("--trust-server-cert");
if (pUsername)
{
args.push_back("--username");
args.push_back(*pUsername);
}
if (pPassword)
{
args.push_back("--password");
args.push_back(*pPassword);
}
if (!cacheCredentials)
args.push_back("--no-auth-cache");
return args;
}
core::Error createConsoleProc(const ShellArgs& args,
const std::string& caption,
bool dialog,
boost::shared_ptr<ConsoleProcess>* ppCP,
const boost::optional<FilePath>& workingDir=boost::optional<FilePath>())
{
core::system::ProcessOptions options = procOptions();
#ifdef _WIN32
options.detachProcess = true;
#endif
if (!workingDir)
options.workingDir = s_workingDir;
else if (!workingDir.get().empty())
options.workingDir = workingDir.get();
#ifdef _WIN32
*ppCP = ConsoleProcess::create(svnBin(),
args.args(),
options,
caption,
dialog,
console_process::InteractionPossible,
console_process::kDefaultMaxOutputLines,
&enqueueRefreshEvent);
#else
*ppCP = ConsoleProcess::create(svn() << args.args(),
options,
caption,
dialog,
console_process::InteractionPossible,
console_process::kDefaultMaxOutputLines,
&enqueueRefreshEvent);
#endif
return Success();
}
#ifdef _WIN32
bool detectSvnExeOnPath(FilePath* pPath)
{
std::vector<wchar_t> path(MAX_PATH+2);
wcscpy(&(path[0]), L"svn.exe");
if (::PathFindOnPathW(&(path[0]), NULL))
{
*pPath = FilePath(&(path[0]));
return true;
}
else
{
return false;
}
}
#endif
FilePath whichSvnExe()
{
std::string whichSvn;
Error error = r::exec::RFunction("Sys.which", "svn").call(&whichSvn);
if (error)
{
LOG_ERROR(error);
return FilePath();
}
else
{
return FilePath(whichSvn);
}
}
bool initSvnBin()
{
// get the svn bin dir from user settings if it is there
s_svnExePath = userSettings().svnExePath().absolutePath();
// if it wasn't provided in settings then make sure we can detect it
if (s_svnExePath.empty())
return !svn::detectedSvnExePath().empty();
else
return true;
}
} // namespace
bool isSvnInstalled()
{
int exitCode;
Error error = runSvn(ShellArgs() << "help", NULL, NULL, &exitCode);
if (error)
{
LOG_ERROR(error);
return false;
}
return exitCode == EXIT_SUCCESS;
}
struct SvnInfo
{
bool empty() const { return repositoryRoot.empty(); }
std::string repositoryRoot;
};
// NOTE: this is a separate run code path than run svn above because
// want to specify the working directory explicitly
Error runSvnInfo(const core::FilePath& workingDir, SvnInfo* pSvnInfo)
{
if (workingDir.empty())
return Success();
core::system::ProcessOptions options = procOptions();
options.workingDir = workingDir;
core::system::ProcessResult result;
#ifdef _WIN32
Error error = core::system::runProgram(svnBin(),
ShellArgs() << "info",
"",
options,
&result);
#else
Error error = core::system::runCommand(svn() << "info",
"",
options,
&result);
#endif
if (error)
return error;
if (result.exitStatus == EXIT_SUCCESS)
{
// break the output into lines
boost::char_separator<char> lineSep("\n");
boost::tokenizer<boost::char_separator<char> > lines(result.stdOut,
lineSep);
for (boost::tokenizer<boost::char_separator<char> >::iterator
lineIter = lines.begin();
lineIter != lines.end();
++lineIter)
{
std::string line = *lineIter;
if (boost::algorithm::starts_with(line, "Repository Root:"))
{
http::Header header;
http::parseHeader(line, &header);
pSvnInfo->repositoryRoot = header.value;
break;
}
}
}
return Success();
}
bool isSvnDirectory(const core::FilePath& workingDir)
{
return !repositoryRoot(workingDir).empty();
}
std::string repositoryRoot(const FilePath& workingDir)
{
SvnInfo svnInfo;
Error error = runSvnInfo(workingDir, &svnInfo);
if (error)
{
LOG_ERROR(error);
return std::string();
}
return svnInfo.repositoryRoot;
}
bool isSvnEnabled()
{
return !s_workingDir.empty();
}
FilePath detectedSvnExePath()
{
#ifdef _WIN32
FilePath path;
if (detectSvnExeOnPath(&path))
{
return path;
}
else
{
return FilePath();
}
#else
FilePath svnExeFilePath = whichSvnExe();
if (!svnExeFilePath.empty())
return FilePath(svnExeFilePath);
else
return FilePath();
#endif
}
std::string nonPathSvnBinDir()
{
if (!s_svnExePath.empty())
return FilePath(s_svnExePath).parent().absolutePath();
else
return std::string();
}
void onUserSettingsChanged()
{
initSvnBin();
}
std::string translateItemStatus(const std::string& status)
{
if (status == "added")
return "A";
if (status == "conflicted")
return "C";
if (status == "deleted")
return "D";
if (status == "external")
return "X";
if (status == "ignored")
return "I";
if (status == "incomplete")
return "!";
if (status == "merged") // ??
return "G";
if (status == "missing")
return "!";
if (status == "modified")
return "M";
if (status == "none")
return " ";
if (status == "normal") // ??
return " ";
if (status == "obstructed")
return "~";
if (status == "replaced")
return "~";
if (status == "unversioned")
return "?";
return " ";
}
int rankItemStatus(const std::string& status)
{
if (status == " " || status.empty())
return 10;
if (status == "I")
return 7;
if (status == "M")
return 1;
if (status == "C")
return 0;
return 5;
}
std::string topStatus(const std::string& a, const std::string& b)
{
if (rankItemStatus(a) <= rankItemStatus(b))
return a;
return b;
}
#define FOREACH_NODE(parent, varname, name) \
for (rapidxml::xml_node<>* varname = parent->first_node(name); \
varname; \
varname = varname->next_sibling(name))
std::string attr_value(rapidxml::xml_node<>* pNode, const std::string& attrName)
{
if (!pNode)
return std::string();
rapidxml::xml_attribute<>* pAttr = pNode->first_attribute(attrName.c_str());
if (!pAttr)
return std::string();
return std::string(pAttr->value());
}
FilePath resolveAliasedJsonPath(const json::Value& value)
{
return module_context::resolveAliasedPath(value.get_str());
}
Error svnAdd(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
RefreshOnExit refreshOnExit;
json::Array files;
Error error = json::readParams(request.params, &files);
if (error)
return error;
std::vector<FilePath> paths;
std::transform(files.begin(), files.end(), std::back_inserter(paths),
&resolveAliasedJsonPath);
core::system::ProcessResult result;
error = runSvn(ShellArgs() << "add" << globalArgs() << "-q" << "--" << paths,
&result, true);
if (error)
return error;
pResponse->setResult(processResultToJson(result));
return Success();
}
Error svnDelete(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
RefreshOnExit refreshOnExit;
json::Array files;
Error error = json::readParams(request.params, &files);
if (error)
return error;
std::vector<FilePath> paths;
std::transform(files.begin(), files.end(), std::back_inserter(paths),
&resolveAliasedJsonPath);
core::system::ProcessResult result;
error = runSvn(ShellArgs() << "delete" << globalArgs() << "-q" << "--" << paths,
&result, true);
if (error)
return error;
pResponse->setResult(processResultToJson(result));
return Success();
}
Error svnRevert(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
RefreshOnExit refreshOnExit;
json::Array files;
Error error = json::readParams(request.params, &files);
if (error)
return error;
std::vector<FilePath> paths;
std::transform(files.begin(), files.end(), std::back_inserter(paths),
&resolveAliasedJsonPath);
core::system::ProcessResult result;
error = runSvn(ShellArgs() << "revert" << globalArgs() << "-q" <<
"--depth" << "infinity" <<
"--" << paths,
&result, true);
if (error)
return error;
pResponse->setResult(processResultToJson(result));
return Success();
}
Error svnStatus(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string stdOut, stdErr;
int exitCode;
Error error = runSvn(
ShellArgs() << "status" << globalArgs() << "--xml" << "--ignore-externals",
&stdOut,
&stdErr,
&exitCode);
if (error)
return error;
if (exitCode != EXIT_SUCCESS)
{
LOG_ERROR_MESSAGE(stdErr);
return Success();
}
std::vector<char> xmlData;
xmlData.reserve(stdOut.size() + 1);
std::copy(stdOut.begin(),
stdOut.end(),
std::back_inserter(xmlData));
xmlData.push_back('\0'); // null terminator
using namespace rapidxml;
xml_document<> doc;
doc.parse<0>(&(xmlData[0]));
const std::string CHANGELIST_NAME("changelist");
json::Array results;
xml_node<>* pStatus = doc.first_node("status");
if (pStatus)
{
FOREACH_NODE(pStatus, pList,)
{
std::string changelist;
if (pList->name() == CHANGELIST_NAME)
{
changelist = attr_value(pList, "name");
}
FOREACH_NODE(pList, pEntry, "entry")
{
std::string path = attr_value(pEntry, "path");
if (path.empty())
{
LOG_ERROR_MESSAGE("Path attribute not found");
continue;
}
xml_node<>* pStatus = pEntry->first_node("wc-status");
if (!pStatus)
{
LOG_ERROR_MESSAGE("Status node not found");
continue;
}
std::string item = attr_value(pStatus, "item");
if (item.empty())
{
LOG_ERROR_MESSAGE("Item attribute not found");
continue;
}
item = translateItemStatus(item);
std::string props = attr_value(pStatus, "props");
if (props.empty())
{
LOG_ERROR_MESSAGE("Item properties not found");
continue;
}
props = translateItemStatus(props);
std::string status = topStatus(item, props);
json::Object info;
info["status"] = status;
// TODO: escape path relative to <target>
info["path"] = path;
info["raw_path"] = module_context::createAliasedPath(
projects::projectContext().directory().childPath(path));
info["changelist"] = changelist;
results.push_back(info);
}
}
}
pResponse->setResult(results);
return Success();
}
Error svnUpdate(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
boost::shared_ptr<ConsoleProcess> pCP;
Error error = createConsoleProc(ShellArgs() << "update" << globalArgs(),
"SVN Update",
true,
&pCP);
if (error)
return error;
// TODO: Authentication if necessary
// TODO: Set askpass handle?? Is that even necessary with SVN?
pResponse->setResult(pCP->toJson());
return Success();
}
Error svnCommit(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
json::Array paths;
std::string message;
Error error = json::readParams(request.params, &paths, &message);
if (error)
return error;
FilePath tempFile = module_context::tempFile("svnmsg", "txt");
boost::shared_ptr<std::ostream> pStream;
error = tempFile.open_w(&pStream);
if (error)
return error;
*pStream << message;
pStream->flush();
pStream.reset(); // release file handle
ShellArgs args;
args << "commit" << globalArgs();
args << "-F" << tempFile;
args << "--";
if (!paths.empty())
args << resolveAliasedPaths(paths);
boost::shared_ptr<ConsoleProcess> pCP;
error = createConsoleProc(args,
"SVN Commit",
true,
&pCP);
if (error)
return error;
// TODO: Authentication if necessary
// TODO: Set askpass handle?? Is that even necessary with SVN?
pResponse->setResult(pCP->toJson());
return Success();
}
Error svnDiffFile(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string path;
int contextLines;
bool noSizeWarning;
Error error = json::readParams(request.params,
&path,
&contextLines,
&noSizeWarning);
if (error)
return error;
FilePath filePath = resolveAliasedPath(path);
if (contextLines < 0)
contextLines = 999999999;
std::string extArgs = "-U " + boost::lexical_cast<std::string>(contextLines);
std::string stdOut, stdErr;
int exitCode;
error = runSvn(ShellArgs() << "diff" <<
"--depth" << "empty" <<
"--diff-cmd" << "diff" <<
"-x" << extArgs <<
"--" << filePath,
&stdOut, &stdErr, &exitCode);
if (error)
return error;
if (exitCode != EXIT_SUCCESS)
{
LOG_ERROR_MESSAGE(stdErr);
}
// TODO: implement size warning
pResponse->setResult(stdOut);
return Success();
}
Error svnApplyPatch(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
RefreshOnExit refreshOnExit;
std::string path, patch;
Error error = json::readParams(request.params,
&path,
&patch);
if (error)
return error;
FilePath filePath = resolveAliasedPath(path);
FilePath tempFile = module_context::tempFile("svnpatch", "txt");
boost::shared_ptr<std::ostream> pStream;
error = tempFile.open_w(&pStream);
if (error)
return error;
*pStream << patch;
pStream->flush();
pStream.reset(); // release file handle
ShellCommand cmd("patch");
cmd << "-i" << tempFile;
cmd << filePath;
core::system::ProcessOptions options = procOptions();
core::system::ProcessResult result;
error = core::system::runCommand(cmd,
options,
&result);
if (error)
return error;
if (result.exitStatus != EXIT_SUCCESS)
{
LOG_ERROR_MESSAGE(result.stdErr);
}
return Success();
}
Error initialize()
{
// install rpc methods
using boost::bind;
using namespace module_context;
ExecBlock initBlock ;
initBlock.addFunctions()
(bind(registerRpcMethod, "svn_add", svnAdd))
(bind(registerRpcMethod, "svn_delete", svnDelete))
(bind(registerRpcMethod, "svn_revert", svnRevert))
(bind(registerRpcMethod, "svn_status", svnStatus))
(bind(registerRpcMethod, "svn_update", svnUpdate))
(bind(registerRpcMethod, "svn_commit", svnCommit))
(bind(registerRpcMethod, "svn_diff_file", svnDiffFile))
(bind(registerRpcMethod, "svn_apply_patch", svnApplyPatch))
;
Error error = initBlock.execute();
if (error)
return error;
return Success();
}
Error initializeSvn(const core::FilePath& workingDir)
{
s_workingDir = workingDir;
// set s_svnExePath if it is provied in userSettings()
initSvnBin();
userSettings().onChanged.connect(onUserSettingsChanged);
return Success();
}
} // namespace svn
} // namespace modules
} //namespace session