example_git_internals

docs/sphinx/src/examples_generate/example_git_internals.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
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
"""Example from https://git-scm.com/book/en/v2/Git-Internals-Git-Objects"""

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

import inspect
import sys
from pathlib import Path
from textwrap import dedent

from git_dag.constants import COMMIT_DATE, DictStrStr
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:])


def step_create_blob() -> None:
    # pylint: disable=possibly-used-before-assignment
    GIT.run_general(
        f"echo 'test content' | {GIT.command_prefix} hash-object -w --stdin"
    )

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-B", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a blob from the content of a string

                echo 'test content' | git hash-object -w --stdin

            .. code-block:: console
                :caption: Output

                d670460b4b4aece5915caf5c68d12f560a9fe3e4
            """
        ),
    )


def step_create_blob_from_file() -> None:
    GIT.add({"test.txt": "version 1\n"})
    GIT.run_general(f"{GIT.command_prefix} hash-object -w test.txt")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-B", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a blob from the content of a file

                echo 'version 1' > test.txt
                git hash-object -w test.txt

            .. code-block:: console
                :caption: Output

                83baae61804e65cc73a7201a7252750c76066a30
            """
        ),
    )


def step_create_blob_from_modified_file() -> None:
    GIT.add({"test.txt": "version 2\n"})
    GIT.run_general(f"{GIT.command_prefix} hash-object -w test.txt")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-B", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a blob from the content of a modified file

                echo 'version 2' > test.txt
                git hash-object -w test.txt

            .. code-block:: console
                :caption: Output

                1f7a7a472abf3dd9643fd615f6da379c4acb3e3a
            """
        ),
    )


def step_create_tree_from_cached_blob() -> None:
    GIT.run_general(
        f"{GIT.command_prefix} update-index --add --cacheinfo 100644 "
        "83baae61804e65cc73a7201a7252750c76066a30 test.txt"
    )
    GIT.run_general(f"{GIT.command_prefix} write-tree")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "--trees-standalone", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a tree from a cached blob

                git update-index --add --cacheinfo 100644 83baae61804e65cc73a7201a7252750c76066a30 test.txt
                git write-tree

            .. code-block:: console
                :caption: Output

                d8329fc1cc938780ffdd9f94e0d364e0ea74f579
            """
        ),
    )


def step_create_tree_from_cached_blob_and_file() -> None:
    GIT.add({"new.txt": "new file\n"})
    GIT.run_general(
        f"{GIT.command_prefix} update-index --cacheinfo 100644 "
        "1f7a7a472abf3dd9643fd615f6da379c4acb3e3a test.txt"
    )
    GIT.run_general(f"{GIT.command_prefix} update-index --add new.txt")
    GIT.run_general(f"{GIT.command_prefix} write-tree")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "--trees-standalone", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a tree from a cached blob and a file

                echo 'new file' > new.txt
                git update-index --add --cacheinfo 100644 1f7a7a472abf3dd9643fd615f6da379c4acb3e3a test.txt
                git update-index --add new.txt
                git write-tree

            .. code-block:: console
                :caption: Output

                0155eb4229851634a0f03eb265b69f5a2d56f341
            """
        ),
    )


def step_create_tree_with_tree() -> None:
    GIT.run_general(f"{GIT.command_prefix} read-tree --prefix=bak d8329fc")
    GIT.run_general(f"{GIT.command_prefix} write-tree")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "--trees-standalone", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Create a tree containing another tree

                git read-tree --prefix=bak d8329fc
                git write-tree

            .. code-block:: console
                :caption: Output

                3c4e9cd789d88d8d89c1073707c3585e41b0e614
            """
        ),
    )


