glv 3.0.5

Git log viewer supporting un/folding merges
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
// Copyright (C) 2021  Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
//
// This file is part of git-log-viewer
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use crate::ui::base::search::Needle;
use url::Url;

use getset::Getters;
use git_wrapper::Repository;
use posix_errors::PosixError;
use std::ffi::OsStr;
use std::fmt::{Debug, Display, Formatter};
use std::path::PathBuf;

macro_rules! next_string {
    ($split:expr) => {
        $split.next().expect("Another split").to_owned()
    };
}

#[derive(Clone, Eq, PartialEq)]
pub struct Oid(pub String);

#[cfg(not(tarpaulin_include))]
impl Display for Oid {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

#[cfg(not(tarpaulin_include))]
impl Debug for Oid {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut text = self.0.clone();
        text.truncate(8);
        f.write_str(&text)
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct GitRef(pub String);

#[cfg(not(tarpaulin_include))]
impl Display for GitRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

#[derive(Getters)]
pub struct Commit {
    #[getset(get = "pub")]
    id: Oid,
    #[getset(get = "pub")]
    short_id: String,
    #[getset(get = "pub")]
    author_name: String,
    #[getset(get = "pub")]
    author_email: String,
    #[getset(get = "pub")]
    author_date: String,
    #[getset(get = "pub")]
    author_rel_date: String,
    #[getset(get = "pub")]
    committer_name: String,
    #[getset(get = "pub")]
    committer_email: String,
    #[getset(get = "pub")]
    committer_date: String,
    #[getset(get = "pub")]
    committer_rel_date: String,
    #[getset(get = "pub")]
    subject: String,
    #[getset(get = "pub")]
    body: String,

    #[getset(get = "pub")]
    parents: Vec<Oid>,
    #[allow(dead_code)]
    is_head: bool,
    #[allow(dead_code)]
    is_merge: bool,
    #[allow(dead_code)]
    branches: Vec<GitRef>,
    #[getset(get = "pub")]
    references: Vec<GitRef>,
    #[allow(dead_code)]
    tags: Vec<GitRef>,
}

impl Commit {
    #[must_use]
    pub fn is_merge(&self) -> bool {
        self.parents.len() >= 2
    }

    pub fn matches(&self, needle: &Needle) -> bool {
        let branches = &mut self.branches.iter().map(|v| &v.0).collect::<Vec<_>>();
        let tags = &mut self.tags.iter().map(|v| &v.0).collect::<Vec<_>>();
        let mut candidates = vec![
            self.author_name(),
            self.short_id(),
            &self.id().0,
            self.author_name(),
            self.author_email(),
            self.committer_name(),
            self.committer_email(),
            &self.subject,
        ];
        candidates.append(branches);
        candidates.append(tags);
        if *needle.ignore_case() {
            let needle_lowercase = needle.text();
            candidates
                .iter()
                .map(|x| x.to_lowercase())
                .any(|x| x.contains(needle_lowercase))
        } else {
            candidates.iter().any(|x| x.contains(needle.text()))
        }
    }
}

const REV_FORMAT: &str =
    "--format=%x1f%H%x1f%h%x1f%P%x1f%D%x1f%aN%x1f%aE%x1f%aI%x1f%ad%x1f%cN%x1f%cE%x1f%cI%x1f%cd%x1f%s%x1f%b%x1e";

impl Commit {
    #[must_use]
    pub fn new(data: &str) -> Self {
        let mut split = data.split('\x1f');
        split.next(); // skip commit: XXXX line
        let id = Oid(next_string!(split));

        let short_id = next_string!(split);
        let mut parents_record: Vec<&str> =
            split.next().expect("Parse parents").split(' ').collect();
        if parents_record.len() == 1 && parents_record[0].is_empty() {
            parents_record = vec![];
        }
        let references_record = next_string!(split);

        let author_name = next_string!(split);
        let author_email = next_string!(split);
        let author_date = next_string!(split);
        let author_rel_date = next_string!(split);

        let committer_name = next_string!(split);
        let committer_email = next_string!(split);
        let committer_date = next_string!(split);
        let committer_rel_date = next_string!(split);
        let subject = next_string!(split);
        let body = next_string!(split);

        let mut is_head = false;

        let mut references: Vec<GitRef> = Vec::new();
        let mut branches: Vec<GitRef> = Vec::new();
        let mut tags: Vec<GitRef> = Vec::new();
        for s in references_record.split(", ") {
            if s == "HEAD" {
                is_head = true;
            } else if s.starts_with("HEAD -> ") {
                is_head = true;
                let split_2: Vec<&str> = s.splitn(2, " -> ").collect();
                let branch = split_2[1].to_owned();
                branches.push(GitRef(branch.clone()));
                references.push(GitRef(branch));
            } else if s.starts_with("tag: ") {
                let split_2: Vec<&str> = s.splitn(2, ": ").collect();
                let tag = split_2[1].to_owned();
                tags.push(GitRef(tag.clone()));
                references.push(GitRef(tag));
            } else if s.is_empty() {
                // do nothing
            } else {
                let branch = s.to_owned();
                branches.push(GitRef(branch.clone()));
                references.push(GitRef(branch));
            }
        }

        let is_merge = parents_record.len() >= 2;

        let parents = parents_record
            .into_iter()
            .map(|c| Oid(c.to_owned()))
            .collect();

        Self {
            id,
            short_id,
            author_name,
            author_email,
            author_date,
            author_rel_date,
            committer_name,
            committer_email,
            committer_date,
            committer_rel_date,
            subject,
            body,
            parents,
            is_head,
            is_merge,
            branches,
            references,
            tags,
        }
    }

