Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/dco.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ jobs:
# whose event payload can still reference an obsolete base commit.
test "$(git rev-parse HEAD^2)" = "$HEAD_SHA"
BASE_SHA="$(git rev-parse HEAD^1)"
# Fetch original PR history. The checker pins the source commit IDs.
if [ "$GITHUB_BASE_REF" = main ]; then
git fetch --no-tags origin refs/pull/321/head refs/pull/347/head refs/pull/362/head
fi
python scripts/check_dco.py "$BASE_SHA" "$HEAD_SHA" "$DCO_BASELINE_SHA"
23 changes: 23 additions & 0 deletions docs/dco.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ git push --force-with-lease

For multiple commits, use an interactive rebase and add a sign-off to each
commit. Do not add another person's sign-off without their authorization.

## Preserving sign-offs when merging

Prefer a merge commit to retain the original signed-off commits. If you squash,
edit the squash message before merging. Preserve the exact `Signed-off-by`
trailers from the signed source commits for every contributor whose work
the squash contains. Verify the final commit message includes those trailers.
If conflict resolution introduces new work, the person who made those changes
must also sign off. Use a merge commit if you cannot preserve the certifications
in the squash message.

## Historical squash certification

The checker binds three historical squash commits to the exact signed source
heads of PRs #321, #347, and #362. It requires a clean replay onto the landed
commit's parent, an exact match with the landed tree, and an author-matching
sign-off on every source commit outside the landed parent's history.
Missing objects, replay conflicts, tree differences, and missing or mismatched
source sign-offs fail the check.

Any change to these mappings requires independent maintainer review of the
official PR's source head, contributor identities, source trailers, and tree
replay. Use the merge procedure above for future contributions.
46 changes: 43 additions & 3 deletions scripts/check_dco.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@
SIGNOFF = re.compile(r"^Signed-off-by:\s+.+\s+<[^<>@\s]+@[^<>\s]+>$", re.IGNORECASE | re.MULTILINE)


# Historical squash commits whose original author certifications can be
# verified against the exact landed tree. The workflow fetches these PR heads.
CERTIFIED_SQUASHES = {
# PR #321
"90f3fa291be3bfb1ef29e0a599e43dd053cafe80": "101d47a15337d0ee8242420c33b13e8cbda58994",
# PR #347
"8371ed6907d3eade7fa836938d1c882f4c8b24c8": "2af0b33f786be171132c3bfe3b9313f53fb93bda",
# PR #362
"5eec78a24c86e5d68da12cb2ad797fd3f4124c95": "23d6dfb69ab395700267a4fabb73a49f75092588",
}


def verify_squash(landed: str, source: str) -> bool:
"""Verify the original authors certified the exact changes that landed."""

def git(*args: str) -> str:
return subprocess.check_output(["git", *args], text=True, stderr=subprocess.DEVNULL).strip()

try:
(parent,) = git("show", "-s", "--format=%P", landed).split()
if git("merge-tree", "--write-tree", parent, source) != git("rev-parse", f"{landed}^{{tree}}"):
return False
commits = git("rev-list", f"{parent}..{source}").splitlines()
if not commits:
return False
for commit in commits:
author = git("show", "-s", "--format=%an <%ae>", commit).casefold()
signoffs = git("show", "-s", "--format=%(trailers:key=Signed-off-by,valueonly)", commit)
if author not in {line.strip().casefold() for line in signoffs.splitlines()}:
return False
return True
except (subprocess.CalledProcessError, ValueError):
return False


def has_signoff(message: str) -> bool:
"""Return whether a commit message contains a well-formed DCO trailer."""
return SIGNOFF.search(message) is not None
Expand Down Expand Up @@ -54,8 +89,13 @@ def commit_message(commit: str) -> str:


def unsigned_commits(commits: Iterable[str]) -> list[str]:
"""Return commit IDs that lack a valid Signed-off-by trailer."""
return [commit for commit in commits if not has_signoff(commit_message(commit))]
"""Return commit IDs without a sign-off or verified historical certification."""
return [
commit
for commit in commits
if not has_signoff(commit_message(commit))
and not (commit in CERTIFIED_SQUASHES and verify_squash(commit, CERTIFIED_SQUASHES[commit]))
]


def main() -> int:
Expand All @@ -70,7 +110,7 @@ def main() -> int:

missing = unsigned_commits(commits)
if missing:
print("The following commits lack a valid DCO Signed-off-by trailer:", file=sys.stderr)
print("The following commits lack a valid DCO sign-off or verified historical certification:", file=sys.stderr)
for commit in missing:
print(f" {commit}", file=sys.stderr)
print("Recreate or amend them with: git commit -s", file=sys.stderr)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_dco_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
from unittest.mock import patch

import pytest

from scripts.check_dco import commit_timestamp, commits_between, has_signoff, main, unsigned_commits


Expand Down Expand Up @@ -97,3 +99,39 @@ def test_baseline_exempts_history_but_checks_new_work_from_old_branch(tmp_path,
side_effect={baseline: 100, new_commit: 101}.get,
):
assert commits_between(baseline, new_commit, baseline) == [new_commit]


@pytest.mark.parametrize(
("trailer", "evidence"),
[
("Signed-off-by: Test User <test@example.com>", "matching"),
("Signed-off-by: Test User <test@example.com>", "different-tree"),
("Signed-off-by: Test User <test@example.com>", "missing-source"),
("Signed-off-by: Other User <other@example.com>", "matching"),
("Signed-off-by: Test User <not-an-email>", "matching"),
("", "matching"),
],
)
def test_historical_squash_requires_exact_signed_source(tmp_path, monkeypatch, trailer, evidence):
_git(tmp_path, "init")
_git(tmp_path, "config", "user.name", "Test User")
_git(tmp_path, "config", "user.email", "test@example.com")
parent = _commit(tmp_path, "parent")
source = _commit(tmp_path, f"source\n\n{trailer}")
_git(tmp_path, "switch", "--detach", parent)
_git(tmp_path, "checkout", source, "--", "history.txt")
if evidence == "different-tree":
(tmp_path / "history.txt").write_text("changed patch", encoding="utf-8")
_git(tmp_path, "add", "history.txt")
_git(tmp_path, "commit", "-m", "unsigned squash")
landed = _git(tmp_path, "rev-parse", "HEAD")
unsigned = _commit(tmp_path, "new unsigned contribution")

if evidence == "missing-source":
source = "0" * 40
monkeypatch.chdir(tmp_path)
with patch.dict("scripts.check_dco.CERTIFIED_SQUASHES", {landed: source}, clear=True):
expected = [unsigned]
if evidence != "matching" or trailer != "Signed-off-by: Test User <test@example.com>":
expected.insert(0, landed)
assert unsigned_commits([landed, unsigned]) == expected
Loading