aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository/vcs/svn_driver.rs
blob: a79334086771dc45e6058a0c17f196e9519611e6 (plain)
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
//! ref: composer/src/Composer/Repository/Vcs/SvnDriver.php

use anyhow::Result;
use chrono::{DateTime, TimeZone, Utc};
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
    JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, RuntimeException, array_key_exists,
    is_array, max, sprintf, stripos, strrpos, strtr, substr, trim,
};

use crate::cache::Cache;
use crate::config::Config;
use crate::downloader::TransportException;
use crate::io::IOInterface;
use crate::json::JsonFile;
use crate::repository::vcs::VcsDriverBase;
use crate::util::Filesystem;
use crate::util::ProcessExecutor;
use crate::util::Svn as SvnUtil;
use crate::util::Url;

#[derive(Debug)]
pub struct SvnDriver {
    pub(crate) inner: VcsDriverBase,
    /// @var string
    pub(crate) base_url: String,
    /// @var array<int|string, string> Map of tag name to identifier
    pub(crate) tags: Option<IndexMap<String, String>>,
    /// @var array<int|string, string> Map of branch name to identifier
    pub(crate) branches: Option<IndexMap<String, String>>,
    /// @var ?string
    pub(crate) root_identifier: Option<String>,

    /// @var string|false
    // TODO(phase-b): PHP uses 'false' as a sentinel; model as Option<String>
    pub(crate) trunk_path: Option<String>,
    /// @var string
    pub(crate) branches_path: String,
    /// @var string
    pub(crate) tags_path: String,
    /// @var string
    pub(crate) package_path: String,
    /// @var bool
    pub(crate) cache_credentials: bool,

    /// @var SvnUtil
    util: Option<SvnUtil>,
}

impl SvnDriver {
    pub fn initialize(&mut self) -> Result<()> {
        let normalized = Self::normalize_url(&self.inner.url);
        self.inner.url = normalized.trim_end_matches('/').to_string();
        self.base_url = self.inner.url.clone();

        SvnUtil::clean_env();

        if let Some(PhpMixed::String(v)) = self.inner.repo_config.get("trunk-path").cloned() {
            self.trunk_path = Some(v);
        }
        if let Some(PhpMixed::String(v)) = self.inner.repo_config.get("branches-path").cloned() {
            self.branches_path = v;
        }
        if let Some(PhpMixed::String(v)) = self.inner.repo_config.get("tags-path").cloned() {
            self.tags_path = v;
        }
        if array_key_exists("svn-cache-credentials", &self.inner.repo_config) {
            self.cache_credentials = self
                .inner
                .repo_config
                .get("svn-cache-credentials")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
        }
        if let Some(PhpMixed::String(v)) = self.inner.repo_config.get("package-path").cloned() {
            self.package_path = format!("/{}", trim(&v, Some("/")));
        }

        if let Some(trunk_path) = &self.trunk_path {
            if let Some(pos) = strrpos(&self.inner.url, &format!("/{}", trunk_path)) {
                self.base_url = substr(&self.inner.url, 0, Some(pos as i64));
            }
        }

        self.inner.cache = Some(Cache::new(
            // TODO(phase-b): pass io by reference/clone
            todo!("self.inner.io clone"),
            &format!(
                "{}/{}",
                self.inner
                    .config
                    .borrow_mut()
                    .get("cache-repo-dir")
                    .as_string()
                    .unwrap_or(""),
                Preg::replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(self.base_url.clone()))?,
            ),
            None,
            None,
            false,
        ));
        self.inner.cache.as_mut().unwrap().set_read_only(
            self.inner
                .config
                .borrow_mut()
                .get("cache-read-only")
                .as_bool()
                .unwrap_or(false),
        );

