solid-pod-rs-git 0.4.0-alpha.15

Git HTTP smart-protocol backend for solid-pod-rs, mirroring JavaScriptSolidServer's src/handlers/git.js (PARITY rows 69, 100, 199, 200).
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
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
//! High-level git control-panel operations for a pod's git repository.
//!
//! Each function shells out to the `git` binary via [`tokio::process::Command`].
//! All functions take `repo: &Path` — the absolute filesystem path to the
//! pod's git repository (i.e. `data_root/{pubkey}`).
//!
//! These are wired to REST endpoints in `solid-pod-rs-server` behind
//! `#[cfg(feature = "git")]`.

use std::path::Path;

use serde::{Deserialize, Serialize};
use tokio::process::Command;

use crate::error::GitError;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// Type of change reported by `git status`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ChangeType {
    /// File contents modified.
    Modified,
    /// File added to the index.
    Added,
    /// File deleted.
    Deleted,
    /// File renamed (old path is in `FileStatus::old_path`).
    Renamed,
    /// File copied (old path is in `FileStatus::old_path`).
    Copied,
}

/// A single file entry in the status report.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileStatus {
    /// Relative path within the repository.
    pub path: String,
    /// Nature of the change.
    pub change_type: ChangeType,
    /// For renames/copies: the original path before the operation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub old_path: Option<String>,
}

/// Full status report for a repository.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusReport {
    /// Current branch name.
    pub branch: String,
    /// Commits ahead of the upstream tracking branch.
    pub ahead: u32,
    /// Commits behind the upstream tracking branch.
    pub behind: u32,
    /// Files staged for the next commit (index changes).
    pub staged: Vec<FileStatus>,
    /// Files with unstaged working-tree changes.
    pub unstaged: Vec<FileStatus>,
    /// Paths not tracked by git.
    pub untracked: Vec<String>,
    /// `true` when all three lists are empty.
    pub is_clean: bool,
}

/// A single commit log entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommitEntry {
    /// Full 40-character SHA-1 hash.
    pub hash: String,
    /// 7-character abbreviated hash.
    pub short_hash: String,
    /// First line of the commit message.
    pub message: String,
    /// Author name.
    pub author: String,
    /// ISO-8601 author date.
    pub date: String,
    /// Human-readable relative date (e.g. "3 days ago").
    pub date_relative: String,
}

/// Current branch state.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BranchInfo {
    /// The currently checked-out branch.
    pub current: String,
    /// All local branches.
    pub local: Vec<String>,
}

/// Result of a successful commit.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommitResult {
    /// Full commit hash.
    pub hash: String,
    /// Abbreviated commit hash.
    pub short_hash: String,
    /// One-line commit subject.
    pub summary: String,
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Run a git command and collect stdout. Returns `GitError::BackendFailed`
/// when the exit code is non-zero.
async fn git_run(args: &[&str], repo: &Path) -> Result<String, GitError> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repo)
        .output()
        .await
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                GitError::BackendNotAvailable("git binary not found in PATH".into())
            } else {
                GitError::Io(e)
            }
        })?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).into_owned())
    } else {
        Err(GitError::BackendFailed {
            exit_code: output.status.code(),
            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        })
    }
}

/// Same as `git_run` but tolerates a non-zero exit code, returning the
/// stdout anyway (some git commands like `reset HEAD` exit 1 on early commits).
async fn git_run_tolerant(args: &[&str], repo: &Path) -> Result<String, GitError> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repo)
        .output()
        .await
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                GitError::BackendNotAvailable("git binary not found in PATH".into())
            } else {
                GitError::Io(e)
            }
        })?;

    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}

/// Validate a user-supplied file path segment: must not contain `..` and must
/// not start with `/`.
fn validate_path(path: &str) -> Result<(), GitError> {
    if path.starts_with('/') || path.contains("..") {
        return Err(GitError::PathTraversal(path.to_string()));
    }
    Ok(())
}

