File: implicit_upstream.t

package info (click to toggle)
git-autofixup 0.004007-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,876; sh: 58; makefile: 2
file content (338 lines) | stat: -rwxr-xr-x 8,767 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
#!/usr/bin/perl

# Test finding "upstream" commits. When making fixups we want to minimize the
# number of commits we need to look at, and avoid making "fixups" (ie fixup
# commits) for commits that could be reasonably expected to be pushed or for
# commits that are likely to be considered outside of the topic branch.

use strict;
use warnings FATAL => 'all';

use Test::More;

require './t/util.pl';
require './t/repo.pl';
require './git-autofixup';

Util::check_test_deps(4);
plan tests => 4;

# fast-forward from upstream
#
# Probably the most common case.
#
# o  upstream
#  \
#   o--o  topic
#
{
    my $name = 'fast-forward from upstream';

    my $wants = {
        fixup_log => <<'EOF',
fixup! commit1

diff --git a a
index 8737a60..ba81a56 100644
--- a
+++ a
@@ -1,3 +1,3 @@
-a1.1
-a2
+a1
+a2.1
 a3
EOF
        staged => '',
        unstaged => '',
    };

    eval {
        my $repo = Repo->new();

        $repo->create_commits({a => "a1\na2\na3\n"});
        my $upstream = $repo->current_commit_sha();

        $repo->switch_to_downstream_branch('topic');
        $repo->create_commits({a => "a1.1\na2\na3\n"});
        my $topic = $repo->current_commit_sha();

        $repo->write_change({a => "a1\na2.1\na3\n"});

        my $upstreams = Autofixup::find_merge_bases();
        my $ok = Util::upstreams_ok(want => [$upstream], got => $upstreams);

        if ($ok) {
            my $exit_code = $repo->autofixup();
            $ok &&= Util::exit_code_ok(want => 0, got => $exit_code);
            $ok &&= Util::repo_state_ok($repo, $topic, $wants);
        }

        ok($ok, $name);
    };
    if ($@) {
        diag($@);
        fail($name);
    }
}


# interactive rebase onto upstream, making a fixup from B for A
#
# o  upstream
#  \
#   A--B--C  topic
#
SKIP: {
    my $name = 'interactive rebase onto upstream';

    if (!Util::git_version_gte('1.7.8')) {
        skip 'GIT_SEQUENCE_EDITOR only available in 1.7.8+', 1;
    }

    my $wants = {
        fixup_log => <<'EOF',
fixup! commit1

diff --git a a
index 8737a60..ba81a56 100644
--- a
+++ a
@@ -1,3 +1,3 @@
-a1.1
-a2
+a1
+a2.1
 a3
EOF
        staged => '',
        unstaged => '',
    };

    eval {
        my $repo = Repo->new();

        # upstream (commit0)
        $repo->create_commits({a => "a1\na2\na3\n"});
        my $upstream = $repo->current_commit_sha();

        # A (commit1)
        $repo->switch_to_downstream_branch('topic');
        $repo->create_commits({a => "a1.1\na2\na3\n"});
        my $topic = $repo->current_commit_sha();

        # B (commit2)
        $repo->create_commits({a => "a1\na2.1\na3\n"});

        # C
        $repo->create_commits({b => "b1\n"});

        # Start an interactive rebase to edit commit B (which'll have commit2
        # in its message).
        local $ENV{GIT_SEQUENCE_EDITOR} = q(perl -i -pe "/commit2/ && s/^pick/edit/");
        Util::run("git rebase -i $upstream 2>/dev/null 1>&2");
        Util::run(qw(git reset HEAD^));

        my $upstreams = Autofixup::find_merge_bases();
        my $ok = Util::upstreams_ok(want => [$upstream], got => $upstreams);
        if ($ok) {
            my $exit_code = $repo->autofixup();
            $ok &&= Util::exit_code_ok(want => 0, got => $exit_code);
            $ok &&= Util::repo_state_ok($repo, $topic, $wants);
        }
        ok($ok, $name);
    };
    if ($@) {
        diag($@);
        fail($name);
    }
}


