File: test_gitignores.rs

package info (click to toggle)
jujutsu 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,756 kB
  • sloc: sh: 283; makefile: 31
file content (155 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download
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
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Write as _;

use testutils::git;

use crate::common::TestEnvironment;

#[test]
fn test_gitignores() {
    let test_env = TestEnvironment::default();
    let work_dir = test_env.work_dir("repo");
    git::init(work_dir.root());
    work_dir
        .run_jj(["git", "init", "--git-repo", "."])
        .success();

    // Say in core.excludesFiles that we don't want file1, file2, or file3
    let mut file = std::fs::OpenOptions::new()
        .append(true)
        .open(work_dir.root().join(".git").join("config"))
        .unwrap();
    // Put the file in "~/my-ignores" so we also test that "~" expands to "$HOME"
    file.write_all(b"[core]\nexcludesFile=~/my-ignores\n")
        .unwrap();
    drop(file);
    std::fs::write(
        test_env.home_dir().join("my-ignores"),
        "file1\nfile2\nfile3",
    )
    .unwrap();

    // Say in .git/info/exclude that we actually do want file2 and file3
    let mut file = std::fs::OpenOptions::new()
        .append(true)
        .open(work_dir.root().join(".git").join("info").join("exclude"))
        .unwrap();
    file.write_all(b"!file2\n!file3").unwrap();
    drop(file);

    // Say in .gitignore (in the working copy) that we actually do not want file2
    // (again)
    work_dir.write_file(".gitignore", "file2");

    // Writes some files to the working copy
    work_dir.write_file("file0", "contents");
    work_dir.write_file("file1", "contents");
    work_dir.write_file("file2", "contents");
    work_dir.write_file("file3", "contents");

    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    A .gitignore
    A file0
    A file3
    [EOF]
    ");
}

#[test]
fn test_gitignores_relative_excludes_file_path() {
    let test_env = TestEnvironment::default();
    let work_dir = test_env.work_dir("repo");
    test_env
        .run_jj_in(".", ["git", "init", "--colocate", "repo"])
        .success();

    let mut file = std::fs::OpenOptions::new()
        .append(true)
        .open(work_dir.root().join(".git").join("config"))
        .unwrap();
    file.write_all(b"[core]\nexcludesFile=../my-ignores\n")
        .unwrap();
    drop(file);
    std::fs::write(test_env.env_root().join("my-ignores"), "ignored\n").unwrap();

    work_dir.write_file("ignored", "");
    work_dir.write_file("not-ignored", "");

    // core.excludesFile should be resolved relative to the workspace root, not
    // to the cwd.
    let sub_dir = work_dir.create_dir("sub");
    let output = sub_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output.normalize_backslash(), @r"
    A ../not-ignored
    [EOF]
    ");
    let output = test_env.run_jj_in(".", ["-Rrepo", "diff", "-s"]);
    insta::assert_snapshot!(output.normalize_backslash(), @r"
    A repo/not-ignored
    [EOF]
    ");
}

#[test]
fn test_gitignores_ignored_file_in_target_commit() {
    let test_env = TestEnvironment::default();
    let work_dir = test_env.work_dir("repo");
    git::init(work_dir.root());
    work_dir
        .run_jj(["git", "init", "--git-repo", "."])
        .success();

    // Create a commit with file "ignored" in it
    work_dir.write_file("ignored", "committed contents\n");
    work_dir
        .run_jj(["bookmark", "create", "-r@", "with-file"])
        .success();
    let target_commit_id = work_dir
        .run_jj(["log", "--no-graph", "-T=commit_id", "-r=@"])
        .success()
        .stdout
        .into_raw();

    // Create another commit where we ignore that path
    work_dir.run_jj(["new", "root()"]).success();
    work_dir.write_file("ignored", "contents in working copy\n");
    work_dir.write_file(".gitignore", ".gitignore\nignored\n");

    // Update to the commit with the "ignored" file
    let output = work_dir.run_jj(["edit", "with-file"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: qpvuntsm 3cf51c1a with-file | (no description set)
    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)
    Added 1 files, modified 0 files, removed 0 files
    Warning: 1 of those updates were skipped because there were conflicting changes in the working copy.
    Hint: Inspect the changes compared to the intended target with `jj diff --from 3cf51c1acf24`.
    Discard the conflicting changes with `jj restore --from 3cf51c1acf24`.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "--git", "--from", &target_commit_id]);
    insta::assert_snapshot!(output, @r"
    diff --git a/ignored b/ignored
    index 8a69467466..4d9be5127b 100644
    --- a/ignored
    +++ b/ignored
    @@ -1,1 +1,1 @@
    -committed contents
    +contents in working copy
    [EOF]
    ");
}