File: test_fixup.py

package info (click to toggle)
git-revise 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: python: 2,505; makefile: 16
file content (384 lines) | stat: -rw-r--r-- 10,398 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
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
# pylint: skip-file

from conftest import *
from gitrevise.utils import commit_range
from gitrevise.todo import StepKind, build_todos, autosquash_todos
import os


@pytest.fixture
def basic_repo(repo):
    bash(
        """
        cat <<EOF >file1
        Hello, World!
        How are things?
        EOF
        git add file1
        git commit -m "commit1"

        cat <<EOF >file1
        Hello, World!
        Oops, gotta add a new line!
        How are things?
        EOF
        git add file1
        git commit -m "commit2"
        """
    )
    return repo


def fixup_helper(repo, flags, target, message=None):
    with fixup_helper_editor(repo=repo, flags=flags, target=target, message=message):
        pass


@contextmanager
def fixup_helper_editor(repo, flags, target, message=None):
    old = repo.get_commit(target)
    assert old.persisted

    bash(
        """
        echo "extra line" >> file1
        git add file1
        """
    )

    with editor_main(flags + [target]) as ed:
        yield ed

    new = repo.get_commit(target)
    assert old != new, "commit was modified"
    assert old.parents() == new.parents(), "parents are unchanged"

    assert old.tree() != new.tree(), "tree is changed"

    if message is None:
        assert new.message == old.message, "message should not be changed"
    else:
        assert new.message == message.encode(), "message set correctly"

    assert new.persisted, "commit persisted to disk"
    assert new.author == old.author, "author is unchanged"
    assert new.committer == repo.default_committer, "committer is updated"


def test_fixup_head(basic_repo):
    fixup_helper(basic_repo, [], "HEAD")


def test_fixup_nonhead(basic_repo):
    fixup_helper(basic_repo, [], "HEAD~")


def test_fixup_head_msg(basic_repo):
    fixup_helper(
        basic_repo,
        ["-m", "fixup_head test", "-m", "another line"],
        "HEAD",
        "fixup_head test\n\nanother line\n",
    )


def test_fixup_nonhead_msg(basic_repo):
    fixup_helper(
        basic_repo,
        ["-m", "fixup_nonhead test", "-m", "another line"],
        "HEAD~",
        "fixup_nonhead test\n\nanother line\n",
    )


def test_fixup_head_editor(basic_repo):
    old = basic_repo.get_commit("HEAD")
    newmsg = "fixup_head_editor test\n\nanother line\n"

    with fixup_helper_editor(basic_repo, ["-e"], "HEAD", newmsg) as ed:
        with ed.next_file() as f:
            assert f.startswith(old.message)
            f.replace_dedent(newmsg)


def test_fixup_nonhead_editor(basic_repo):
    old = basic_repo.get_commit("HEAD~")
    newmsg = "fixup_nonhead_editor test\n\nanother line\n"

    with fixup_helper_editor(basic_repo, ["-e"], "HEAD~", newmsg) as ed:
        with ed.next_file() as f:
            assert f.startswith(old.message)
            f.replace_dedent(newmsg)


def test_fixup_nonhead_conflict(basic_repo):
    bash('echo "conflict" > file1')
    bash("git add file1")

    old = basic_repo.get_commit("HEAD~")
    assert old.persisted

    with editor_main(["HEAD~"], input=b"y\ny\ny\ny\n") as ed:
        with ed.next_file() as f:
            assert f.equals_dedent(
                f"""\
                <<<<<<< {os.sep}file1 (new parent): commit1
                Hello, World!
                How are things?
                =======
                conflict
                >>>>>>> {os.sep}file1 (current): <git index>
                """
            )
            f.replace_dedent("conflict1\n")

        with ed.next_file() as f:
            assert f.equals_dedent(
                f"""\
                <<<<<<< {os.sep}file1 (new parent): commit1
                conflict1
                =======
                Hello, World!
                Oops, gotta add a new line!
                How are things?
                >>>>>>> {os.sep}file1 (current): commit2
                """
            )
            f.replace_dedent("conflict2\n")

    new = basic_repo.get_commit("HEAD~")
    assert new.persisted
    assert new != old


def test_autosquash_nonhead(repo):
    bash(
        """
        echo "hello, world" > file1
        git add file1
        git commit -m "commit one"

        echo "second file" > file2
        git add file2
        git commit -m "commit two"

        echo "new line!" >> file1
        git add file1
        git commit -m "commit three"

        echo "extra line" >> file2
        git add file2
        git commit --fixup=HEAD~
        """
    )

    old = repo.get_commit("HEAD~~")
    assert old.persisted

    main(["--autosquash", str(old.parent().oid)])

    new = repo.get_commit("HEAD~")
    assert old != new, "commit was modified"
    assert old.parents() == new.parents(), "parents are unchanged"

    assert old.tree() != new.tree(), "tree is changed"

    assert new.message == old.message, "message should not be changed"

    assert new.persisted, "commit persisted to disk"
    assert new.author == old.author, "author is unchanged"
    assert new.committer == repo.default_committer, "committer is updated"

    file1 = new.tree().entries[b"file1"].blob().body
    assert file1 == b"hello, world\n"
    file2 = new.tree().entries[b"file2"].blob().body
    assert file2 == b"second file\nextra line\n"