# fork-point and merge-base are different
#
# Here the upstream commit that topic originally diverged from is different
# from the first ancestor that currently belongs to both topic and upstream,
# due to upstream being rewound and rebuilt. We don't want to make fixups for
# the fork-point since the user probably doesn't consider it part of the topic
# branch at a conceptual level and by default `git rebase` excludes the
# fork-point from the set of commits to be rewritten.
#
# B1--o  upstream
#  \
#   B0  fork-point: was previously part of upstream
#    \
#     T0  topic
#
SKIP: {
    my $name = 'fork-point and merge-base are different';

    if (!Util::git_version_gte('1.9.0')) {
        skip 'merge-base --fork-point only available in 1.9.0+', 1;
    }

    my $wants = {
        fixup_log => <<'EOF',
fixup! commit2

diff --git a a
index 8737a60..472f448 100644
--- a
+++ a
@@ -1,3 +1,3 @@
 a1.1
-a2
+a2.1
 a3
EOF
        staged => '',
        unstaged => '',
    };

    eval {
        my $repo = Repo->new();

        # upstream
        #
        # Create a commit to use as the fork-point for the topic branch, save
        # the SHA, then amend it so that the fork-point is no longer reachable
        # from master and create another commit on top.
        $repo->create_commits({a => "a1\na2\na3\n"});
        my $fork_point = $repo->current_commit_sha();
        Util::run(qw(git commit --amend -m), 'commit0, reworded');  # B1
        $repo->create_commits({b => "b1\n"});  # o (commit1)

        # topic
        Util::run(qw(git checkout -q -b topic), $fork_point);
        $repo->set_upstream('topic', 'master');
        $repo->create_commits({a => "a1.1\na2\na3\n"});

        $repo->write_change({a => "a1.1\na2.1\na3\n"});
        my $topic = $repo->current_commit_sha();

        my $upstreams = Autofixup::find_merge_bases();
        my $ok = Util::upstreams_ok(want => [$fork_point], got => $upstreams);
        if ($ok) {
            my $exit_code = $repo->autofixup();
            $ok &&= Util::exit_code_ok(want => 0, got => $exit_code);
            $ok &&= Util::repo_state_ok($repo, $topic, $wants);
        }
        ok($ok, $name);
    };
    if ($@) {
        diag($@);
        fail($name);
    }
}


# criss-cross merge and gc'd fork-point reflog
#
# Here's one way to get a criss-cross merge. If you have two branches (A and B,
# here) that include the same merge commit, M0:
#
# C1-M0  A, B
#   /
# C2
#
# And then you amend that merge commit from one of the branches (A, in this
# case), creating M1, you get the following topology. The important implication
# for us is that commits 1 and 2 are both equally good merge bases of A and B,
# and we don't want to create fixups for either of them or their ancestors.
# (Here 'X' represents overlapping graph edges, not another commit.)
#
# C1---M1  A
#   \ /
#    X
#   / \
# C2---M0  B
#
# For this test we'll also amend B's merge commit and garbage-collect the
# reflog so that M0 isn't simply used as B's fork-point from its tracking
# branch A, forcing git-autofixup to fall back on the merge-bases C1 and C2.
#
# C1---M1  A
#   \ /
#    X
#   / \
# C2---M2---o  B
#
{
    my $name = "criss-cross merge and gc'd fork point reflog";

    my $wants = {
        fixup_log => <<'EOF',
fixup! commit2

diff --git a a
index 8737a60..472f448 100644
--- a
+++ a
@@ -1,3 +1,3 @@
 a1.1
-a2
+a2.1
 a3
diff --git b b
index 0ef8a8e..b1710a1 100644
--- b
+++ b
@@ -1,3 +1,3 @@
 b1.1
-b2
+b2.1
 b3
EOF
        staged => '',
        unstaged => '',
    };

    eval {
        my $repo = Repo->new();

        # C1
        Util::run(qw(git checkout -q -b A));
        $repo->create_commits({a => "a1\na2\na3\n"});
        my $c1 = $repo->current_commit_sha();

        # C2
        Util::run(qw(git checkout -q -b B master));
        $repo->set_upstream('B', 'A');
        $repo->create_commits({b => "b1\nb2\nb3\n"});
        my $c2 = $repo->current_commit_sha();

        # Merge A and B, so they're both pointing to the same merge commit.
        Util::run(qw(git merge --no-ff), '-m' => 'Merge A into B', 'A');  # M0
        Util::run(qw(git checkout -q A));
        Util::run(qw(git merge --ff-only B));  # fast-forward to M0

        # Then ammend the merge commits for both branches and gc the reflog so
        # git can't tell what the original fork-point of B from A is.
        Util::run(qw(git commit --amend -m), 'Merge A into B, reworded for A');  # M1
        Util::run(qw(git checkout -q B));
        Util::run(qw(git commit --amend -m), 'Merge A into B, reworded for B');  # M2
        Util::run('git -c gc.reflogExpire=now gc 2>/dev/null');

        # topic
        $repo->create_commits({a => "a1.1\na2\na3\n", b => "b1.1\nb2\nb3\n"});
        my $topic = $repo->current_commit_sha();

        $repo->write_change({a => "a1.1\na2.1\na3\n", b => "b1.1\nb2.1\nb3\n"});

        my @upstreams_got = sort(Autofixup::find_merge_bases());
        my @upstreams_want = sort $c1, $c2;
        my $ok = Util::upstreams_ok(want => \@upstreams_want, got => \@upstreams_got);

        if ($ok) {
            my $exit_code = $repo->autofixup();
            $ok &&= Util::exit_code_ok(want => 0, got => $exit_code);
            $ok &&= Util::repo_state_ok($repo, $topic, $wants);
        }

        ok($ok, $name);
    };
    if ($@) {
        diag($@);
        fail($name);
    }
}