silver-platter 0.7.0

Large scale VCS change management
Documentation
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
//! Version control system (VCS) support.
use breezyshim::branch::GenericBranch;
use breezyshim::controldir::{open_containing_from_transport, open_from_transport};
use breezyshim::error::Error as BrzError;
use breezyshim::{
    get_transport, join_segment_parameters, split_segment_parameters, Branch, Transport,
};
use percent_encoding::{utf8_percent_encode, CONTROLS};

#[derive(Debug)]
/// Errors that can occur when opening a branch.
pub enum BranchOpenError {
    /// The VCS is not supported.
    Unsupported {
        /// The URL of the branch.
        url: url::Url,
        /// A description of the error.
        description: String,
        /// The VCS that is not supported.
        vcs: Option<String>,
    },
    /// The branch is missing.
    Missing {
        /// The URL of the branch.
        url: url::Url,

        /// A description of the error.
        description: String,
    },
    /// The branch is rate limited.
    RateLimited {
        /// The URL of the branch.
        url: url::Url,

        /// A description of the error.
        description: String,

        /// The time to wait before retrying.
        retry_after: Option<f64>,
    },
    /// The branch is unavailable.
    Unavailable {
        /// The URL of the branch.
        url: url::Url,

        /// A description of the error.
        description: String,
    },
    /// The branch is temporarily unavailable.
    TemporarilyUnavailable {
        /// The URL of the branch.
        url: url::Url,

        /// A description of the error.
        description: String,
    },

    /// An error occurred.
    Other(String),
}

impl std::fmt::Display for BranchOpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            BranchOpenError::Unsupported {
                url,
                description,
                vcs,
            } => write!(
                f,
                "Unsupported VCS for {}: {} ({})",
                url,
                description,
                vcs.as_deref().unwrap_or("unknown")
            ),
            BranchOpenError::Missing { url, description } => {
                write!(f, "Missing branch {}: {}", url, description)
            }
            BranchOpenError::RateLimited {
                url,
                description,
                retry_after,
            } => write!(
                f,
                "Rate limited {}: {} (retry after: {:?})",
                url, description, retry_after
            ),
            BranchOpenError::Unavailable { url, description } => {
                write!(f, "Unavailable {}: {}", url, description)
            }
            BranchOpenError::TemporarilyUnavailable { url, description } => {
                write!(f, "Temporarily unavailable {}: {}", url, description)
            }
            BranchOpenError::Other(e) => write!(f, "Error: {}", e),
        }
    }
}

#[cfg(feature = "pyo3")]
impl From<BranchOpenError> for pyo3::PyErr {
    fn from(e: BranchOpenError) -> Self {
        use pyo3::import_exception;
        import_exception!(silver_platter, BranchUnsupported);
        import_exception!(silver_platter, BranchTemporarilyUnavailable);
        import_exception!(silver_platter, BranchUnavailable);
        import_exception!(silver_platter, BranchRateLimited);
        import_exception!(silver_platter, BranchMissing);

        use pyo3::exceptions::PyRuntimeError;
        match e {
            BranchOpenError::Unsupported {
                url,
                description,
                vcs,
            } => BranchUnsupported::new_err((url.to_string(), description, vcs)),
            BranchOpenError::Missing { url, description } => {
                BranchMissing::new_err((url.to_string(), description))
            }
            BranchOpenError::RateLimited {
                url,
                description,
                retry_after,
            } => BranchRateLimited::new_err((url.to_string(), description, retry_after)),
            BranchOpenError::Unavailable { url, description } => {
                BranchUnavailable::new_err((url.to_string(), description))
            }
            BranchOpenError::TemporarilyUnavailable { url, description } => {
                BranchTemporarilyUnavailable::new_err((url.to_string(), description))
            }
            BranchOpenError::Other(e) => PyRuntimeError::new_err((e,)),
        }
    }
}

