File: test_git.py

package info (click to toggle)
setuptools-scm 9.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,108 kB
  • sloc: python: 7,068; sh: 18; makefile: 7
file content (863 lines) | stat: -rw-r--r-- 29,470 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
from __future__ import annotations

import contextlib
import os
import shutil
import subprocess
import sys

from datetime import date
from datetime import datetime
from datetime import timezone
from os.path import join as opj
from pathlib import Path
from textwrap import dedent
from typing import Generator
from unittest.mock import Mock
from unittest.mock import patch

import pytest

import setuptools_scm._file_finders

from setuptools_scm import Configuration
from setuptools_scm import NonNormalizedVersion
from setuptools_scm import git
from setuptools_scm._file_finders.git import git_find_files
from setuptools_scm._run_cmd import CommandNotFoundError
from setuptools_scm._run_cmd import CompletedProcess
from setuptools_scm._run_cmd import has_command
from setuptools_scm._run_cmd import run
from setuptools_scm.git import archival_to_version
from setuptools_scm.version import format_version

from .conftest import DebugMode
from .wd_wrapper import WorkDir

pytestmark = pytest.mark.skipif(
    not has_command("git", warn=False), reason="git executable not found"
)


def setup_git_wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch | None = None) -> WorkDir:
    """Set up a WorkDir with git initialized and configured for testing."""
    if monkeypatch:
        monkeypatch.delenv("HOME", raising=False)
    wd("git init")
    wd("git config user.email test@example.com")
    wd('git config user.name "a test"')
    wd.add_command = "git add ."
    wd.commit_command = "git commit -m test-{reason}"
    return wd


@pytest.fixture(name="wd")
def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode) -> WorkDir:
    debug_mode.disable()
    setup_git_wd(wd, monkeypatch)
    debug_mode.enable()
    return wd


@pytest.mark.parametrize(
    ("given", "tag", "number", "node", "dirty"),
    [
        ("3.3.1-rc26-0-g9df187b", "3.3.1-rc26", 0, "g9df187b", False),
        ("17.33.0-rc-17-g38c3047c0", "17.33.0-rc", 17, "g38c3047c0", False),
    ],
)
def test_parse_describe_output(
    given: str, tag: str, number: int, node: str, dirty: bool
) -> None:
    parsed = git._git_parse_describe(given)
    assert parsed == (tag, number, node, dirty)


