example_fetch_merge_rebase

docs/sphinx/src/examples_generate/example_fetch_merge_rebase.py
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
"""Example of fetch with merge or rebase."""

# pylint: disable=missing-function-docstring,line-too-long,wrong-import-position,wrong-import-order

import inspect
import sys
from pathlib import Path
from typing import Any

from git_dag.constants import COMMIT_DATE
from git_dag.git_commands import GitCommandMutate

sys.path.append(str(Path(__file__).parent))
from common_utils import StepResultsGenerator

EXAMPLE_NAME = "_".join(Path(__file__).stem.split("_")[1:])

ELENA_CREDENTIALS = "Elena Coder <elena.coder@mail.com>"
MARINA_CREDENTIALS = "Marina Coder <marina.coder@mail.com>"

SERVER_DIR = "server"
ELENA_DIR = "elena"
MARINA_DIR = "marina"
SHOW_ARGS = ["-m", "1", "-l", "-r", "-H", "-u"]


def bare_repo_and_two_clones() -> dict[str, Any]:
    repo_server_path = SRG.example_dir / SERVER_DIR
    repo_elena_path = SRG.example_dir / ELENA_DIR
    repo_marina_path = SRG.example_dir / MARINA_DIR

    repo_server_path.mkdir(parents=True)
    repo_elena_path.mkdir(parents=True)
    repo_marina_path.mkdir(parents=True)

    GitCommandMutate(repo_server_path).init(bare=True)

    git_elena = GitCommandMutate(
        repo_elena_path,
        author=ELENA_CREDENTIALS,
        committer=ELENA_CREDENTIALS,
        date=COMMIT_DATE,
        evolving_date=True,
    )
    git_elena.clone_from_local(repo_server_path, repo_elena_path)

    git_elena.init_remote_head()
    git_elena.cm(["A", "B", "C"])
    git_elena.push()

    git_marina = GitCommandMutate(
        repo_marina_path,
        author=MARINA_CREDENTIALS,
        committer=MARINA_CREDENTIALS,
        date=git_elena.date,
        evolving_date=True,
    )
    git_marina.clone_from_local(repo_server_path, repo_marina_path)
    git_marina.pull()
    git_marina.cm(["D", "E"])
    git_marina.push()

    git_elena.cm(["F", "G"])
    git_elena.fetch()

    return {
        "example_path": SRG.example_dir,
        "repo_server_path": repo_server_path,
        "repo_elena_path": repo_elena_path,
        "repo_marina_path": repo_marina_path,
        "git_elena": git_elena,
        "git_marina": git_marina,
    }


def start_new_repo(step_number: int = 1) -> StepResultsGenerator:
    return StepResultsGenerator(example_name=EXAMPLE_NAME, step_number=step_number)


def example_initial_state() -> None:
    args = bare_repo_and_two_clones()

    SRG.results(
        inspect.stack()[0][3] + "-marina",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_marina_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-server",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_server_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-elena",
        show_args=SHOW_ARGS + ["-R", "..@{u}"],
        repo_dir=args["repo_elena_path"],
    )


def example_merge() -> None:
    args = bare_repo_and_two_clones()

    args["git_elena"].mg("origin/main")
    args["git_elena"].push()
    args["git_marina"].pull()

    SRG.results(
        inspect.stack()[0][3] + "-marina",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_marina_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-server",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_server_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-elena",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_elena_path"],
    )


def example_rebase() -> None:
    args = bare_repo_and_two_clones()

    args["git_elena"].rebase("origin/main")
    args["git_elena"].push()
    args["git_marina"].pull()

    SRG.results(
        inspect.stack()[0][3] + "-marina",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_marina_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-server",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_server_path"],
    )

    SRG.results(
        inspect.stack()[0][3] + "-elena",
        show_args=SHOW_ARGS,
        repo_dir=args["repo_elena_path"],
    )


SRG = start_new_repo()
example_initial_state()

SRG = start_new_repo(SRG.step_number)
example_merge()

SRG = start_new_repo(SRG.step_number)
example_rebase()