impl BranchOpenError {
    /// Convert a BrzError to a BranchOpenError.
    pub fn from_err(url: url::Url, e: &BrzError) -> Self {
        match e {
            BrzError::NotBranchError(e, reason) => {
                let description = if let Some(reason) = reason {
                    format!("{}: {}", e, reason)
                } else {
                    e.to_string()
                };
                Self::Missing { url, description }
            }
            BrzError::DependencyNotPresent(l, e) => Self::Unavailable {
                url,
                description: format!("missing {}: {}", l, e),
            },
            BrzError::NoColocatedBranchSupport => Self::Unsupported {
                url,
                description: "no colocated branch support".to_string(),
                vcs: None,
            },
            BrzError::Socket(e) => Self::Unavailable {
                url,
                description: format!("Socket error: {}", e),
            },
            BrzError::UnsupportedProtocol(url, extra) => Self::Unsupported {
                url: url.parse().unwrap(),
                description: if let Some(extra) = extra {
                    format!("Unsupported protocol: {}", extra)
                } else {
                    "Unsupported protocol".to_string()
                },
                vcs: None,
            },
            BrzError::ConnectionError(msg) => {
                if e.to_string()
                    .contains("Temporary failure in name resolution")
                {
                    Self::TemporarilyUnavailable {
                        url,
                        description: msg.to_string(),
                    }
                } else {
                    Self::Unavailable {
                        url,
                        description: msg.to_string(),
                    }
                }
            }
            BrzError::PermissionDenied(path, extra) => Self::Unavailable {
                url,
                description: format!(
                    "Permission denied: {}: {}",
                    path.to_string_lossy(),
                    extra.as_deref().unwrap_or("")
                ),
            },
            BrzError::InvalidURL(url, extra) => Self::Unavailable {
                url: url.parse().unwrap(),
                description: extra
                    .as_ref()
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| format!("Invalid URL: {}", url)),
            },
            BrzError::InvalidHttpResponse(_path, msg, _orig_error, headers) => {
                if msg.to_string().contains("Unexpected HTTP status 429") {
                    if let Some(retry_after) = headers.get("Retry-After") {
                        match retry_after.parse::<f64>() {
                            Ok(retry_after) => {
                                return Self::RateLimited {
                                    url,
                                    description: e.to_string(),
                                    retry_after: Some(retry_after),
                                };
                            }
                            Err(e) => {
                                log::warn!("Unable to parse retry-after header: {}", retry_after);
                                return Self::RateLimited {
                                    url,
                                    description: e.to_string(),
                                    retry_after: None,
                                };
                            }
                        }
                    }
                    Self::RateLimited {
                        url,
                        description: e.to_string(),
                        retry_after: None,
                    }
                } else {
                    Self::Unavailable {
                        url,
                        description: e.to_string(),
                    }
                }
            }
            BrzError::TransportError(message) => Self::Unavailable {
                url,
                description: message.to_string(),
            },
            BrzError::UnusableRedirect(source, target, reason) => Self::Unavailable {
                url,
                description: format!("Unusable redirect: {} -> {}: {}", source, target, reason),
            },
            BrzError::UnsupportedVcs(vcs) => Self::Unsupported {
                url,
                description: e.to_string(),
                vcs: Some(vcs.clone()),
            },
            BrzError::UnsupportedFormat(format) => Self::Unsupported {
                url,
                description: e.to_string(),
                vcs: Some(format.clone()),
            },
            BrzError::UnknownFormat(_format) => Self::Unsupported {
                url,
                description: e.to_string(),
                vcs: None,
            },
            BrzError::RemoteGitError(msg) => Self::Unavailable {
                url,
                description: msg.to_string(),
            },
            BrzError::LineEndingError(msg) => Self::Unavailable {
                url,
                description: msg.to_string(),
            },
            BrzError::IncompleteRead(_partial, _expected) => Self::Unavailable {
                url,
                description: e.to_string(),
            },
            _ => Self::Other(e.to_string()),
        }
    }
}

/// Open a branch from a URL.
pub fn open_branch(
    url: &url::Url,
    possible_transports: Option<&mut Vec<Transport>>,
    probers: Option<&[&dyn breezyshim::controldir::PyProber]>,
    name: Option<&str>,
) -> Result<GenericBranch, BranchOpenError> {
    let (url, params) = split_segment_parameters(url);

    let name_owned;
    let name = if let Some(name) = name {
        Some(name)
    } else if let Some(param_name) = params.get("name") {
        name_owned = param_name.clone();
        Some(name_owned.as_str())
    } else {
        None
    };

    let transport = get_transport(&url, possible_transports)
        .map_err(|e| BranchOpenError::from_err(url.clone(), &e))?;
    let dir = open_from_transport(&transport, probers)
        .map_err(|e| BranchOpenError::from_err(url.clone(), &e))?;

    dir.open_branch(name)
        .map(|branch| *branch)
        .map_err(|e| BranchOpenError::from_err(url.clone(), &e))
}