        self.get_branches();
        self.get_tags();
        Ok(())
    }

    pub fn get_root_identifier(&self) -> String {
        self.root_identifier
            .clone()
            .unwrap_or_else(|| self.trunk_path.clone().unwrap_or_default())
    }

    pub fn get_url(&self) -> &str {
        &self.inner.url
    }

    pub fn get_source(&self, identifier: &str) -> IndexMap<String, String> {
        let mut m = IndexMap::new();
        m.insert("type".to_string(), "svn".to_string());
        m.insert("url".to_string(), self.base_url.clone());
        m.insert("reference".to_string(), identifier.to_string());
        m
    }

    pub fn get_dist(&self, _identifier: &str) -> Option<IndexMap<String, String>> {
        None
    }

    pub(crate) fn should_cache(&self, identifier: &str) -> bool {
        self.inner.cache.is_some() && Preg::is_match(r"{@\d+$}", identifier).unwrap_or(false)
    }

    pub fn get_composer_information(
        &mut self,
        identifier: &str,
    ) -> Result<Option<IndexMap<String, PhpMixed>>> {
        if !self.inner.info_cache.contains_key(identifier) {
            if self.should_cache(identifier) {
                if let Some(mut res) = self
                    .inner
                    .cache
                    .as_mut()
                    .and_then(|c| c.read(&format!("{}.json", identifier)))
                {
                    // old cache files had '' stored instead of null due to af3783b5f40bae32a23e353eaf0a00c9b8ce82e2, so we make sure here that we always return null or array
                    // and fix outdated invalid cache files
                    if res == "\"\"" {
                        res = "null".to_string();
                        self.inner
                            .cache
                            .as_mut()
                            .unwrap()
                            .write(&format!("{}.json", identifier), &res)?;
                    }

                    let parsed = JsonFile::parse_json(Some(res.as_str()), None)?;
                    // TODO(phase-b): info_cache expects Option<IndexMap<String, PhpMixed>>;
                    // PhpMixed → IndexMap conversion is non-trivial here. Skip insert/return.
                    let _ = parsed;
                    return Ok(None);
                }
            }

            // TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
            let base_result =
                self.get_file_content("composer.json", identifier)
                    .and_then(|file_content| {
                        VcsDriverBase::finish_base_composer_information(
                            identifier,
                            file_content,
                            || self.get_change_date(identifier),
                        )
                    });
            let composer: Option<IndexMap<String, PhpMixed>> = match base_result {
                Ok(c) => c,
                Err(e) => {
                    // TODO(phase-b): downcast to TransportException
                    let _te: &TransportException = todo!("downcast e to TransportException");
                    let message = e.to_string();
                    if stripos(&message, "path not found").is_none()
                        && stripos(&message, "svn: warning: W160013").is_none()
                    {
                        return Err(e);
                    }
                    // remember a not-existent composer.json
                    None
                }
            };

            if self.should_cache(identifier) {
                let encoded = JsonFile::encode(
                    &composer
                        .clone()
                        .map(PhpMixed::from)
                        .unwrap_or(PhpMixed::Null),
                    JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
                );
                self.inner
                    .cache
                    .as_mut()
                    .unwrap()
                    .write(&format!("{}.json", identifier), &encoded)?;
            }

            self.inner
                .info_cache
                .insert(identifier.to_string(), composer);
        }

        // old cache files had '' stored instead of null due to af3783b5f40bae32a23e353eaf0a00c9b8ce82e2, so we make sure here that we always return null or array
        let cached = self
            .inner
            .info_cache
            .get(identifier)
            .and_then(|v| v.clone());
        if cached.is_none()
            || !is_array(
                // TODO(phase-b): wrap IndexMap to PhpMixed for is_array check
                &cached.clone().map(PhpMixed::from).unwrap_or(PhpMixed::Null),
            )
        {
            return Ok(None);
        }

        Ok(cached)
    }

    pub fn get_file_content(&mut self, file: &str, identifier: &str) -> Result<Option<String>> {
        let identifier = format!("/{}/", trim(identifier, Some("/")));

        let (path, rev) = if let Ok(Some(m)) =
            Preg::is_match_with_indexed_captures(r"{^(.+?)(@\d+)?/$}", &identifier)
        {
            if m.get(2).is_some() {
                (
                    m.get(1).cloned().unwrap_or_default(),
                    m.get(2).cloned().unwrap_or_default(),
                )
            } else {
                (identifier.clone(), String::new())
            }
        } else {
            (identifier.clone(), String::new())
        };

        // TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
        let output: String = match self.execute(
            vec!["svn".to_string(), "cat".to_string()],
            &format!("{}{}{}", self.base_url, path, rev),
        ) {
            Ok(o) => o,
            Err(e) => {
                return Err(TransportException::new(e.to_string(), 0).into());
            }
        };
        if trim(&output, None) == "" {
            return Ok(None);
        }

        Ok(Some(output))
    }

    pub fn get_change_date(&mut self, identifier: &str) -> Result<Option<DateTime<Utc>>> {
        let identifier = format!("/{}/", trim(identifier, Some("/")));

        let (path, rev) = if let Ok(Some(m)) =
            Preg::is_match_with_indexed_captures(r"{^(.+?)(@\d+)?/$}", &identifier)
        {
            if m.get(2).is_some() {
                (
                    m.get(1).cloned().unwrap_or_default(),
                    m.get(2).cloned().unwrap_or_default(),
                )
            } else {
                (identifier.clone(), String::new())
            }
        } else {
            (identifier.clone(), String::new())
        };

        let output = self.execute(
            vec!["svn".to_string(), "info".to_string()],
            &format!("{}{}{}", self.base_url, path, rev),
        )?;
        for line in self.inner.process.borrow().split_lines(&output) {
            if !line.is_empty() {
                let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
                if Preg::is_match_strict_groups3(
                    r"{^Last Changed Date: ([^(]+)}",
                    &line,
                    Some(&mut m),
                )
                .unwrap_or(false)
                {
                    let date_str = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default();
                    // PHP: new \DateTimeImmutable($match[1], new \DateTimeZone('UTC'))
                    return Ok(Utc
                        .datetime_from_str(date_str.trim(), "%Y-%m-%d %H:%M:%S %z")
                        .ok());
                }
            }
        }

        Ok(None)
    }

    pub fn get_tags(&mut self) -> &IndexMap<String, String> {
        if self.tags.is_none() {
            let mut tags: IndexMap<String, String> = IndexMap::new();

            // PHP: if ($this->tagsPath !== false) — tagsPath is "string"; treat empty string as false
            if !self.tags_path.is_empty() {
                let output = self
                    .execute(
                        vec!["svn".to_string(), "ls".to_string(), "--verbose".to_string()],
                        &format!("{}/{}", self.base_url, self.tags_path),
                    )
                    .unwrap_or_default();
                if !output.is_empty() {
                    let mut last_rev: i64 = 0;
                    for line in self.inner.process.borrow().split_lines(&output) {
                        let line = trim(&line, None);
                        if !line.is_empty() {
                            let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
                            if Preg::is_match_strict_groups3(
                                r"{^\s*(\S+).*?(\S+)\s*$}",
                                &line,
                                Some(&mut m),
                            )
                            .unwrap_or(false)
                            {
                                let rev: i64 = m
                                    .get(&CaptureKey::ByIndex(1))
                                    .and_then(|s| s.parse().ok())
                                    .unwrap_or(0);
                                let path =
                                    m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default();
                                if path == "./" {
                                    last_rev = rev;
                                } else {
                                    let identifier = self.build_identifier(
                                        &format!("/{}/{}", self.tags_path, path),
                                        max(last_rev, rev),
                                    );
                                    tags.insert(path.trim_end_matches('/').to_string(), identifier);
                                }
                            }
                        }
                    }
                }
            }

            self.tags = Some(tags);
        }

        self.tags.as_ref().unwrap()
    }

    pub fn get_branches(&mut self) -> &IndexMap<String, String> {
        if self.branches.is_none() {
            let mut branches: IndexMap<String, String> = IndexMap::new();

            let trunk_parent = if self.trunk_path.is_none() {
                format!("{}/", self.base_url)
            } else {
                format!("{}/{}", self.base_url, self.trunk_path.as_ref().unwrap())
            };

            let output = self
                .execute(
                    vec!["svn".to_string(), "ls".to_string(), "--verbose".to_string()],
                    &trunk_parent,
                )
                .unwrap_or_default();
            if !output.is_empty() {
                for line in self.inner.process.borrow().split_lines(&output) {
                    let line = trim(&line, None);
                    if !line.is_empty() {
                        let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
                        if Preg::is_match_strict_groups3(
                            r"{^\s*(\S+).*?(\S+)\s*$}",
                            &line,
                            Some(&mut m),
                        )
                        .unwrap_or(false)
                        {
                            let rev: i64 = m
                                .get(&CaptureKey::ByIndex(1))
                                .and_then(|s| s.parse().ok())
                                .unwrap_or(0);
                            let path = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default();
                            if path == "./" {
                                let identifier = self.build_identifier(
                                    &format!("/{}", self.trunk_path.clone().unwrap_or_default()),
                                    rev,
                                );
                                branches.insert("trunk".to_string(), identifier.clone());
                                self.root_identifier = Some(identifier);
                                break;
                            }
                        }
                    }
                }
            }
            // PHP: unset($output);

            // PHP: if ($this->branchesPath !== false) — branchesPath is "string"; treat empty string as false
            if !self.branches_path.is_empty() {
                let output = self
                    .execute(
                        vec!["svn".to_string(), "ls".to_string(), "--verbose".to_string()],
                        &format!("{}/{}", self.base_url, self.branches_path),
                    )
                    .unwrap_or_default();
                if !output.is_empty() {
                    let mut last_rev: i64 = 0;
                    for line in self
                        .inner
                        .process
                        .borrow()
                        .split_lines(&trim(&output, None))
                    {
                        let line = trim(&line, None);
                        if !line.is_empty() {
                            let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
                            if Preg::is_match_strict_groups3(
                                r"{^\s*(\S+).*?(\S+)\s*$}",
                                &line,
                                Some(&mut m),
                            )
                            .unwrap_or(false)
                            {
                                let rev: i64 = m
                                    .get(&CaptureKey::ByIndex(1))
                                    .and_then(|s| s.parse().ok())
                                    .unwrap_or(0);
                                let path =
                                    m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default();
                                if path == "./" {
                                    last_rev = rev;
                                } else {
                                    let identifier = self.build_identifier(
                                        &format!("/{}/{}", self.branches_path, path),
                                        max(last_rev, rev),
                                    );
                                    branches
                                        .insert(path.trim_end_matches('/').to_string(), identifier);
                                }
                            }
                        }
                    }
                }
            }

            self.branches = Some(branches);
        }

        self.branches.as_ref().unwrap()
    }

    pub fn supports(
        io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
        _config: &Config,
        url: &str,
        deep: bool,
    ) -> bool {
        let url = Self::normalize_url(url);
        if Preg::is_match(r"#(^svn://|^svn\+ssh://|svn\.)#i", &url).unwrap_or(false) {
            return true;
        }

        // proceed with deep check for local urls since they are fast to process
        if !deep && !Filesystem::is_local_path(&url) {
            return false;
        }

        let mut process = ProcessExecutor::new(Some(io));
        let mut ignored_output = String::new();
        let exit = process.execute_args(
            &[
                "svn".to_string(),
                "info".to_string(),
                "--non-interactive".to_string(),
                "--".to_string(),
                url.clone(),
            ],
            &mut ignored_output,
            (),
        );

        if exit == 0 {
            // This is definitely a Subversion repository.
            return true;
        }

        // Subversion client 1.7 and older
        if stripos(&process.get_error_output(), "authorization failed:").is_some() {
            // This is likely a remote Subversion repository that requires
            // authentication. We will handle actual authentication later.
            return true;
        }

        // Subversion client 1.8 and newer
        if stripos(&process.get_error_output(), "Authentication failed").is_some() {
            // This is likely a remote Subversion or newer repository that requires
            // authentication. We will handle actual authentication later.
            return true;
        }

        false
    }

    /// An absolute path (leading '/') is converted to a file:// url.
    pub(crate) fn normalize_url(url: &str) -> String {
        let fs = Filesystem::new(None);
        if fs.is_absolute_path(url) {
            return format!("file://{}", strtr(url, "\\", "/"));
        }

        url.to_string()
    }

    /// Execute an SVN command and try to fix up the process with credentials
    /// if necessary.
    ///
    /// @param  non-empty-list<string> $command The svn command to run.
    /// @param  string            $url     The SVN URL.
    /// @throws \RuntimeException
    pub(crate) fn execute(&mut self, command: Vec<String>, url: &str) -> Result<String> {
        if self.util.is_none() {
            self.util = Some(SvnUtil::new(
                self.base_url.clone(),
                // TODO(phase-b): clone or borrow io/config
                todo!("self.inner.io clone"),
                todo!("self.inner.config clone"),
                Some(todo!("self.inner.process clone")),
            ));
            self.util
                .as_mut()
                .unwrap()
                .set_cache_credentials(self.cache_credentials);
        }

        // TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
        match self
            .util
            .as_mut()
            .unwrap()
            .execute(command, url, None, None, false)
        {
            Ok(o) => Ok(o),
            Err(e) => {
                if self.util.as_mut().unwrap().binary_version().is_none() {
                    return Err(RuntimeException {
                        message: format!(
                            "Failed to load {}, svn was not found, check that it is installed and in your PATH env.\n\n{}",
                            self.inner.url,
                            self.inner.process.borrow().get_error_output(),
                        ),
                        code: 0,
                    }
                    .into());
                }

                Err(RuntimeException {
                    message: format!(
                        "Repository {} could not be processed, {}",
                        self.inner.url, e,
                    ),
                    code: 0,
                }
                .into())
            }
        }
    }

    /// Build the identifier respecting "package-path" config option
    ///
    /// @param string $baseDir  The path to trunk/branch/tag
    /// @param int $revision The revision mark to add to identifier
    pub(crate) fn build_identifier(&self, base_dir: &str, revision: i64) -> String {
        format!(
            "{}{}/@{}",
            base_dir.trim_end_matches('/'),
            self.package_path,
            revision,
        )
    }
}