def test_root_relative_to(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
    p = wd.cwd.joinpath("sub/package")
    p.mkdir(parents=True)
    p.joinpath("setup.py").write_text(
        """from setuptools import setup
setup(use_scm_version={"root": "../..",
                       "relative_to": __file__})
""",
        encoding="utf-8",
    )
    res = run([sys.executable, "setup.py", "--version"], p)
    assert res.stdout == "0.1.dev0+d20090213"


def test_root_search_parent_directories(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
    p = wd.cwd.joinpath("sub/package")
    p.mkdir(parents=True)
    p.joinpath("setup.py").write_text(
        """from setuptools import setup
setup(use_scm_version={"search_parent_directories": True})
""",
        encoding="utf-8",
    )
    res = run([sys.executable, "setup.py", "--version"], p)
    assert res.stdout == "0.1.dev0+d20090213"


def test_git_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("PATH", str(wd.cwd / "not-existing"))

    wd.write("pyproject.toml", "[tool.setuptools_scm]")
    with pytest.raises(CommandNotFoundError, match=r"git"):
        git.parse(wd.cwd, Configuration(), git.DEFAULT_DESCRIBE)

    assert wd.get_version(fallback_version="1.0") == "1.0"


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/298")
@pytest.mark.issue(403)
def test_file_finder_no_history(wd: WorkDir, caplog: pytest.LogCaptureFixture) -> None:
    file_list = git_find_files(str(wd.cwd))
    assert file_list == []

    assert "listing git files failed - pretending there aren't any" in caplog.text


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/281")
def test_parse_call_order(wd: WorkDir) -> None:
    git.parse(str(wd.cwd), Configuration(), git.DEFAULT_DESCRIBE)


def sudo_devnull(
    args: list[str | os.PathLike[str]], check: bool = False
) -> subprocess.CompletedProcess[bytes]:
    """shortcut to run sudo with non-interactive input"""
    return subprocess.run(
        ["sudo", *args],
        stdin=subprocess.DEVNULL,
        check=check,
    )


@contextlib.contextmanager
def break_folder_permissions(path: Path) -> Generator[None, None, None]:
    """break the permissions of a folder for a while"""
    if not shutil.which("sudo"):
        pytest.skip("sudo executable not found")
    original_stat = path.stat()

    proc = sudo_devnull(["chown", "-R", "12345", path])
    if proc.returncode != 0:
        pytest.xfail("Failed to change ownership, is passwordless sudo available?")

    try:
        sudo_devnull(["chmod", "a+r", path], check=True)
        sudo_devnull(["chgrp", "-R", "12345", path], check=True)
        yield
    finally:
        # Restore the ownership
        sudo_devnull(["chown", "-R", str(original_stat.st_uid), path], check=True)
        sudo_devnull(["chgrp", "-R", str(original_stat.st_gid), path], check=True)


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/707")
def test_not_owner(wd: WorkDir) -> None:
    with break_folder_permissions(wd.cwd):
        assert git.parse(str(wd.cwd), Configuration())


def test_version_from_git(wd: WorkDir) -> None:
    assert wd.get_version() == "0.1.dev0+d20090213"

    parsed = git.parse(str(wd.cwd), Configuration(), git.DEFAULT_DESCRIBE)
    assert parsed is not None
    assert parsed.branch in ("master", "main")

    wd.commit_testfile()
    assert wd.get_version().startswith("0.1.dev1+g")
    assert not wd.get_version().endswith("1-")

    wd("git tag v0.1")
    assert wd.get_version() == "0.1"

    wd.write("test.txt", "test2")
    assert wd.get_version().startswith("0.2.dev0+g")

    wd.commit_testfile()
    assert wd.get_version().startswith("0.2.dev1+g")

    wd("git tag version-0.2")
    assert wd.get_version().startswith("0.2")

    wd.commit_testfile()
    wd("git tag version-0.2.post210+gbe48adfpost3+g0cc25f2")
    with pytest.warns(
        UserWarning, match="tag '.*' will be stripped of its suffix '.*'"
    ):
        assert wd.get_version().startswith("0.2")

    wd.commit_testfile()
    wd("git tag 17.33.0-rc")
    assert wd.get_version() == "17.33.0rc0"

    # custom normalization
    assert wd.get_version(normalize=False) == "17.33.0-rc"
    assert wd.get_version(version_cls=NonNormalizedVersion) == "17.33.0-rc"
    assert (
        wd.get_version(version_cls="setuptools_scm.NonNormalizedVersion")
        == "17.33.0-rc"
    )


setup_py_with_normalize: dict[str, str] = {
    "false": """
        from setuptools import setup
        setup(use_scm_version={'normalize': False, 'write_to': 'VERSION.txt'})
        """,
    "with_created_class": """
from setuptools import setup

class MyVersion:
    def __init__(self, tag_str: str):
        self.version = tag_str

    def __repr__(self):
        return self.version

    @property
    def public(self):
        return self.version.split('+')[0]

    @property
    def local(self):
        if '+' in self.version:
            return self.version.split('+', 1)[1]
        return None

setup(use_scm_version={'version_cls': MyVersion, 'write_to': 'VERSION.txt'})
        """,
    "with_named_import": """
        from setuptools import setup
        setup(use_scm_version={
            'version_cls': 'setuptools_scm.NonNormalizedVersion',
            'write_to': 'VERSION.txt'
        })
        """,
}


@pytest.mark.parametrize(
    "setup_py_txt",
    [pytest.param(text, id=key) for key, text in setup_py_with_normalize.items()],
)
def test_git_version_unnormalized_setuptools(
    setup_py_txt: str, wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    """
    Test that when integrating with setuptools without normalization,
    the version is not normalized in write_to files,
    but still normalized by setuptools for the final dist metadata.
    """
    # monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
    monkeypatch.chdir(wd.cwd)
    wd.write("setup.py", dedent(setup_py_txt))

    # do git operations and tag
    wd.commit_testfile()
    wd("git tag 17.33.0-rc1")

    # setuptools still normalizes using packaging.Version (removing the dash)
    res = wd([sys.executable, "setup.py", "--version"])
    assert res == "17.33.0rc1"

    # but the version tag in the file is non-normalized (with the dash)
    assert wd.cwd.joinpath("VERSION.txt").read_text(encoding="utf-8") == "17.33.0-rc1"


@pytest.mark.issue(179)
def test_unicode_version_scheme(wd: WorkDir) -> None:
    scheme = b"guess-next-dev".decode("ascii")
    assert wd.get_version(version_scheme=scheme)


@pytest.mark.issue(108)
@pytest.mark.issue(109)
def test_git_worktree(wd: WorkDir) -> None:
    wd.write("test.txt", "test2")
    # untracked files dont change the state
    assert wd.get_version() == "0.1.dev0+d20090213"

    wd("git add test.txt")
    assert wd.get_version().startswith("0.1.dev0+d")


@pytest.mark.issue(86)
@pytest.mark.parametrize("today", [False, True])
def test_git_dirty_notag(
    today: bool, wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    if today:
        monkeypatch.delenv("SOURCE_DATE_EPOCH", raising=False)
    wd.commit_testfile()
    wd.write("test.txt", "test2")
    wd("git add test.txt")
    version = wd.get_version()

    if today:
        # the date on the tag is in UTC
        tag = datetime.now(timezone.utc).date().strftime(".d%Y%m%d")
    else:
        tag = ".d20090213"
    assert version.startswith("0.1.dev1+g")
    assert version.endswith(tag)


@pytest.mark.issue(193)
@pytest.mark.xfail(reason="sometimes relative path results")
def test_git_worktree_support(wd: WorkDir, tmp_path: Path) -> None:
    wd.commit_testfile()
    worktree = tmp_path / "work_tree"
    wd(f"git worktree add -b work-tree {worktree}")

    res = run([sys.executable, "-m", "setuptools_scm", "ls"], cwd=worktree)
    assert "test.txt" in res.stdout
    assert str(worktree) in res.stdout


@pytest.fixture
def shallow_wd(wd: WorkDir, tmp_path: Path) -> Path:
    wd.commit_testfile()
    wd.commit_testfile()
    wd.commit_testfile()
    target = tmp_path / "wd_shallow"
    run(["git", "clone", f"file://{wd.cwd}", target, "--depth=1"], tmp_path, check=True)
    return target


def test_git_parse_shallow_warns(
    shallow_wd: Path, recwarn: pytest.WarningsRecorder
) -> None:
    git.parse(shallow_wd, Configuration())
    print(list(recwarn))
    msg = recwarn.pop()
    assert "is shallow and may cause errors" in str(msg.message)


def test_git_parse_shallow_fail(shallow_wd: Path) -> None:
    with pytest.raises(ValueError, match="git fetch"):
        git.parse(str(shallow_wd), Configuration(), pre_parse=git.fail_on_shallow)


def test_git_shallow_autocorrect(
    shallow_wd: Path, recwarn: pytest.WarningsRecorder
) -> None:
    git.parse(str(shallow_wd), Configuration(), pre_parse=git.fetch_on_shallow)
    msg = recwarn.pop()
    assert "git fetch was used to rectify" in str(msg.message)
    git.parse(str(shallow_wd), Configuration(), pre_parse=git.fail_on_shallow)


def test_find_files_stop_at_root_git(wd: WorkDir) -> None:
    wd.commit_testfile()
    project = wd.cwd / "project"
    project.mkdir()
    project.joinpath("setup.cfg").touch()
    assert setuptools_scm._file_finders.find_files(str(project)) == []


@pytest.mark.issue(128)
def test_parse_no_worktree(tmp_path: Path) -> None:
    ret = git.parse(str(tmp_path), Configuration(root=str(tmp_path)))
    assert ret is None


def test_alphanumeric_tags_match(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag newstyle-development-started")
    assert wd.get_version().startswith("0.1.dev1+g")


def test_git_archive_export_ignore(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    wd.write("test1.txt", "test")
    wd.write("test2.txt", "test")
    wd.write(
        ".git/info/attributes",
        # Explicitly include test1.txt so that the test is not affected by
        # a potentially global gitattributes file on the test machine.
        "/test1.txt -export-ignore\n/test2.txt export-ignore",
    )
    wd("git add test1.txt test2.txt")
    wd.commit()
    monkeypatch.chdir(wd.cwd)
    assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]


@pytest.mark.issue(228)
def test_git_archive_subdirectory(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
    os.mkdir(wd.cwd / "foobar")
    wd.write("foobar/test1.txt", "test")
    wd("git add foobar")
    wd.commit()
    monkeypatch.chdir(wd.cwd)
    assert setuptools_scm._file_finders.find_files(".") == [
        opj(".", "foobar", "test1.txt")
    ]


@pytest.mark.issue(251)
def test_git_archive_run_from_subdirectory(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    os.mkdir(wd.cwd / "foobar")
    wd.write("foobar/test1.txt", "test")
    wd("git add foobar")
    wd.commit()
    monkeypatch.chdir(wd.cwd / "foobar")
    assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/728")
def test_git_branch_names_correct(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git checkout -b test/fun")
    wd_git = git.GitWorkdir(wd.cwd)
    assert wd_git.get_branch() == "test/fun"


def test_git_feature_branch_increments_major(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag 1.0.0")
    wd.commit_testfile()
    assert wd.get_version(version_scheme="python-simplified-semver").startswith("1.0.1")
    wd("git checkout -b feature/fun")
    wd.commit_testfile()
    assert wd.get_version(version_scheme="python-simplified-semver").startswith("1.1.0")


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/303")
def test_not_matching_tags(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag apache-arrow-0.11.1")
    wd.commit_testfile()
    wd("git tag apache-arrow-js-0.9.9")
    wd.commit_testfile()
    assert wd.get_version(
        tag_regex=r"^apache-arrow-([\.0-9]+)$",
        scm={
            "git": {
                "describe_command": "git describe --dirty --tags --long --exclude *js* "
            }
        },
    ).startswith("0.11.2")


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/411")
def test_non_dotted_version(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag apache-arrow-1")
    wd.commit_testfile()
    assert wd.get_version().startswith("2")


def test_non_dotted_version_with_updated_regex(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag apache-arrow-1")
    wd.commit_testfile()
    assert wd.get_version(tag_regex=r"^apache-arrow-([\.0-9]+)$").startswith("2")


def test_non_dotted_tag_no_version_match(wd: WorkDir) -> None:
    wd.commit_testfile()
    wd("git tag apache-arrow-0.11.1")
    wd.commit_testfile()
    wd("git tag apache-arrow")
    wd.commit_testfile()
    assert wd.get_version().startswith("0.11.2.dev2")


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/381")
def test_gitdir(monkeypatch: pytest.MonkeyPatch, wd: WorkDir) -> None:
    """ """
    wd.commit_testfile()
    normal = wd.get_version()
    # git hooks set this and break subsequent setuptools-scm unless we clean
    monkeypatch.setenv("GIT_DIR", __file__)
    assert wd.get_version() == normal


def test_git_getdate(wd: WorkDir) -> None:
    # TODO: case coverage for git wd parse
    today = datetime.now(timezone.utc).date()

    def parse_date() -> date:
        parsed = git.parse(os.fspath(wd.cwd), Configuration())
        assert parsed is not None
        assert parsed.node_date is not None
        return parsed.node_date

    git_wd = git.GitWorkdir(wd.cwd)
    assert git_wd.get_head_date() is None
    assert parse_date() == today

    wd.commit_testfile()
    assert git_wd.get_head_date() == today
    assert parse_date() == today


def test_git_getdate_badgit(
    wd: WorkDir, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
    wd.commit_testfile()
    git_wd = git.GitWorkdir(wd.cwd)
    fake_date_result = CompletedProcess(args=[], stdout="%cI", stderr="", returncode=0)
    with patch.object(
        git,
        "run_git",
        Mock(return_value=fake_date_result),
    ):
        assert git_wd.get_head_date() is None


def test_git_getdate_git_2_45_0_plus(
    wd: WorkDir, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
    wd.commit_testfile()
    git_wd = git.GitWorkdir(wd.cwd)
    fake_date_result = CompletedProcess(
        args=[], stdout="2024-04-30T22:33:10Z", stderr="", returncode=0
    )
    with patch.object(
        git,
        "run_git",
        Mock(return_value=fake_date_result),
    ):
        assert git_wd.get_head_date() == date(2024, 4, 30)


def test_git_getdate_timezone_consistency(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
    """Test that get_head_date returns consistent UTC dates regardless of local timezone.

    This test forces a git commit with a timestamp that represents a time
    after midnight in a positive timezone offset but still the previous day in UTC.
    This is the exact scenario that was causing test failures in issue #1145.
    """
    # Create a timestamp that's problematic:
    # - In Europe/Berlin (UTC+2): 2025-06-12 00:30:00 (June 12th)
    # - In UTC: 2025-06-11 22:30:00 (June 11th)
    problematic_timestamp = "2025-06-12T00:30:00+02:00"

    # Force git to use this specific timestamp for the commit
    monkeypatch.setenv("GIT_AUTHOR_DATE", problematic_timestamp)
    monkeypatch.setenv("GIT_COMMITTER_DATE", problematic_timestamp)

    wd.commit_testfile()

    git_wd = git.GitWorkdir(wd.cwd)
    result_date = git_wd.get_head_date()

    # The correct behavior is to return the UTC date (2025-06-11)
    # If the bug is present, it would return the timezone-local date (2025-06-12)
    expected_utc_date = date(2025, 6, 11)

    assert result_date == expected_utc_date


@pytest.fixture
def signed_commit_wd(monkeypatch: pytest.MonkeyPatch, wd: WorkDir) -> WorkDir:
    if not has_command("gpg", args=["--version"], warn=False):
        pytest.skip("gpg executable not found")

    wd.write(
        ".gpg_batch_params",
        """\
%no-protection
%transient-key
Key-Type: RSA
Key-Length: 2048
Name-Real: a test
Name-Email: test@example.com
Expire-Date: 0
""",
    )
    monkeypatch.setenv("GNUPGHOME", str(wd.cwd.resolve(strict=True)))
    wd("gpg --batch --generate-key .gpg_batch_params")

    wd("git config log.showSignature true")
    wd.signed_commit_command = "git commit -S -m test-{reason}"
    return wd


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/548")
def test_git_getdate_signed_commit(signed_commit_wd: WorkDir) -> None:
    today = datetime.now(timezone.utc).date()
    signed_commit_wd.commit_testfile(signed=True)
    git_wd = git.GitWorkdir(signed_commit_wd.cwd)
    assert git_wd.get_head_date() == today


@pytest.mark.parametrize(
    ("expected", "from_data"),
    [
        (
            "1.0",
            {"describe-name": "1.0-0-g0000"},
        ),
        (
            "1.1.dev3+g0000",
            {
                "describe-name": "1.0-3-g0000",
                "node": "0" * 20,
            },
        ),
        ("0.0", {"node": "0" * 20}),
        ("1.2.2", {"describe-name": "release-1.2.2-0-g00000"}),
        ("1.2.2.dev0", {"ref-names": "tag: release-1.2.2.dev"}),
        ("1.2.2", {"describe-name": "v1.2.2"}),
    ],
)
@pytest.mark.filterwarnings("ignore:git archive did not support describe output")
def test_git_archival_to_version(expected: str, from_data: dict[str, str]) -> None:
    config = Configuration(
        version_scheme="guess-next-dev", local_scheme="node-and-date"
    )
    version = archival_to_version(from_data, config=config)
    assert version is not None
    assert format_version(version) == expected


@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/727")
def test_git_archival_node_missing_no_version() -> None:
    config = Configuration()
    version = archival_to_version({}, config=config)
    assert version is None


def test_git_archival_from_unfiltered() -> None:
    config = Configuration()

    with pytest.warns(
        UserWarning, match=r"unprocessed git archival found \(no export subst applied\)"
    ):
        version = archival_to_version({"node": "$Format:%H$"}, config=config)
    assert version is None


def test_fail_on_missing_submodules_no_gitmodules(wd: WorkDir) -> None:
    """Test that fail_on_missing_submodules does nothing when no .gitmodules exists."""
    wd.commit_testfile()
    # Should not raise any exception
    git.fail_on_missing_submodules(git.GitWorkdir(wd.cwd))


def test_fail_on_missing_submodules_with_initialized_submodules(wd: WorkDir) -> None:
    """Test that fail_on_missing_submodules passes when submodules are initialized."""
    # Create a submodule directory and .gitmodules file
    submodule_dir = wd.cwd / "external"
    submodule_dir.mkdir()

    # Initialize a git repo in the submodule directory
    wd(["git", "-C", str(submodule_dir), "init"])
    wd(["git", "-C", str(submodule_dir), "config", "user.email", "test@example.com"])
    wd(["git", "-C", str(submodule_dir), "config", "user.name", "Test User"])

    # Create a commit in the submodule
    test_file = submodule_dir / "test.txt"
    test_file.write_text("test content", encoding="utf-8")
    wd(["git", "-C", str(submodule_dir), "add", "test.txt"])
    wd(["git", "-C", str(submodule_dir), "commit", "-m", "Initial commit"])

    # Add it as a submodule to the main repo
    wd(["git", "submodule", "add", str(submodule_dir), "external"])
    wd.commit_testfile()

    # Should not raise any exception since the submodule is initialized
    git.fail_on_missing_submodules(git.GitWorkdir(wd.cwd))


def test_fail_on_missing_submodules_with_uninitialized_submodules(
    tmp_path: Path,
) -> None:
    """Test that fail_on_missing_submodules fails when submodules are not initialized."""
    # Create a test repository with a .gitmodules file but no actual submodule
    test_repo = tmp_path / "test_repo"
    test_repo.mkdir()
    test_wd = setup_git_wd(WorkDir(test_repo))

    # Create a fake .gitmodules file (this simulates what happens after cloning without --recurse-submodules)
    gitmodules_content = """[submodule "external"]
	path = external
	url = https://example.com/external.git
"""
    test_wd.write(".gitmodules", gitmodules_content)
    test_wd.add_and_commit("Add-submodule-reference")

    # Should raise ValueError for uninitialized submodules
    with pytest.raises(
        ValueError, match=r"Submodules are defined in \.gitmodules but not initialized"
    ):
        git.fail_on_missing_submodules(git.GitWorkdir(test_repo))


def test_git_pre_parse_config_integration(wd: WorkDir) -> None:
    """Test that git_pre_parse configuration is used by the parse function."""
    wd.commit_testfile()

    # Test with default (None) - should use warn_on_shallow
    config = Configuration()
    result = git.parse(str(wd.cwd), config)
    assert result is not None

    # Test with explicit configuration
    from setuptools_scm._config import GitConfiguration
    from setuptools_scm._config import ScmConfiguration

    config_with_pre_parse = Configuration(
        scm=ScmConfiguration(
            git=GitConfiguration(pre_parse=git.GitPreParse.WARN_ON_SHALLOW)
        )
    )
    result = git.parse(str(wd.cwd), config_with_pre_parse)
    assert result is not None

    # Test with different pre_parse value
    config_fail_shallow = Configuration(
        scm=ScmConfiguration(
            git=GitConfiguration(pre_parse=git.GitPreParse.FAIL_ON_MISSING_SUBMODULES)
        )
    )
    result = git.parse(str(wd.cwd), config_fail_shallow)
    assert result is not None


def test_nested_scm_git_config_from_toml(tmp_path: Path) -> None:
    """Test that nested SCM git configuration is properly parsed from TOML."""
    # Create a test pyproject.toml with nested SCM configuration
    pyproject_path = tmp_path / "pyproject.toml"
    pyproject_content = """
[tool.setuptools_scm.scm.git]
pre_parse = "fail_on_missing_submodules"
"""
    pyproject_path.write_text(pyproject_content, encoding="utf-8")

    # Parse the configuration from file
    config = Configuration.from_file(pyproject_path)

    # Verify the nested configuration was parsed correctly and converted to enum
    assert config.scm.git.pre_parse == git.GitPreParse.FAIL_ON_MISSING_SUBMODULES


def test_nested_scm_git_config_from_data() -> None:
    """Test that nested SCM git configuration parsing works correctly with from_data."""
    # Test configuration parsing directly without file I/O
    config_data = {"scm": {"git": {"pre_parse": "fail_on_missing_submodules"}}}

    # Parse the configuration data
    config = Configuration.from_data(relative_to=".", data=config_data)

    # Verify the nested configuration was parsed correctly and converted to enum
    assert config.scm.git.pre_parse == git.GitPreParse.FAIL_ON_MISSING_SUBMODULES


def test_invalid_git_pre_parse_raises_error() -> None:
    """Test that invalid git pre_parse values raise a helpful ValueError."""
    # Test configuration parsing directly without file I/O
    invalid_config_data = {"scm": {"git": {"pre_parse": "invalid_function"}}}

    # Parse the configuration data - should raise ValueError
    with pytest.raises(
        ValueError, match="Invalid git pre_parse function 'invalid_function'"
    ):
        Configuration.from_data(relative_to=".", data=invalid_config_data)


def test_git_describe_command_backward_compatibility() -> None:
    """Test backward compatibility for git_describe_command configuration."""
    # Test old configuration style still works with deprecation warning
    old_config_data = {
        "git_describe_command": "git describe --dirty --tags --long --exclude *js*"
    }

    with pytest.warns(DeprecationWarning, match=r"git_describe_command.*deprecated"):
        config = Configuration.from_data(relative_to=".", data=old_config_data)

    # Verify it was migrated to the new location
    assert (
        config.scm.git.describe_command
        == "git describe --dirty --tags --long --exclude *js*"
    )


def test_git_describe_command_from_data_conflict() -> None:
    """Test that specifying both old and new configuration in from_data raises ValueError."""
    # Both old and new configuration specified - should raise ValueError
    mixed_config_data = {
        "git_describe_command": "old command",
        "scm": {"git": {"describe_command": "new command"}},
    }

    # The Configuration constructor should handle the conflict detection
    with pytest.warns(DeprecationWarning, match=r"git_describe_command.*deprecated"):
        with pytest.raises(
            ValueError, match=r"Cannot specify both.*git_describe_command"
        ):
            Configuration.from_data(relative_to=".", data=mixed_config_data)


def test_git_describe_command_init_argument_deprecation() -> None:
    """Test that passing git_describe_command as init argument issues deprecation warning."""
    # Test init argument
    with pytest.warns(DeprecationWarning, match=r"git_describe_command.*deprecated"):
        config = Configuration(git_describe_command="test command")

    # Verify the value was migrated to the new location
    assert config.scm.git.describe_command == "test command"


def test_git_describe_command_init_conflict() -> None:
    """Test that specifying both old and new configuration raises ValueError."""
    from setuptools_scm._config import GitConfiguration
    from setuptools_scm._config import ScmConfiguration

    # Both old init arg and new configuration specified - should raise ValueError
    with pytest.warns(DeprecationWarning, match=r"git_describe_command.*deprecated"):
        with pytest.raises(
            ValueError, match=r"Cannot specify both.*git_describe_command"
        ):
            Configuration(
                git_describe_command="old command",
                scm=ScmConfiguration(
                    git=GitConfiguration(describe_command="new command")
                ),
            )


def test_git_no_commits_uses_fallback_version(wd: WorkDir) -> None:
    """Test that when git describe fails (no commits), fallback_version is used instead of 0.0."""
    # Reinitialize as empty repo to remove any existing commits
    wd("rm -rf .git")
    wd("git init")
    wd("git config user.email test@example.com")
    wd('git config user.name "a test"')

    # Test with fallback_version set - should use the fallback instead of "0.0"
    config = Configuration(fallback_version="1.2.3")
    version = git.parse(str(wd.cwd), config)

    # Should get a version starting with the fallback version
    assert version is not None
    assert str(version.tag) == "1.2.3"
    assert version.distance == 0
    assert version.dirty is True  # No commits means dirty

    # Test without fallback_version - should default to "0.0"
    config_no_fallback = Configuration()
    version_no_fallback = git.parse(str(wd.cwd), config_no_fallback)

    assert version_no_fallback is not None
    assert str(version_no_fallback.tag) == "0.0"
    assert version_no_fallback.distance == 0
    assert version_no_fallback.dirty is True