/// Open a branch, either at the specified URL or in a containing directory.
///
/// Return the branch and the subpath of the URL that was used to open it.
pub fn open_branch_containing(
    url: &url::Url,
    possible_transports: Option<&mut Vec<Transport>>,
    probers: Option<&[&dyn breezyshim::controldir::PyProber]>,
    name: Option<&str>,
) -> Result<(GenericBranch, String), BranchOpenError> {
    let (url, params) = split_segment_parameters(url);

    let name_owned;
    let name = if let Some(name) = name {
        Some(name)
    } else if let Some(param_name) = params.get("name") {
        name_owned = param_name.clone();
        Some(name_owned.as_str())
    } else {
        None
    };

    let transport = match get_transport(&url, possible_transports) {
        Ok(transport) => transport,
        Err(e) => return Err(BranchOpenError::from_err(url.clone(), &e)),
    };
    let (dir, subpath) =
        open_containing_from_transport(&transport, probers).map_err(|e| match e {
            BrzError::UnknownFormat(_) => {
                unreachable!("open_containing_from_transport should not return UnknownFormat")
            }
            e => BranchOpenError::from_err(url.clone(), &e),
        })?;

    let branch = dir
        .open_branch(name)
        .map_err(|e| BranchOpenError::from_err(url.clone(), &e))?;
    Ok((*branch, subpath))
}

/// Get the full URL for a branch.
///
/// Ideally this should just return Branch.user_url,
/// but that currently exclude the branch name
/// in some situations.
pub fn full_branch_url(branch: &dyn Branch) -> url::Url {
    match branch.name() {
        None => branch.get_user_url(),
        Some(ref name) if name.is_empty() => branch.get_user_url(),
        Some(name) => {
            let (url, mut params) = split_segment_parameters(&branch.get_user_url());
            params.insert(
                "branch".to_string(),
                utf8_percent_encode(&name, CONTROLS).to_string(),
            );
            join_segment_parameters(&url, params)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use url::Url;

    #[test]
    fn test_branch_open_error_display() {
        // Test Unsupported error
        let err = BranchOpenError::Unsupported {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Not supported".to_string(),
            vcs: Some("git".to_string()),
        };
        assert_eq!(
            err.to_string(),
            "Unsupported VCS for https://example.com/repo: Not supported (git)"
        );

        // Test Unsupported error with unknown VCS
        let err = BranchOpenError::Unsupported {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Not supported".to_string(),
            vcs: None,
        };
        assert_eq!(
            err.to_string(),
            "Unsupported VCS for https://example.com/repo: Not supported (unknown)"
        );

        // Test Missing error
        let err = BranchOpenError::Missing {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Branch not found".to_string(),
        };
        assert_eq!(
            err.to_string(),
            "Missing branch https://example.com/repo: Branch not found"
        );

        // Test RateLimited error
        let err = BranchOpenError::RateLimited {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Too many requests".to_string(),
            retry_after: Some(60.0),
        };
        assert_eq!(
            err.to_string(),
            "Rate limited https://example.com/repo: Too many requests (retry after: Some(60.0))"
        );

        // Test Unavailable error
        let err = BranchOpenError::Unavailable {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Server unavailable".to_string(),
        };
        assert_eq!(
            err.to_string(),
            "Unavailable https://example.com/repo: Server unavailable"
        );

        // Test TemporarilyUnavailable error
        let err = BranchOpenError::TemporarilyUnavailable {
            url: Url::parse("https://example.com/repo").unwrap(),
            description: "Server maintenance".to_string(),
        };
        assert_eq!(
            err.to_string(),
            "Temporarily unavailable https://example.com/repo: Server maintenance"
        );

        // Test Other error
        let err = BranchOpenError::Other("Unknown error".to_string());
        assert_eq!(err.to_string(), "Error: Unknown error");
    }

    #[test]
    fn test_branch_open_error_from_err() {
        // Test NotBranchError conversion
        let brz_err = BrzError::NotBranchError(
            "Not a branch".to_string(),
            Some("Additional info".to_string()),
        );
        let url = Url::parse("https://example.com/repo").unwrap();
        let err = BranchOpenError::from_err(url.clone(), &brz_err);
        match err {
            BranchOpenError::Missing {
                url: err_url,
                description,
            } => {
                assert_eq!(err_url, url);
                assert_eq!(description, "Not a branch: Additional info");
            }
            _ => panic!("Expected Missing error"),
        }

        // Test NotBranchError without reason
        let brz_err = BrzError::NotBranchError("Not a branch".to_string(), None);
        let err = BranchOpenError::from_err(url.clone(), &brz_err);
        match err {
            BranchOpenError::Missing {
                url: err_url,
                description,
            } => {
                assert_eq!(err_url, url);
                assert_eq!(description, "Not a branch");
            }
            _ => panic!("Expected Missing error"),
        }

        // Test ConnectionError with name resolution failure
        let brz_err = BrzError::ConnectionError("Temporary failure in name resolution".to_string());
        let err = BranchOpenError::from_err(url.clone(), &brz_err);
        match err {
            BranchOpenError::TemporarilyUnavailable {
                url: err_url,
                description,
            } => {
                assert_eq!(err_url, url);
                assert_eq!(description, "Temporary failure in name resolution");
            }
            _ => panic!("Expected TemporarilyUnavailable error"),
        }
    }
}