lintian-brush 0.182.0

Automatic lintian issue fixer
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
use crate::declare_detector;
use crate::diagnostic::{Action, Deb822Action, DebcargoAction, Diagnostic, ParagraphSelector};
use crate::{FixerError, FixerPreferences, LintianIssue, Visibility};
use debian_control::vcs::ParsedVcs;
use debian_workspace::Workspace;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::path::PathBuf;

#[cfg(feature = "udd")]
async fn get_branch_from_url(vcs_type: &str, url: &str) -> Result<Option<String>, FixerError> {
    use sqlx::Row;

    let client = debian_analyzer::udd::connect_udd_mirror()
        .await
        .map_err(|e| FixerError::Other(format!("Failed to connect to UDD: {}", e)))?;

    let query = "SELECT branch, status, error FROM vcswatch WHERE url = $1 AND vcs = $2";

    let row = sqlx::query(query)
        .bind(url)
        .bind(vcs_type)
        .fetch_optional(&client)
        .await
        .map_err(|e| FixerError::Other(format!("Failed to query vcswatch: {}", e)))?;

    if let Some(row) = row {
        let status: String = row.get(1);
        if status == "ERROR" {
            let error: String = row.get(2);
            return Err(FixerError::Other(format!(
                "vcswatch URL unusable: {}",
                error
            )));
        }
        let branch: Option<String> = row.get(0);
        Ok(branch)
    } else {
        // Not found in vcswatch
        Ok(None)
    }
}

#[cfg(not(feature = "udd"))]
async fn get_branch_from_url(_vcs_type: &str, _url: &str) -> Result<Option<String>, FixerError> {
    Err(FixerError::NoChanges)
}

/// Get the default branch from a Git repository using dulwich
fn get_default_branch(url: &str, branch: Option<&str>) -> Result<Option<String>, FixerError> {
    Python::attach(|py| {
        // Import dulwich.client
        let dulwich_client = py
            .import("dulwich.client")
            .map_err(|e| FixerError::Other(format!("Failed to import dulwich.client: {}", e)))?;

        // Call get_transport_and_path
        let (client, path): (Py<PyAny>, Py<PyAny>) = dulwich_client
            .call_method1("get_transport_and_path", (url,))
            .map_err(|e| {
                FixerError::Other(format!("Failed to call get_transport_and_path: {}", e))
            })?
            .extract()
            .map_err(|e| FixerError::Other(format!("Failed to extract result: {}", e)))?;

        // Call get_refs which returns LsRemoteResult
        let result = client
            .call_method1(py, "get_refs", (path,))
            .map_err(|e| FixerError::Other(format!("Failed to call get_refs: {}", e)))?;

        // Get the symrefs attribute from the result
        let symrefs_obj = result
            .getattr(py, "symrefs")
            .map_err(|e| FixerError::Other(format!("Failed to get symrefs: {}", e)))?;
        let symrefs = symrefs_obj
            .bind(py)
            .cast::<PyDict>()
            .map_err(|e| FixerError::Other(format!("symrefs is not a dict: {}", e)))?;

        // Determine which ref to look up
        let ref_name: Vec<u8> = if let Some(b) = branch {
            format!("refs/heads/{}", b).into_bytes()
        } else {
            b"HEAD".to_vec()
        };

        // Try to get the symref
        let head = match symrefs.get_item(&ref_name) {
            Ok(Some(head)) => head,
            Ok(None) => return Ok(None),
            Err(_) => return Ok(None),
        };

        // Extract the head as bytes
        let head_bytes: Vec<u8> = head
            .extract()
            .map_err(|e| FixerError::Other(format!("Failed to extract head: {}", e)))?;

        // Check if it starts with refs/heads/
        let prefix = b"refs/heads/";
        if head_bytes.starts_with(prefix) {
            let branch_name = String::from_utf8(head_bytes[prefix.len()..].to_vec())
                .map_err(|e| FixerError::Other(format!("Invalid UTF-8 in branch name: {}", e)))?;
            Ok(Some(branch_name))
        } else {
            // Return as-is if it doesn't start with refs/heads/
            let branch_name = String::from_utf8(head_bytes)
                .map_err(|e| FixerError::Other(format!("Invalid UTF-8 in branch name: {}", e)))?;
            Ok(Some(branch_name))
        }
    })
}