    pub fn from_repo(repo: &Repository, oid: &Oid) -> Option<Self> {
        to_commit(repo, oid)
    }
}

/// Return commit count with `--first-parent`
///
/// # Errors
///
/// Returns a [`PosixError`] if `working_dir` does not exist or `rev_range` is invalid.
pub fn history_length<S>(
    repo: &Repository,
    rev_range: &Vec<S>,
    paths: &[PathBuf],
) -> Result<usize, PosixError>
where
    S: AsRef<OsStr>,
{
    let mut git = repo.git();
    git.args(vec!["rev-list", "--first-parent", "--count"])
        .args(rev_range)
        .arg("--")
        .args(paths);
    let proc = git.output().expect("Failed to run rev-list");

    if proc.status.success() {
        let text = String::from_utf8_lossy(&proc.stdout).trim_end().to_owned();
        return Ok(text
            .parse::<usize>()
            .expect("Failed to parse commit length"));
    }

    let err = PosixError::from(proc);
    if err.code() == 128 {
        let msg = "No revisions match the given arguments".to_owned();
        return Err(PosixError::new(128, msg));
    }
    Err(err)
}

#[allow(unused_qualifications)]
pub fn commits_for_range<S>(
    repo: &Repository,
    rev_range: &Vec<S>,
    paths: &[PathBuf],
    skip: Option<usize>,
    max: Option<usize>,
) -> Vec<Commit>
where
    S: AsRef<OsStr> + std::fmt::Debug,
{
    let mut cmd = repo.git();
    cmd.arg("rev-list")
        .args(vec!["--date=human", "--first-parent", REV_FORMAT]);

    let tmp;
    if let Some(val) = skip {
        tmp = format!("--skip={}", val);
        cmd.arg(&tmp);
    }

    let tmp2;
    if let Some(val) = max {
        tmp2 = format!("--max-count={}", val);
        cmd.arg(&tmp2);
    }

    cmd.args(rev_range);

    if !paths.is_empty() {
        cmd.arg("--");
        for p in paths {
            cmd.arg(<PathBuf as AsRef<OsStr>>::as_ref(p));
        }
    }

    let proc = cmd.output().expect("Failed to run git-rev-list(1)");
    if proc.status.success() {
        let output = String::from_utf8_lossy(&proc.stdout);
        let lines = output.split('\u{1e}');
        let mut result: Vec<Commit> = Vec::new();
        for data in lines {
            if data.is_empty() || data == "\n" {
                break;
            }
            result.push(Commit::new(data));
        }
        return result;
    }
    log::error!(
        "Failed to find commits for range({:?}), with skip({:?}) / max({:?}) & path({})",
        rev_range,
        skip,
        max,
        paths.is_empty()
    );
    vec![]
}

#[must_use]
pub fn child_history(repo: &Repository, commit: &Commit, paths: &[PathBuf]) -> Vec<Commit> {
    let bellow = commit.parents.first().expect("Expected merge commit");
    let first_child = commit.parents.get(1).expect("Expected merge commit");
    let end = repo
        .merge_base(&[&bellow.0, &first_child.0])
        .expect("merge base shouldn't fail");

    let revision;
    if let Some(v) = &end {
        if v == &first_child.0 {
            revision = first_child.0.clone();
        } else {
            revision = format!("{}..{}", v, first_child.0);
        }
    } else {
        revision = first_child.0.clone();
    }
    commits_for_range(repo, &vec![revision], paths, None, None)
}

fn to_commit(repo: &Repository, oid: &Oid) -> Option<Commit> {
    let mut cmd = repo.git();
    cmd.args(["rev-list", "--date=human", REV_FORMAT, "-1", &oid.0]);
    let proc = cmd.output().expect("Failed to run git-rev-list(1)");
    proc.status.success().then(|| {
        let tmp = String::from_utf8_lossy(&proc.stdout);
        let lines: Vec<&str> = tmp.lines().collect();
        Commit::new(lines.get(1).expect("No second data line for commit"))
    })
}

pub fn parse_remote_url(input: &str) -> Option<Url> {
    // TODO handle upper case wording
    #[allow(clippy::case_sensitive_file_extension_comparisons)]
    let remote = if input.ends_with(".git") {
        input
            .strip_suffix(".git")
            .expect("Remote url ends with .git")
    } else {
        input
    };
    if let Ok(u) = Url::parse(remote) {
        return Some(u);
    }
    if remote.contains(':') {
        let tmp: Vec<&str> = remote.splitn(2, ':').collect();
        if tmp.len() == 2 {
            let domain = tmp[0];
            let path = tmp[1];
            let candidate = format!("ssh://{}/{}", domain, path);
            if let Ok(u) = Url::parse(&candidate) {
                return Some(u);
            }
        }
    }
    None
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;
    use std::path::PathBuf;

    use crate::commit::commits_for_range;
    use git_wrapper::Repository;

    use super::history_length;

    #[test]
    #[allow(clippy::print_stderr)]
    fn initial_commit() {
        let repo = Repository::default().unwrap();
        if repo.is_shallow() {
            eprintln!("Skipped test commit::test::initial_commit, because of shallow repo");
        } else {
            let paths: &[PathBuf] = &[];
            let result =
                commits_for_range(&repo, &vec!["a17989470af".to_owned()], paths, None, None);
            assert_eq!(result.len(), 1);
            let commit = &result[0];
            assert_eq!(commit.parents.len(), 0);
            assert!(!commit.is_merge);
        }
    }

    #[test]
    #[allow(clippy::print_stderr)]
    fn history_length_calc() {
        let repo = Repository::default().unwrap();
        if repo.is_shallow() {
            eprintln!("Skipped test commit::test::history_length_calc, because of shallow repo");
        } else {
            let paths: &[PathBuf] = &[];
            // Kind::IncludeReachable
            {
                let spec = &vec!["33f91157b4"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 4488, "Length of {:?}", spec);
            }

            // Kind::ExcludeReachable
            {
                let spec = &vec!["^33f91157b4"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 0, "Length of {:?}", spec);
            }

            // Kind::Range
            {
                let spec = &vec!["HEAD~20..HEAD"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 20, "Length of {:?}", spec);
            }

            // Kind::Merge
            {
                let spec = &vec!["HEAD~20...HEAD"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 20, "Length of {:?}", spec);
            }
            // Kind::IncludeOnlyParents
            {
                let spec = &vec!["33f91157b4^@"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 4487, "Length of {:?}", spec);
            }
            // Kind::ExcludeParents
            {
                let spec = &vec!["33f91157b4^!"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 1, "Length of {:?}", spec);
            }
        }
    }

    #[test]
    #[allow(clippy::print_stderr)]
    fn history_length_calc_path() {
        let repo = Repository::default().unwrap();
        if repo.is_shallow() {
            eprintln!(
                "Skipped test commit::test::history_length_calc_path, because of shallow repo"
            );
        } else {
            let paths: &[PathBuf] = &[PathBuf::from("file-expert/CHANGELOG.md")];
            // Kind::IncludeReachable
            {
                let spec = &vec!["33f91157b4"];
                let actual = history_length(&repo, spec, paths).unwrap();
                assert_eq!(actual, 13, "Length of {:?}", spec);
            }
        }
    }
}