def test_fixup_of_fixup(repo):
    bash(
        """
        echo "hello, world" > file1
        git add file1
        git commit -m "commit one"

        echo "second file" > file2
        git add file2
        git commit -m "commit two"

        echo "new line!" >> file1
        git add file1
        git commit -m "commit three"

        echo "extra line" >> file2
        git add file2
        git commit --fixup=HEAD~

        echo "even more" >> file2
        git add file2
        git commit --fixup=HEAD
        """
    )

    old = repo.get_commit("HEAD~~~")
    assert old.persisted

    main(["--autosquash", str(old.parent().oid)])

    new = repo.get_commit("HEAD~")
    assert old != new, "commit was modified"
    assert old.parents() == new.parents(), "parents are unchanged"

    assert old.tree() != new.tree(), "tree is changed"

    assert new.message == old.message, "message should not be changed"

    assert new.persisted, "commit persisted to disk"
    assert new.author == old.author, "author is unchanged"
    assert new.committer == repo.default_committer, "committer is updated"

    file1 = new.tree().entries[b"file1"].blob().body
    assert file1 == b"hello, world\n"
    file2 = new.tree().entries[b"file2"].blob().body
    assert file2 == b"second file\nextra line\neven more\n"


def test_fixup_by_id(repo):
    bash(
        """
        echo "hello, world" > file1
        git add file1
        git commit -m "commit one"

        echo "second file" > file2
        git add file2
        git commit -m "commit two"

        echo "new line!" >> file1
        git add file1
        git commit -m "commit three"

        echo "extra line" >> file2
        git add file2
        git commit -m "fixup! $(git rev-parse HEAD~)"
        """
    )

    old = repo.get_commit("HEAD~~")
    assert old.persisted

    main(["--autosquash", str(old.parent().oid)])

    new = repo.get_commit("HEAD~")
    assert old != new, "commit was modified"
    assert old.parents() == new.parents(), "parents are unchanged"

    assert old.tree() != new.tree(), "tree is changed"

    assert new.message == old.message, "message should not be changed"

    assert new.persisted, "commit persisted to disk"
    assert new.author == old.author, "author is unchanged"
    assert new.committer == repo.default_committer, "committer is updated"

    file1 = new.tree().entries[b"file1"].blob().body
    assert file1 == b"hello, world\n"
    file2 = new.tree().entries[b"file2"].blob().body
    assert file2 == b"second file\nextra line\n"


def test_fixup_order(repo):
    bash(
        """
        git commit --allow-empty -m 'old'
        git commit --allow-empty -m 'target commit'
        git commit --allow-empty -m 'first fixup'  --fixup=HEAD
        git commit --allow-empty -m 'second fixup' --fixup=HEAD~
        """
    )

    old = repo.get_commit("HEAD~3")
    assert old.persisted
    tip = repo.get_commit("HEAD")
    assert tip.persisted

    todos = build_todos(commit_range(old, tip), index=None)
    [target, first, second] = autosquash_todos(todos)

    assert b"target commit" in target.commit.message
    assert b"first fixup" in first.commit.message
    assert b"second fixup" in second.commit.message


def test_fixup_order_transitive(repo):
    bash(
        """
        git commit --allow-empty -m 'old'
        git commit --allow-empty -m 'target commit'
        git commit --allow-empty -m '1.0' --fixup=HEAD
        git commit --allow-empty -m '1.1' --fixup=HEAD
        git commit --allow-empty -m '2.0' --fixup=HEAD~2
        """
    )

    old = repo.get_commit("HEAD~4")
    assert old.persisted
    tip = repo.get_commit("HEAD")
    assert tip.persisted

    todos = build_todos(commit_range(old, tip), index=None)
    [target, a, b, c] = autosquash_todos(todos)

    assert b"target commit" in target.commit.message
    assert b"1.0" in a.commit.message
    assert b"1.1" in b.commit.message
    assert b"2.0" in c.commit.message


def test_fixup_order_cycle(repo):
    bash(
        """
        git commit --allow-empty -m 'old'
        git commit --allow-empty -m 'fixup! fixup!' # Cannot fixup self.
        git commit --allow-empty -m 'fixup! future commit' # Cannot fixup future commit.
        git commit --allow-empty -m 'future commit'
        """
    )

    old = repo.get_commit("HEAD~3")
    assert old.persisted
    tip = repo.get_commit("HEAD")
    assert tip.persisted

    todos = build_todos(commit_range(old, tip), index=None)

    new_todos = autosquash_todos(todos)
    assert len(new_todos) == 3
    assert all(step.kind == StepKind.PICK for step in new_todos)


def test_autosquash_multiline_summary(repo):
    bash(
        """
        git commit --allow-empty -m 'initial commit'
        git commit --allow-empty -m 'multi
        line
        summary

        body goes here
        '

        echo >file
        git add file
        git commit --fixup=HEAD
        """
    )

    old = repo.get_commit("HEAD~")
    assert old.persisted

    main(["--autosquash"])

    new = repo.get_commit("HEAD")
    assert old != new, "commit was modified"
    assert old.parents() == new.parents(), "parents are unchanged"