/// Parse a `--porcelain=v1 -b` status line for the index (X) character.
fn parse_xy(x: char, y: char) -> (Option<ChangeType>, Option<ChangeType>) {
    let map_char = |c: char| match c {
        'M' => Some(ChangeType::Modified),
        'A' => Some(ChangeType::Added),
        'D' => Some(ChangeType::Deleted),
        'R' => Some(ChangeType::Renamed),
        'C' => Some(ChangeType::Copied),
        _ => None,
    };
    (map_char(x), map_char(y))
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Return the working-tree and index status of `repo`.
pub async fn git_status(repo: &Path) -> Result<StatusReport, GitError> {
    let raw = git_run(&["status", "--porcelain=v1", "-b"], repo).await?;
    parse_status_output(&raw)
}

/// Pure-function status parser — split out for unit-testing.
pub fn parse_status_output(raw: &str) -> Result<StatusReport, GitError> {
    let mut lines = raw.lines();

    // ── Branch line ──────────────────────────────────────────────────────────
    let branch_line = lines.next().unwrap_or("");
    // Strip leading `## `
    let branch_line = branch_line.trim_start_matches("## ");

    let mut ahead: u32 = 0;
    let mut behind: u32 = 0;

    let branch: String;
    if branch_line.starts_with("No commits yet on ") {
        branch = branch_line
            .trim_start_matches("No commits yet on ")
            .to_string();
    } else {
        // Format: `main...origin/main [ahead 1, behind 2]`
        // or just: `main` (no tracking)
        let (branch_part, tracking_part) = if let Some(idx) = branch_line.find("...") {
            (&branch_line[..idx], Some(&branch_line[idx + 3..]))
        } else {
            (branch_line, None)
        };
        branch = branch_part.to_string();

        if let Some(tracking) = tracking_part {
            // Parse optional `[ahead N, behind M]` suffix.
            if let Some(bracket_start) = tracking.find('[') {
                let inside = &tracking[bracket_start + 1..];
                let inside = inside.trim_end_matches(']');
                for part in inside.split(',') {
                    let part = part.trim();
                    if let Some(n) = part.strip_prefix("ahead ") {
                        ahead = n.trim().parse().unwrap_or(0);
                    } else if let Some(n) = part.strip_prefix("behind ") {
                        behind = n.trim().parse().unwrap_or(0);
                    }
                }
            }
        }
    }

    // ── File lines ───────────────────────────────────────────────────────────
    let mut staged: Vec<FileStatus> = Vec::new();
    let mut unstaged: Vec<FileStatus> = Vec::new();
    let mut untracked: Vec<String> = Vec::new();

    for line in lines {
        if line.len() < 4 {
            continue;
        }
        let x = line.chars().next().unwrap_or(' ');
        let y = line.chars().nth(1).unwrap_or(' ');
        let rest = &line[3..]; // skip "XY "

        if x == '?' && y == '?' {
            untracked.push(rest.to_string());
            continue;
        }

        let (staged_change, unstaged_change) = parse_xy(x, y);

        // Renamed / Copied entries in porcelain v1 look like:
        //   `R  new_path\0old_path` but porcelain v1 uses `->` separator.
        //   `R  newpath -> oldpath`
        // Actually porcelain=v1 uses: `R  dest\toriginal` on a single line
        // separated by `\0` in -z mode. Without -z it is `dest -> origin`.
        let (path, old_path) = if (x == 'R' || x == 'C' || y == 'R' || y == 'C')
            && rest.contains(" -> ")
        {
            let mut parts = rest.splitn(2, " -> ");
            let dest = parts.next().unwrap_or(rest).to_string();
            let orig = parts.next().map(str::to_string);
            (dest, orig)
        } else {
            (rest.to_string(), None)
        };

        if let Some(ct) = staged_change {
            staged.push(FileStatus {
                path: path.clone(),
                change_type: ct,
                old_path: old_path.clone(),
            });
        }
        if let Some(ct) = unstaged_change {
            unstaged.push(FileStatus {
                path: path.clone(),
                change_type: ct,
                old_path,
            });
        }
    }

    let is_clean = staged.is_empty() && unstaged.is_empty() && untracked.is_empty();

    Ok(StatusReport {
        branch,
        ahead,
        behind,
        staged,
        unstaged,
        untracked,
        is_clean,
    })
}

/// Return the commit log for `repo`, up to `limit` entries (capped at 100).
pub async fn git_log(repo: &Path, limit: u32) -> Result<Vec<CommitEntry>, GitError> {
    let cap = limit.min(100);
    let cap_str = cap.to_string();
    let format = "%H\x1F%h\x1F%s\x1F%an\x1F%aI\x1F%ar";
    let raw = match git_run(
        &["log", &format!("--format={format}"), "-n", &cap_str],
        repo,
    )
    .await
    {
        Ok(v) => v,
        Err(GitError::BackendFailed { ref stderr, .. }) if stderr.contains("does not have any commits") || stderr.contains("bad default revision") || stderr.contains("fatal: your current branch") => {
            return Ok(Vec::new());
        }
        Err(e) => return Err(e),
    };

    if raw.trim().is_empty() {
        return Ok(Vec::new());
    }

    let mut entries = Vec::new();
    for line in raw.lines() {
        let parts: Vec<&str> = line.splitn(6, '\x1F').collect();
        if parts.len() < 6 {
            continue;
        }
        entries.push(CommitEntry {
            hash: parts[0].to_string(),
            short_hash: parts[1].to_string(),
            message: parts[2].to_string(),
            author: parts[3].to_string(),
            date: parts[4].to_string(),
            date_relative: parts[5].to_string(),
        });
    }
    Ok(entries)
}

/// Return a unified diff for the repository or a specific file.
///
/// - `staged = true` produces `git diff --cached` (index vs HEAD).
/// - `staged = false` produces `git diff` (working tree vs index).
/// - `path` restricts the diff to a single file.
pub async fn git_diff(repo: &Path, path: Option<&str>, staged: bool) -> Result<String, GitError> {
    if let Some(p) = path {
        validate_path(p)?;
    }

    let mut args: Vec<&str> = vec!["diff", "-U5"];
    if staged {
        args.push("--cached");
    }
    if let Some(p) = path {
        args.push("--");
        args.push(p);
    }

    // `git diff` exits 0 even when there are no differences.
    git_run(&args, repo).await
}

/// Stage files. If `all` is true, runs `git add -A`. Otherwise stages
/// only the listed `paths`.
pub async fn git_add(repo: &Path, paths: &[String], all: bool) -> Result<(), GitError> {
    if all {
        git_run(&["add", "-A"], repo).await?;
    } else {
        for p in paths {
            validate_path(p)?;
        }
        let mut args = vec!["add", "--"];
        let path_refs: Vec<&str> = paths.iter().map(String::as_str).collect();
        args.extend_from_slice(&path_refs);
        git_run(&args, repo).await?;
    }
    Ok(())
}

/// Unstage files. If `all` is true, unstages everything. Otherwise
/// unstages only the listed `paths`.
///
/// `git reset HEAD` can exit 1 on repositories with no commits yet;
/// this is handled gracefully.
pub async fn git_unstage(repo: &Path, paths: &[String], all: bool) -> Result<(), GitError> {
    if all {
        git_run_tolerant(&["reset", "HEAD", "--", "."], repo).await?;
    } else {
        for p in paths {
            validate_path(p)?;
        }
        let mut args = vec!["reset", "HEAD", "--"];
        let path_refs: Vec<&str> = paths.iter().map(String::as_str).collect();
        args.extend_from_slice(&path_refs);
        git_run_tolerant(&args, repo).await?;
    }
    Ok(())
}

/// Create a commit with the given message. Returns the new commit hash and
/// a one-line summary.
pub async fn git_commit(
    repo: &Path,
    message: &str,
    author_name: &str,
    author_email: &str,
) -> Result<CommitResult, GitError> {
    let author_str = format!("{author_name} <{author_email}>");
    git_run(
        &["commit", "-m", message, "--author", &author_str],
        repo,
    )
    .await?;

    let hash = git_run(&["rev-parse", "HEAD"], repo).await?;
    let hash = hash.trim().to_string();
    let short_hash = if hash.len() >= 7 {
        hash[..7].to_string()
    } else {
        hash.clone()
    };

    // Extract the commit subject for the summary.
    let summary = git_run(&["log", "-1", "--format=%s", &hash], repo)
        .await
        .unwrap_or_else(|_| message.to_string());
    let summary = summary.trim().to_string();

    Ok(CommitResult {
        hash,
        short_hash,
        summary,
    })
}

/// Return the list of local branches and identify the current one.
pub async fn git_branches(repo: &Path) -> Result<BranchInfo, GitError> {
    let raw = match git_run(
        &["branch", "--format=%(HEAD) %(refname:short)"],
        repo,
    )
    .await
    {
        Ok(v) => v,
        Err(GitError::BackendFailed { ref stderr, .. })
            if stderr.contains("does not have any commits")
                || stderr.contains("bad default revision") =>
        {
            return Ok(BranchInfo {
                current: "main".to_string(),
                local: vec![],
            });
        }
        Err(e) => return Err(e),
    };

    if raw.trim().is_empty() {
        return Ok(BranchInfo {
            current: "main".to_string(),
            local: vec![],
        });
    }

    let mut current = String::from("main");
    let mut local: Vec<String> = Vec::new();

    for line in raw.lines() {
        // Format: `* main` or `  feature-x`
        let is_current = line.starts_with("* ");
        let name = line.trim_start_matches("* ").trim_start_matches("  ").trim();
        if name.is_empty() {
            continue;
        }
        if is_current {
            current = name.to_string();
        }
        local.push(name.to_string());
    }

    Ok(BranchInfo { current, local })
}

/// Create and switch to a new branch called `name`.
pub async fn git_create_branch(repo: &Path, name: &str) -> Result<(), GitError> {
    // Validate branch name: no `..`, no spaces, must not start with `-`.
    if name.contains("..") || name.contains(' ') || name.starts_with('-') {
        return Err(GitError::PathTraversal(format!(
            "invalid branch name: {name}"
        )));
    }
    git_run(&["checkout", "-b", name], repo).await?;
    Ok(())
}

/// Discard working-tree changes to the listed `paths` (equivalent to
/// `git checkout -- <paths>`).
pub async fn git_discard(repo: &Path, paths: &[String]) -> Result<(), GitError> {
    for p in paths {
        validate_path(p)?;
    }
    let mut args = vec!["checkout", "--"];
    let path_refs: Vec<&str> = paths.iter().map(String::as_str).collect();
    args.extend_from_slice(&path_refs);
    git_run(&args, repo).await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_status(raw: &str) -> StatusReport {
        parse_status_output(raw).expect("parse failed")
    }

    #[test]
    fn parse_clean_repo() {
        let raw = "## main...origin/main\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "main");
        assert_eq!(s.ahead, 0);
        assert_eq!(s.behind, 0);
        assert!(s.is_clean);
        assert!(s.staged.is_empty());
        assert!(s.unstaged.is_empty());
        assert!(s.untracked.is_empty());
    }

    #[test]
    fn parse_ahead_behind() {
        let raw = "## main...origin/main [ahead 3, behind 1]\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "main");
        assert_eq!(s.ahead, 3);
        assert_eq!(s.behind, 1);
    }

    #[test]
    fn parse_ahead_only() {
        let raw = "## feature...origin/feature [ahead 2]\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "feature");
        assert_eq!(s.ahead, 2);
        assert_eq!(s.behind, 0);
    }

    #[test]
    fn parse_no_commits_yet() {
        let raw = "## No commits yet on main\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "main");
        assert_eq!(s.ahead, 0);
        assert_eq!(s.behind, 0);
        assert!(s.is_clean);
    }

    #[test]
    fn parse_no_commits_yet_with_staged() {
        let raw = "## No commits yet on main\nA  README.md\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "main");
        assert_eq!(s.staged.len(), 1);
        assert_eq!(s.staged[0].change_type, ChangeType::Added);
        assert_eq!(s.staged[0].path, "README.md");
        assert!(!s.is_clean);
    }

    #[test]
    fn parse_modified_staged_and_unstaged() {
        // X=M (staged modified), Y=M (unstaged modified)
        let raw = "## main\nMM src/lib.rs\n";
        let s = make_status(raw);
        assert_eq!(s.staged.len(), 1);
        assert_eq!(s.staged[0].change_type, ChangeType::Modified);
        assert_eq!(s.unstaged.len(), 1);
        assert_eq!(s.unstaged[0].change_type, ChangeType::Modified);
    }

    #[test]
    fn parse_untracked() {
        let raw = "## main\n?? newfile.txt\n";
        let s = make_status(raw);
        assert_eq!(s.untracked, vec!["newfile.txt"]);
        assert!(!s.is_clean);
    }

    #[test]
    fn parse_deleted_staged() {
        let raw = "## main\nD  old.txt\n";
        let s = make_status(raw);
        assert_eq!(s.staged.len(), 1);
        assert_eq!(s.staged[0].change_type, ChangeType::Deleted);
        assert_eq!(s.staged[0].path, "old.txt");
    }

    #[test]
    fn parse_renamed_staged() {
        let raw = "## main\nR  new.txt -> old.txt\n";
        let s = make_status(raw);
        assert_eq!(s.staged.len(), 1);
        assert_eq!(s.staged[0].change_type, ChangeType::Renamed);
        assert_eq!(s.staged[0].path, "new.txt");
        assert_eq!(s.staged[0].old_path.as_deref(), Some("old.txt"));
    }

    #[test]
    fn parse_branch_no_tracking() {
        let raw = "## detached-head\nM  foo.rs\n";
        let s = make_status(raw);
        assert_eq!(s.branch, "detached-head");
        assert_eq!(s.ahead, 0);
        assert_eq!(s.behind, 0);
    }

    #[test]
    fn validate_path_rejects_dotdot() {
        assert!(validate_path("../etc/passwd").is_err());
        assert!(validate_path("foo/../../bar").is_err());
    }

    #[test]
    fn validate_path_rejects_absolute() {
        assert!(validate_path("/etc/passwd").is_err());
    }

    #[test]
    fn validate_path_accepts_normal() {
        assert!(validate_path("src/lib.rs").is_ok());
        assert!(validate_path("README.md").is_ok());
    }
}