arks-server 1.3.10

Git server: upload-pack / receive-pack + smart/dumb HTTP + daemon (M11/M25)
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
//! Commit diff and branch/tag compare views with GitHub-exact dual line numbers
//! (Unified + Split SSR fallback) and vendored `diff2html` progressive enhancement.

use super::{ActiveTab, RepoContext, format_timestamp, html_escape, render_page, resolve_rev_oid};
use crate::http::HttpResponse;
use arks_object::TreeMode;
use arks_oid::Oid;
use arks_repo::Repository;
use std::collections::BTreeMap;

struct FileDiff {
    path: String,
    status: &'static str,
    adds: usize,
    dels: usize,
    rows: Vec<DiffRow>,
    raw_patch: String,
}

struct DiffRow {
    old_ln: Option<usize>,
    new_ln: Option<usize>,
    kind: DiffKind,
    text: String,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum DiffKind {
    Context,
    Add,
    Del,
    Hunk,
}

fn flatten_tree(repo: &Repository, tree_oid: Oid, prefix: &str, out: &mut BTreeMap<String, Oid>) {
    let Some(tree) = super::load_tree(repo, &tree_oid) else {
        return;
    };
    for e in tree.entries {
        let full = if prefix.is_empty() {
            e.name
        } else {
            format!("{prefix}/{}", e.name)
        };
        if e.mode == TreeMode::Tree {
            flatten_tree(repo, e.oid, &full, out);
        } else {
            out.insert(full, e.oid);
        }
    }
}

fn read_text_lines(repo: &Repository, oid: Oid) -> Option<Vec<String>> {
    let raw = super::load_blob(repo, &oid)?;
    if raw.len() > 256 * 1024 || raw.contains(&0) {
        return None;
    }
    let s = String::from_utf8_lossy(&raw);
    Some(s.lines().map(ToString::to_string).collect())
}

fn diff_two_trees(repo: &Repository, base_tree: Option<Oid>, head_tree: Oid) -> Vec<FileDiff> {
    let mut base_map = BTreeMap::new();
    if let Some(bt) = base_tree {
        flatten_tree(repo, bt, "", &mut base_map);
    }
    let mut head_map = BTreeMap::new();
    flatten_tree(repo, head_tree, "", &mut head_map);

    let mut all_paths: Vec<String> = base_map.keys().chain(head_map.keys()).cloned().collect();
    all_paths.sort();
    all_paths.dedup();

    let mut diffs = Vec::new();
    for path in all_paths.into_iter().take(64) {
        let b = base_map.get(&path).copied();
        let h = head_map.get(&path).copied();
        if b == h {
            continue;
        }
        let status = match (b, h) {
            (None, Some(_)) => "added",
            (Some(_), None) => "deleted",
            _ => "modified",
        };
        let old_lines = b.and_then(|o| read_text_lines(repo, o)).unwrap_or_default();
        let new_lines = h.and_then(|o| read_text_lines(repo, o)).unwrap_or_default();
        diffs.push(build_file_diff(path, status, &old_lines, &new_lines));
    }
    diffs
}

fn build_file_diff(
    path: String,
    status: &'static str,
    old_lines: &[String],
    new_lines: &[String],
) -> FileDiff {
    let mut rows = Vec::new();
    let mut raw = format!("diff --git a/{path} b/{path}\n--- a/{path}\n+++ b/{path}\n");
    let mut adds = 0usize;
    let mut dels = 0usize;

    let hunk = format!("@@ -1,{} +1,{} @@", old_lines.len(), new_lines.len());
    raw.push_str(&hunk);
    raw.push('\n');
    rows.push(DiffRow {
        old_ln: None,
        new_ln: None,
        kind: DiffKind::Hunk,
        text: hunk,
    });

    // Simple prefix/suffix trimming + line-by-line delta (fast, bounded, exact line numbers)
    let mut pref = 0;
    while pref < old_lines.len() && pref < new_lines.len() && old_lines[pref] == new_lines[pref] {
        pref += 1;
    }
    let mut suff = 0;
    while suff + pref < old_lines.len()
        && suff + pref < new_lines.len()
        && old_lines[old_lines.len() - 1 - suff] == new_lines[new_lines.len() - 1 - suff]
    {
        suff += 1;
    }

    // Context before change (up to 3 lines)
    let ctx_start = pref.saturating_sub(3);
    for (idx, line) in old_lines.iter().enumerate().take(pref).skip(ctx_start) {
        raw.push(' ');
        raw.push_str(line);
        raw.push('\n');
        rows.push(DiffRow {
            old_ln: Some(idx + 1),
            new_ln: Some(idx + 1),
            kind: DiffKind::Context,
            text: line.clone(),
        });
    }
    // Deleted lines
    for (idx, line) in old_lines[pref..old_lines.len() - suff].iter().enumerate() {
        dels += 1;
        raw.push('-');
        raw.push_str(line);
        raw.push('\n');
        rows.push(DiffRow {
            old_ln: Some(pref + idx + 1),
            new_ln: None,
            kind: DiffKind::Del,
            text: line.clone(),
        });
    }
    // Added lines
    for (idx, line) in new_lines[pref..new_lines.len() - suff].iter().enumerate() {
        adds += 1;
        raw.push('+');
        raw.push_str(line);
        raw.push('\n');
        rows.push(DiffRow {
            old_ln: None,
            new_ln: Some(pref + idx + 1),
            kind: DiffKind::Add,
            text: line.clone(),
        });
    }
    // Context after change (up to 3 lines)
    let tail_old_start = old_lines.len() - suff;
    let tail_new_start = new_lines.len() - suff;
    for k in 0..suff.min(3) {
        let line = &old_lines[tail_old_start + k];
        raw.push(' ');
        raw.push_str(line);
        raw.push('\n');
        rows.push(DiffRow {
            old_ln: Some(tail_old_start + k + 1),
            new_ln: Some(tail_new_start + k + 1),
            kind: DiffKind::Context,
            text: line.clone(),
        });
    }

    FileDiff {
        path,
        status,
        adds,
        dels,
        rows,
        raw_patch: raw,
    }
}

fn render_diff_section(diffs: &[FileDiff]) -> String {
    let total_adds: usize = diffs.iter().map(|d| d.adds).sum();
    let total_dels: usize = diffs.iter().map(|d| d.dels).sum();
    let mut combined_raw = String::new();
    for d in diffs {
        combined_raw.push_str(&d.raw_patch);
    }

    let mut html = format!(
        "<div class=\"toolbar\"><div class=\"toolbar-group\">\
         <strong>{} changed file(s)</strong>\
         <span class=\"badge badge-green\">+{}</span>\
         <span class=\"badge u-text-red\">-{}</span></div>\
         <div class=\"btn-group\">\
           <button type=\"button\" class=\"btn active\" data-diff-mode=\"line-by-line\">Unified</button>\
           <button type=\"button\" class=\"btn\" data-diff-mode=\"side-by-side\">Split</button>\
         </div></div>\n\
         <textarea id=\"raw-diff-payload\" hidden>{}</textarea>\n\
         <div id=\"diff2html-mount\" data-diff2html-raw=\"raw-diff-payload\"></div>\n",
        diffs.len(),
        total_adds,
        total_dels,
        html_escape(&combined_raw),
    );

    // Dual-line-number SSR Unified & Split fallback (works even with JS disabled)
    html.push_str("<div id=\"ssr-diff-unified\">");
    for d in diffs {
        html.push_str(&format!(
            "<div class=\"card\"><div class=\"card-header\">\
             <span class=\"mono\"><strong>{}</strong> <span class=\"badge\">{}</span></span>\
             <span class=\"mono muted\">+{} -{}</span></div>\
             <div class=\"code-wrap\"><table class=\"diff-table\">",
            html_escape(&d.path),
            d.status,
            d.adds,
            d.dels
        ));
        for r in &d.rows {
            let cls = match r.kind {
                DiffKind::Add => " class=\"diff-add\"",
                DiffKind::Del => " class=\"diff-del\"",
                DiffKind::Hunk => " class=\"diff-hunk\"",
                DiffKind::Context => "",
            };
            let prefix = match r.kind {
                DiffKind::Add => "+",
                DiffKind::Del => "-",
                DiffKind::Context => " ",
                DiffKind::Hunk => "",
            };
            let old_s = r.old_ln.map(|n| n.to_string()).unwrap_or_default();
            let new_s = r.new_ln.map(|n| n.to_string()).unwrap_or_default();
            html.push_str(&format!(
                "<tr{cls}><td class=\"diff-ln\">{old_s}</td><td class=\"diff-ln\">{new_s}</td>\
                 <td class=\"diff-code\">{prefix}{}</td></tr>",
                html_escape(&r.text)
            ));
        }
        html.push_str("</table></div></div>");
    }
    html.push_str("</div>");

    html.push_str("<div id=\"ssr-diff-split\" hidden>");
    for d in diffs {
        html.push_str(&format!(
            "<div class=\"card\"><div class=\"card-header\">\
             <span class=\"mono\"><strong>{}</strong></span>\
             <span class=\"mono muted\">+{} -{}</span></div>\
             <div class=\"diff-split-grid\"><div><table class=\"diff-table\">",
            html_escape(&d.path),
            d.adds,
            d.dels
        ));
        for r in &d.rows {
            if r.kind != DiffKind::Add {
                let cls = match r.kind {
                    DiffKind::Del => " class=\"diff-del\"",
                    DiffKind::Hunk => " class=\"diff-hunk\"",
                    _ => "",
                };
                let old_s = r.old_ln.map(|n| n.to_string()).unwrap_or_default();
                html.push_str(&format!(
                    "<tr{cls}><td class=\"diff-ln\">{old_s}</td><td class=\"diff-code\">{}</td></tr>",
                    html_escape(&r.text)
                ));
            }
        }
        html.push_str("</table></div><div><table class=\"diff-table\">");
        for r in &d.rows {
            if r.kind != DiffKind::Del {
                let cls = match r.kind {
                    DiffKind::Add => " class=\"diff-add\"",
                    DiffKind::Hunk => " class=\"diff-hunk\"",
                    _ => "",
                };
                let new_s = r.new_ln.map(|n| n.to_string()).unwrap_or_default();
                html.push_str(&format!(
                    "<tr{cls}><td class=\"diff-ln\">{new_s}</td><td class=\"diff-code\">{}</td></tr>",
                    html_escape(&r.text)
                ));
            }
        }
        html.push_str("</table></div></div></div>");
    }
    html.push_str("</div>");
    html
}

/// Render `/commit/<oid>` with commit metadata, signature badge, and dual-line diff.
pub fn render_commit(repo: &Repository, ctx: &RepoContext, oid_str: &str) -> HttpResponse {
    let Some(oid) = resolve_rev_oid(repo, oid_str) else {
        return render_page(
            404,
            ctx,
            ActiveTab::Code,
            "Commit not found",
            "<div class=\"card\"><div class=\"card-body\">Commit not found.</div></div>",
        );
    };
    let Some(commit) = super::load_commit(repo, &oid) else {
        return render_page(
            404,
            ctx,
            ActiveTab::Code,
            "Not a commit",
            "<div class=\"card\"><div class=\"card-body\">Object is not a commit.</div></div>",
        );
    };

    let parent_tree = commit
        .parents
        .first()
        .and_then(|p| super::load_commit(repo, p).map(|c| c.tree));
    let diffs = diff_two_trees(repo, parent_tree, commit.tree);
    let has_sig = super::commit_has_sig(&commit);
    let sig_badge = if has_sig {
        super::SIG_BADGE_UNVERIFIED
    } else {
        "<span class=\"badge\">Unsigned</span>"
    };

    let m = ctx.mount_prefix();
    let oid_hex = oid.to_hex();
    let subject = commit.message.lines().next().unwrap_or("(no message)");
    let body = format!(
        "<div class=\"card\"><div class=\"card-header\">\
         <div><strong>{}</strong> {}</div>\
         <div class=\"u-flex u-gap-2 u-items-center\">\
           <span class=\"mono muted\">{}</span>\
           <a class=\"btn\" href=\"{m}/commit/{}.patch\">.patch</a>\
         </div></div>\
         <div class=\"card-body\">\
           <div class=\"muted\">Author: <strong>{}</strong> &lt;{}&gt; · {}</div>\
           <pre class=\"u-mt-3 u-pre-wrap\">{}</pre>\
         </div></div>\n{}",
        html_escape(subject),
        sig_badge,
        html_escape(&oid_hex),
        html_escape(&oid_hex),
        html_escape(&commit.author.name),
        html_escape(&commit.author.email),
        html_escape(&format_timestamp(commit.author.timestamp)),
        html_escape(&commit.message),
        render_diff_section(&diffs)
    );
    render_page(
        200,
        ctx,
        ActiveTab::Code,
        &format!("Commit {} — {}", &oid.to_hex()[..7], ctx.name),
        &body,
    )
}

/// Render `/compare` or `/compare/<base>..<head>` comparing two branches/tags/commits.
pub fn render_compare(repo: &Repository, ctx: &RepoContext, spec: &str) -> HttpResponse {
    let (base_ref, head_ref) = if let Some((b, h)) = spec.split_once("..") {
        (b.trim(), h.trim())
    } else if ctx.branches.len() >= 2 {
        (ctx.branches[0].0.as_str(), ctx.branches[1].0.as_str())
    } else {
        (ctx.default_branch.as_str(), ctx.default_branch.as_str())
    };

    let base_oid = resolve_rev_oid(repo, base_ref);
    let head_oid = resolve_rev_oid(repo, head_ref);

    let mut header = format!(
        "<div class=\"card\"><div class=\"card-header\">\
         <div><strong>Compare</strong> <span class=\"badge mono\">base: {}</span> \
         <span class=\"muted\">←</span> \
         <span class=\"badge badge-green mono\">head: {}</span></div>\
         <span class=\"muted\">URL: <code>/compare/&lt;base&gt;..&lt;head&gt;</code></span>\
         </div></div>",
        html_escape(base_ref),
        html_escape(head_ref),
    );

    let (Some(b_oid), Some(h_oid)) = (base_oid, head_oid) else {
        header.push_str(
            "<div class=\"card\"><div class=\"card-body muted\">Specify two valid refs (e.g. <code>/compare/main..feature</code>).</div></div>",
        );
        return render_page(
            200,
            ctx,
            ActiveTab::Pulls,
            &format!("Compare — {}", ctx.name),
            &header,
        );
    };

    let b_tree = super::load_commit(repo, &b_oid).map(|c| c.tree);
    let h_tree = super::load_commit(repo, &h_oid).map(|c| c.tree);

    let Some(ht) = h_tree else {
        header.push_str("<div class=\"card\"><div class=\"card-body muted\">Head ref is not a commit.</div></div>");
        return render_page(
            200,
            ctx,
            ActiveTab::Pulls,
            &format!("Compare — {}", ctx.name),
            &header,
        );
    };

    let diffs = diff_two_trees(repo, b_tree, ht);
    if diffs.is_empty() {
        header.push_str("<div class=\"card\"><div class=\"card-body muted\">There are no differences between these revisions.</div></div>");
    } else {
        header.push_str(&render_diff_section(&diffs));
    }

    render_page(
        200,
        ctx,
        ActiveTab::Pulls,
        &format!("Compare {base_ref}..{head_ref} — {}", ctx.name),
        &header,
    )
}

/// Render dual-line Unified/Split diff HTML between two commit OIDs for embedding in PR views.
pub(crate) fn render_diff_between_commits(
    repo: &Repository,
    base_oid: Oid,
    head_oid: Oid,
) -> String {
    let b_tree = super::load_commit(repo, &base_oid).map(|c| c.tree);
    let Some(h_tree) = super::load_commit(repo, &head_oid).map(|c| c.tree) else {
        return "<div class=\"card\"><div class=\"card-body muted\">Head commit unavailable.</div></div>".into();
    };
    let diffs = diff_two_trees(repo, b_tree, h_tree);
    if diffs.is_empty() {
        return "<div class=\"card\"><div class=\"card-body muted\">No file differences.</div></div>".into();
    }
    render_diff_section(&diffs)
}