pub async fn detect_async(
    ws: &dyn Workspace,
    preferences: &FixerPreferences,
) -> Result<Vec<Diagnostic>, FixerError> {
    if preferences
        .minimum_certainty
        .is_some_and(|c| c < debian_analyzer::Certainty::Certain)
    {
        return Ok(Vec::new());
    }

    let debcargo_rel = PathBuf::from("debian/debcargo.toml");
    let control_rel = PathBuf::from("debian/control");

    let debcargo_doc = ws.parsed_debcargo()?;
    let is_debcargo = debcargo_doc.is_some();

    // Read the current Vcs-Git URL.
    let vcs_git: Option<String> = if let Some(ref doc) = debcargo_doc {
        doc.get("source")
            .and_then(|s| s.as_table())
            .and_then(|s| s.get("vcs_git"))
            .and_then(|v| v.as_str())
            .map(str::to_string)
    } else {
        let control = match ws.parsed_control() {
            Ok(c) => c,
            Err(_) => return Ok(Vec::new()),
        };
        control.source().and_then(|s| s.as_deb822().get("Vcs-Git"))
    };

    let Some(vcs_git) = vcs_git else {
        return Ok(Vec::new());
    };

    let parsed: ParsedVcs = vcs_git
        .parse()
        .map_err(|e| FixerError::Other(format!("Failed to parse Vcs-Git URL: {}", e)))?;
    let repo_url = &parsed.repo_url;
    let branch = parsed.branch.as_deref();
    let subpath = parsed.subpath.as_deref();

    // Query vcswatch for the branch.
    let new_branch = match get_branch_from_url("Git", &vcs_git).await {
        Ok(Some(b)) => b,
        Ok(None) => return Ok(Vec::new()),
        Err(FixerError::NoChanges) => return Ok(Vec::new()),
        Err(FixerError::Other(msg)) if msg.starts_with("vcswatch URL unusable") => {
            tracing::debug!("{}", msg);
            return Ok(Vec::new());
        }
        Err(e) => return Err(e),
    };

    if Some(new_branch.as_str()) == branch {
        return Ok(Vec::new());
    }

    let default_branch = match get_default_branch(repo_url, branch) {
        Ok(db) => db,
        Err(e) => {
            tracing::debug!("Failed to get default branch from {}: {}", repo_url, e);
            None
        }
    };

    let should_change = preferences.opinionated.unwrap_or(false)
        || default_branch
            .as_ref()
            .is_none_or(|db| new_branch.as_str() != db && Some(db.as_str()) != branch);
    if !should_change {
        return Ok(Vec::new());
    }

    let new_vcs = ParsedVcs {
        repo_url: repo_url.clone(),
        branch: Some(new_branch.clone()),
        subpath: subpath.map(String::from),
    };
    let new_vcs_git = new_vcs.to_string();
    let new_vcs_browser =
        debian_analyzer::vcs::determine_browser_url("git", &new_vcs_git, preferences.net_access)
            .map(|u| u.to_string());

    let issue = LintianIssue::source("vcs-field-invalid-branch", Visibility::Warning);
    let mut actions: Vec<Action> = Vec::new();

    if is_debcargo {
        actions.push(Action::Debcargo(DebcargoAction::SetSourceField {
            file: debcargo_rel.clone(),
            field: "vcs_git".into(),
            value: new_vcs_git,
        }));
        if let Some(browser) = new_vcs_browser {
            actions.push(Action::Debcargo(DebcargoAction::SetSourceField {
                file: debcargo_rel,
                field: "vcs_browser".into(),
                value: browser,
            }));
        }
    } else {
        actions.push(Action::Deb822(Deb822Action::SetField {
            file: control_rel.clone(),
            paragraph: ParagraphSelector::Source,
            field: "Vcs-Git".into(),
            value: new_vcs_git,
        }));
        if let Some(browser) = new_vcs_browser {
            actions.push(Action::Deb822(Deb822Action::SetField {
                file: control_rel,
                paragraph: ParagraphSelector::Source,
                field: "Vcs-Browser".into(),
                value: browser,
            }));
        }
    }

    Ok(vec![Diagnostic::with_actions(
        issue,
        "Vcs-Git URL points at an invalid branch.".to_string(),
        "Set branch from vcswatch in Vcs-Git URL.".to_string(),
        actions,
    )])
}

fn detect(
    ws: &dyn Workspace,
    preferences: &FixerPreferences,
) -> Result<Vec<Diagnostic>, FixerError> {
    let rt = tokio::runtime::Runtime::new()
        .map_err(|e| FixerError::Other(format!("Failed to create runtime: {}", e)))?;
    rt.block_on(detect_async(ws, preferences))
}