def step_add_commits() -> DictStrStr:
    commit1 = GIT.run_general(
        f"echo 'First commit' | {GIT.command_prefix} commit-tree d8329fc",
        env=GIT.env,
    )

    commit2 = GIT.run_general(
        f"echo 'Second commit' | {GIT.command_prefix} commit-tree 0155eb4 -p {commit1}",
        env=GIT.env,
    )

    commit3 = GIT.run_general(
        f"echo 'Third commit' | {GIT.command_prefix} commit-tree 3c4e9cd -p {commit2}",
        env=GIT.env,
    )

    SRG.results(
        inspect.stack()[0][3],
        show_args=[
            "-T",
            "-B",
            "-u",
            "-n",
            "0",
            "--trees-standalone",
            "--blobs-standalone",
        ],
        commands=dedent(
            f"""
            .. code-block:: bash
                :caption: Create three commits

                GIT_AUTHOR_NAME="First Last"
                GIT_AUTHOR_EMAIL="first.last.mail.com"
                GIT_COMMITTER_NAME="Nom Prenom"
                GIT_COMMITTER_EMAIL="nom.prenom@mail.com"

                # by fixing the author and committer dates as well, we have reproducible commit hashes
                GIT_AUTHOR_DATE="{COMMIT_DATE}"
                GIT_COMMITTER_DATE="{COMMIT_DATE}"

                SHA_FIRST_COMMIT=$(echo 'First commit' | git commit-tree d8329fc)
                SHA_SECOND_COMMIT=$(echo 'Second commit' | git commit-tree 0155eb4 -p $SHA_FIRST_COMMIT)
                SHA_THIRD_COMMIT=$(echo 'Third commit' | git commit-tree 3c4e9cd -p $SHA_SECOND_COMMIT)

                echo $SHA_FIRST_COMMIT
                echo $SHA_SECOND_COMMIT
                echo $SHA_THIRD_COMMIT

            .. code-block:: console
                :caption: Output

                {commit1}
                {commit2}
                {commit3}
            """
        ),
    )

    return {"commit1": commit1, "commit2": commit2, "commit3": commit3}


def step_add_tag(commits: DictStrStr) -> None:
    GIT.run_general(
        f'{GIT.command_prefix} tag first-commit -m "First commit" {commits["commit1"]}',
        env=GIT.env,
    )

    SRG.results(
        inspect.stack()[0][3],
        show_args=[
            "-T",
            "-B",
            "-t",
            "-u",
            "-n",
            "0",
            "--trees-standalone",
            "--blobs-standalone",
        ],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Add a tag to the first commit

                git tag first-commit -m "First commit" $SHA_FIRST_COMMIT
            """
        ),
    )


def step_add_branch(commits: DictStrStr) -> None:
    GIT.run_general(f"{GIT.command_prefix} branch main {commits['commit3']}")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "-H", "-l", "-t", "-u", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Add a branch

                git branch main $SHA_THIRD_COMMIT
            """
        ),
    )


def step_reset_main(commits: DictStrStr) -> None:
    GIT.run_general(f"{GIT.command_prefix} reset {commits['commit2']}")

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "-H", "-l", "-t", "-u", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Reset main

                git reset $SHA_SECOND_COMMIT
            """
        ),
    )


def step_detached_head(commits: DictStrStr) -> None:
    GIT.run_general(
        f"{GIT.command_prefix} checkout {commits['commit2']}",
        expected_stderr="You are in 'detached HEAD' state",
    )

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "-H", "-l", "-t", "-u", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Checkout a commit

                git checkout $SHA_SECOND_COMMIT
            """
        ),
    )


def step_add_lightweight_tag(commits: DictStrStr) -> None:
    GIT.run_general(f'{GIT.command_prefix} tag third-commit {commits["commit3"]}')

    SRG.results(
        inspect.stack()[0][3],
        show_args=["-T", "-B", "-H", "-l", "-t", "-u", "--blobs-standalone"],
        commands=dedent(
            """
            .. code-block:: bash
                :caption: Add a lightweight tag to the third commit

                git tag third-commit $SHA_THIRD_COMMIT
            """
        ),
    )


if __name__ == "__main__":
    SRG = StepResultsGenerator(example_name=EXAMPLE_NAME)
    GIT = GitCommandMutate(SRG.example_dir, date=COMMIT_DATE)
    GIT.init()

    step_create_blob()
    step_create_blob_from_file()
    step_create_blob_from_modified_file()
    step_create_tree_from_cached_blob()
    step_create_tree_from_cached_blob_and_file()
    step_create_tree_with_tree()
    commits_sha = step_add_commits()
    step_add_tag(commits_sha)
    step_add_branch(commits_sha)
    step_reset_main(commits_sha)
    step_detached_head(commits_sha)
    step_add_lightweight_tag(commits_sha)
    SRG.results(
        "final_dag_no_trees_and_blobs",
        show_args=["-H", "-l", "-t", "-u"],
        increment_step_number=False,
    )