declare_detector! {
    name: "vcs-field-invalid-branch",
    tags: ["vcs-field-invalid-branch"],
    triggers: [
        debian_workspace::Trigger::DebcargoField("source.vcs_git"),
        debian_workspace::Trigger::Deb822Field {
            file: "debian/control",
            paragraph_key: "Source",
            field: "Vcs-Git",
        },
    ],
    cost: crate::detector::DetectorCost::Network,
    detect: |ws, prefs| detect(ws, prefs),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Version;
    use debian_workspace::fs_workspace::FsWorkspace;
    use std::fs;
    use std::path::Path;
    use std::str::FromStr;
    use tempfile::TempDir;

    fn make_ws(base: &Path) -> FsWorkspace {
        let v = Version::from_str("1.0-1").unwrap();
        FsWorkspace::new(base.to_path_buf(), Some("test".into()), Some(v))
    }

    #[test]
    fn test_no_vcs_git() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();
        let debian_dir = base_path.join("debian");
        fs::create_dir(&debian_dir).unwrap();

        let control_path = debian_dir.join("control");
        let control_content = r#"Source: test-package

Package: test-package
Description: Test package
 This is a test package.
"#;
        fs::write(&control_path, control_content).unwrap();

        let preferences = FixerPreferences::default();
        let rt = tokio::runtime::Runtime::new().unwrap();
        let ws = make_ws(base_path);
        let result = rt.block_on(detect_async(&ws, &preferences)).unwrap();

        // Should return no diagnostics since there's no Vcs-Git field
        assert!(result.is_empty());
    }

    #[test]
    fn test_vcs_git_with_branch_parsing() {
        // Test that we can parse a Vcs-Git URL with a branch
        let url = "https://salsa.debian.org/team/package.git -b debian/unstable [subdir]";
        let parsed: ParsedVcs = url.parse().unwrap();

        assert_eq!(parsed.repo_url, "https://salsa.debian.org/team/package.git");
        assert_eq!(parsed.branch.as_deref(), Some("debian/unstable"));
        assert_eq!(parsed.subpath.as_deref(), Some("subdir"));
    }

    #[test]
    fn test_vcs_git_without_branch_parsing() {
        // Test parsing a Vcs-Git URL without a branch
        let url = "https://salsa.debian.org/team/package.git";
        let parsed: ParsedVcs = url.parse().unwrap();

        assert_eq!(parsed.repo_url, "https://salsa.debian.org/team/package.git");
        assert_eq!(parsed.branch, None);
        assert_eq!(parsed.subpath, None);
    }

    #[test]
    fn test_get_default_branch_with_local_repo() {
        // Create a local git repository for testing
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path().join("test-repo");
        fs::create_dir(&repo_path).unwrap();

        // Initialize a git repo
        let output = std::process::Command::new("git")
            .args(["init", "--initial-branch=main"])
            .current_dir(&repo_path)
            .output();

        if output.is_err() {
            // Skip test if git is not available
            return;
        }

        // Configure git user for commits
        std::process::Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        // Create a commit
        fs::write(repo_path.join("test.txt"), "test").unwrap();
        std::process::Command::new("git")
            .args(["add", "test.txt"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        // Test get_default_branch
        let repo_url = format!("file://{}", repo_path.display());
        let result = get_default_branch(&repo_url, None);

        match result {
            Ok(Some(branch)) => {
                assert_eq!(branch, "main");
            }
            Ok(None) => {
                // This might happen if dulwich isn't available
                tracing::debug!("get_default_branch returned None");
            }
            Err(e) => {
                // This is expected if dulwich isn't available or can't access the repo
                tracing::debug!(
                    "get_default_branch failed (expected if dulwich unavailable): {}",
                    e
                );
            }
        }
    }

    #[test]
    fn test_low_certainty_returns_no_changes() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();
        let debian_dir = base_path.join("debian");
        fs::create_dir(&debian_dir).unwrap();

        let control_path = debian_dir.join("control");
        let control_content = r#"Source: test-package
Vcs-Git: https://salsa.debian.org/team/package.git -b old-branch

Package: test-package
Description: Test package
 This is a test package.
"#;
        fs::write(&control_path, control_content).unwrap();

        let mut preferences = FixerPreferences::default();
        preferences.minimum_certainty = Some(debian_analyzer::Certainty::Possible);

        let rt = tokio::runtime::Runtime::new().unwrap();
        let ws = make_ws(base_path);
        let result = rt.block_on(detect_async(&ws, &preferences)).unwrap();

        // Should return no diagnostics since minimum_certainty is below Certain
        assert!(result.is_empty());
    }

    #[test]
    #[cfg(not(feature = "udd"))]
    fn test_without_udd_feature() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();
        let debian_dir = base_path.join("debian");
        fs::create_dir(&debian_dir).unwrap();

        let control_path = debian_dir.join("control");
        let control_content = r#"Source: test-package
Vcs-Git: https://salsa.debian.org/team/package.git

Package: test-package
Description: Test package
 This is a test package.
"#;
        fs::write(&control_path, control_content).unwrap();

        let preferences = FixerPreferences::default();
        let rt = tokio::runtime::Runtime::new().unwrap();
        let ws = make_ws(base_path);
        let result = rt.block_on(detect_async(&ws, &preferences)).unwrap();

        // Without UDD feature, should return no diagnostics
        assert!(result.is_empty());
    }
}