Skip to content

Commit b85e174

Browse files
committed
Review comments
1 parent 660c40a commit b85e174

6 files changed

Lines changed: 155 additions & 149 deletions

File tree

dfetch/commands/diff.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@
3434
The patch can be used in the manifest; see the :ref:`patch` attribute for more information.
3535
3636
Because the patch is generated relative to the project's directory, you should use the :ref:`format-patch`
37-
command to reformat the patch for upstream use.
37+
command to reformat the patch for upstream use. This will make the patch relative to the root of the project,
38+
which is usually expected by upstream projects.
39+
40+
.. tabs::
41+
42+
.. tab:: Git
43+
44+
.. code-block:: sh
45+
46+
git apply --check formatted-some-project.patch
47+
48+
.. tab:: SVN
49+
50+
.. code-block:: sh
51+
52+
svn patch formatted-some-project.patch
3853
3954
"""
4055

dfetch/commands/format_patch.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
*Dfetch* allows you to keep local changes to external projects in the form of
44
patch files. These patch files should be created with the `dfetch diff` command.
5-
However, these patch files are relative the :ref:`source directory <source-dir>`
5+
However, these patch files are relative to the :ref:`source directory <source-dir>`
66
of the project inside the superproject. This makes it hard to apply these patches
77
upstream, as upstream projects usually expect patches to be relative to their
88
root directory. The ``format-patch`` command reformats all patches of a project
@@ -14,17 +14,18 @@
1414
1515
.. tabs::
1616
17-
.. tab:: Git
17+
.. tab:: Git
1818
1919
.. scenario-include:: ../features/format-patch-in-git.feature
2020
21-
.. tab:: SVN
21+
.. tab:: SVN
2222
23-
.. scenario-include:: ../features/format-patch-in-svn.feature
23+
.. scenario-include:: ../features/format-patch-in-svn.feature
2424
2525
"""
2626

2727
import argparse
28+
import os
2829
import pathlib
2930
import re
3031

@@ -96,10 +97,9 @@ def __call__(self, args: argparse.Namespace) -> None:
9697
)
9798
continue
9899

100+
version = subproject.on_disk_version()
99101
for idx, patch in enumerate(subproject.patch, start=1):
100102

101-
version = subproject.on_disk_version()
102-
103103
patch_info = PatchInfo(
104104
author=PatchAuthor(
105105
name=superproject.get_username(),
@@ -126,7 +126,7 @@ def __call__(self, args: argparse.Namespace) -> None:
126126

127127
logger.print_info_line(
128128
project.name,
129-
f"formatted patch written to {output_patch_file}",
129+
f"formatted patch written to {output_patch_file.relative_to(os.getcwd())}",
130130
)
131131

132132
if exceptions:

dfetch/vcs/patch.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import stat
88
from collections.abc import Sequence
99
from dataclasses import dataclass, field
10+
from email.utils import format_datetime
1011
from pathlib import Path
1112

1213
import patch_ng
@@ -236,7 +237,7 @@ def to_git_header(self) -> str:
236237
return (
237238
f"From {self.revision or '0000000000000000000000000000000000000000'} Mon Sep 17 00:00:00 2001\n"
238239
f"From: {self.author.name} <{self.author.email}>\n"
239-
f"Date: {self.date:%a, %d %b %Y %H:%M:%S +0000}\n"
240+
f"Date: {format_datetime(self.date)}\n"
240241
f"Subject: {subject_line}\n"
241242
"\n"
242243
f"{self.description if self.description else self.subject}\n"
@@ -252,7 +253,7 @@ def add_prefix_to_patch(file_path: str, path_prefix: str) -> str:
252253
"""Add a prefix to all file paths in the given patch file."""
253254
patch = patch_ng.fromfile(file_path)
254255
if not patch or not patch.items:
255-
return ""
256+
raise RuntimeError(f'Failed to parse patch file: "{file_path}"')
256257

257258
prefix = path_prefix.strip("/").encode()
258259
if prefix:
@@ -264,19 +265,19 @@ def rewrite_path(path: bytes) -> bytes:
264265
return prefix + path
265266

266267
diff_git = re.compile(
267-
r"^diff --git (?:(?P<a>a/))?(?P<old>.+) (?:(?P<b>b/))?(?P<new>.+)$"
268+
r"^diff --git (?:(?P<a>a/))?(?P<old>.+) (?:(?P<b>b/))?(?P<new>.+?)[\r\n]*$"
268269
)
269270
svn_index = re.compile(rb"^Index: (.+)$")
270271

271272
for file in patch.items:
272273
file.source = rewrite_path(file.source)
273274
file.target = rewrite_path(file.target)
274275

275-
for line in file.header:
276+
for idx, line in enumerate(file.header):
276277

277278
git_match = diff_git.match(line.decode("utf-8", errors="replace"))
278279
if git_match:
279-
file.header[file.header.index(line)] = (
280+
file.header[idx] = (
280281
b"diff --git "
281282
+ (git_match.group("a").encode() if git_match.group("a") else b"")
282283
+ rewrite_path(git_match.group("old").encode())
@@ -288,9 +289,7 @@ def rewrite_path(path: bytes) -> bytes:
288289

289290
svn_match = svn_index.match(line)
290291
if svn_match:
291-
file.header[file.header.index(line)] = b"Index: " + rewrite_path(
292-
svn_match.group(1)
293-
)
292+
file.header[idx] = b"Index: " + rewrite_path(svn_match.group(1))
294293
break
295294

296295
return dump_patch(patch)

doc/asciicasts/format-patch.cast

Lines changed: 123 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,123 @@
1-
{"version": 2, "width": 246, "height": 26, "timestamp": 1770070401, "env": {"SHELL": "/usr/bin/zsh", "TERM": "xterm-256color"}}
2-
[0.395866, "o", "\u001b[H\u001b[2J\u001b[3J"]
3-
[0.398292, "o", "$ "]
4-
[1.402875, "o", "\u001b["]
5-
[1.5835, "o", "1m"]
6-
[1.673799, "o", "ls"]
7-
[1.763914, "o", " -"]
8-
[1.854333, "o", "l ."]
9-
[1.944755, "o", "\u001b["]
10-
[2.035147, "o", "0m"]
11-
[3.03709, "o", "\r\n"]
12-
[3.042968, "o", "total 16\r\ndrwxr-xr-x 3 ben ben 4096 2 feb 23:13 cpputest\r\n-rw-r--r-- 1 ben ben 229 2 feb 23:13 dfetch.yaml\r\ndrwxr-xr-x 4 ben ben 4096 2 feb 23:13 jsmn\r\ndrwxr-xr-x 2 ben ben 4096 2 feb 23:13 patches\r\n"]
13-
[3.047904, "o", "$ "]
14-
[4.052253, "o", "\u001b"]
15-
[4.233017, "o", "[1"]
16-
[4.32355, "o", "mc"]
17-
[4.413797, "o", "at"]
18-
[4.504315, "o", " "]
19-
[4.59472, "o", "df"]
20-
[4.684814, "o", "et"]
21-
[4.774994, "o", "ch"]
22-
[4.865002, "o", ".y"]
23-
[4.955307, "o", "a"]
24-
[5.135917, "o", "ml"]
25-
[5.226055, "o", "\u001b["]
26-
[5.316658, "o", "0m"]
27-
[6.317789, "o", "\r\n"]
28-
[6.319116, "o", "manifest:\r\n version: 0.0\r\n\r\n remotes:\r\n - name: github\r\n url-base: https://github.com/\r\n\r\n projects:\r\n - name: cpputest\r\n dst: cpputest/src/\r\n repo-path: cpputest/cpputest.git\r\n tag: v3.4\r\n patch: patches/cpputest.patch\r\n\r\n"]
29-
[6.322761, "o", "$ "]
30-
[7.325505, "o", "\u001b"]
31-
[7.50573, "o", "[1"]
32-
[7.596031, "o", "mc"]
33-
[7.686183, "o", "at"]
34-
[7.776327, "o", " "]
35-
[7.866535, "o", "pa"]
36-
[7.956615, "o", "tc"]
37-
[8.04674, "o", "he"]
38-
[8.136871, "o", "s/"]
39-
[8.226877, "o", "c"]
40-
[8.407184, "o", "pp"]
41-
[8.497273, "o", "ut"]
42-
[8.587437, "o", "es"]
43-
[8.677675, "o", "t."]
44-
[8.767753, "o", "p"]
45-
[8.857886, "o", "at"]
46-
[8.947974, "o", "ch"]
47-
[9.038122, "o", "\u001b["]
48-
[9.128248, "o", "0m"]
49-
[10.129992, "o", "\r\n"]
50-
[10.132673, "o", "diff --git a/README.md b/README.md\r\nindex 2655a7b..fc6084e 100644\r\n--- a/README.md\r\n+++ b/README.md\r\n@@ -3,7 +3,7 @@ CppUTest\r\n \r\n CppUTest unit testing and mocking framework for C/C++\r\n \r\n-[More information on the project page](http://cpputest.github.com)\r\n+[More information on the project page](http://cpputest.gitlab.com)\r\n \r\n [![Build Status](https://travis-ci.org/cpputest/cpputest.png?branch=master)](https://travis-ci.org/cpputest/cpputest)\r\n \r\n"]
51-
[10.138318, "o", "$ "]
52-
[11.142116, "o", "\u001b"]
53-
[11.322193, "o", "[1"]
54-
[11.412258, "o", "md"]
55-
[11.502828, "o", "fe"]
56-
[11.593204, "o", "t"]
57-
[11.683581, "o", "ch"]
58-
[11.773932, "o", " f"]
59-
[11.864416, "o", "or"]
60-
[11.954734, "o", "ma"]
61-
[12.04482, "o", "t"]
62-
[12.225664, "o", "-p"]
63-
[12.315617, "o", "at"]
64-
[12.406095, "o", "ch"]
65-
[12.496632, "o", " c"]
66-
[12.586952, "o", "p"]
67-
[12.6773, "o", "pu"]
68-
[12.767391, "o", "te"]
69-
[12.857596, "o", "st"]
70-
[12.947987, "o", " -"]
71-
[13.128738, "o", "-"]
72-
[13.219079, "o", "ou"]
73-
[13.309201, "o", "tp"]
74-
[13.399715, "o", "ut"]
75-
[13.490034, "o", "-d"]
76-
[13.580433, "o", "i"]
77-
[13.670814, "o", "re"]
78-
[13.761331, "o", "ct"]
79-
[13.851659, "o", "or"]
80-
[13.942035, "o", "y "]
81-
[14.122601, "o", "f"]
82-
[14.212799, "o", "or"]
83-
[14.303215, "o", "ma"]
84-
[14.393702, "o", "tt"]
85-
[14.483958, "o", "ed"]
86-
[14.574369, "o", "-"]
87-
[14.664761, "o", "pa"]
88-
[14.754758, "o", "tc"]
89-
[14.84471, "o", "he"]
90-
[15.02514, "o", "s\u001b"]
91-
[15.115163, "o", "["]
92-
[15.205631, "o", "0m"]
93-
[16.208065, "o", "\r\n"]
94-
[16.394329, "o", "\u001b[1;34mDfetch (0.11.0)\u001b[0m \r\n"]
95-
[16.42452, "o", " \u001b[1;92mcpputest:\u001b[0m \r\n"]
96-
[16.425051, "o", " \u001b[1;34m> formatted patch written to /home/ben/Programming/dfetch/doc/generate-casts/format-patch/formatted-patches/cpputest.patch\u001b[0m \r\n"]
97-
[16.47633, "o", "$ "]
98-
[17.479211, "o", "\u001b"]
99-
[17.659454, "o", "[1"]
100-
[17.749572, "o", "mc"]
101-
[17.839756, "o", "at"]
102-
[17.930321, "o", " f"]
103-
[18.020526, "o", "or"]
104-
[18.110909, "o", "ma"]
105-
[18.201313, "o", "tt"]
106-
[18.291811, "o", "ed"]
107-
[18.382122, "o", "-p"]
108-
[18.562995, "o", "a"]
109-
[18.65331, "o", "tc"]
110-
[18.743776, "o", "he"]
111-
[18.833818, "o", "s/"]
112-
[18.92426, "o", "cp"]
113-
[19.014617, "o", "pu"]
114-
[19.104804, "o", "te"]
115-
[19.195143, "o", "st"]
116-
[19.285591, "o", ".p"]
117-
[19.4663, "o", "at"]
118-
[19.556447, "o", "c"]
119-
[19.646761, "o", "h\u001b"]
120-
[19.737077, "o", "[0"]
121-
[19.827509, "o", "m"]
122-
[20.828831, "o", "\r\n"]
123-
[20.831096, "o", "From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001\r\nFrom: Ben Spoor <ben.spoor@gmail.com>\r\nDate: Mon, 02 Feb 2026 22:13:37 +0000\r\nSubject: [PATCH] Patch for cpputest\r\n\r\nPatch for cpputest\r\n\r\ndiff --git a/README.md b/README.md\r\n--- a/README.md\r\n+++ b/README.md\r\n@@ -3,7 +3,7 @@\r\n \r\n CppUTest unit testing and mocking framework for C/C++\r\n \r\n-[More information on the project page](http://cpputest.github.com)\r\n+[More information on the project page](http://cpputest.gitlab.com)\r\n \r\n [![Build Status](https://travis-ci.org/cpputest/cpputest.png?branch=master)](https://travis-ci.org/cpputest/cpputest)\r\n \r\n"]
124-
[23.841354, "o", "$ "]
125-
[23.842933, "o", "\u001b"]
126-
[24.023636, "o", "[1"]
127-
[24.114049, "o", "m\u001b"]
128-
[24.204447, "o", "[0"]
129-
[24.294616, "o", "m"]
130-
[24.295248, "o", "\r\n"]
131-
[24.298402, "o", "~/Programming/dfetch/doc/generate-casts\r\n"]
1+
{"version": 2, "width": 165, "height": 24, "timestamp": 1770414454, "env": {"SHELL": "/usr/bin/zsh", "TERM": "xterm-256color"}}
2+
[0.385166, "o", "\u001b[H\u001b[2J\u001b[3J"]
3+
[0.388707, "o", "$ "]
4+
[1.39279, "o", "\u001b"]
5+
[1.57374, "o", "[1"]
6+
[1.663894, "o", "ml"]
7+
[1.754375, "o", "s "]
8+
[1.84471, "o", "-"]
9+
[1.935087, "o", "l "]
10+
[2.025545, "o", ".\u001b"]
11+
[2.115956, "o", "[0"]
12+
[2.206353, "o", "m"]
13+
[3.208625, "o", "\r\n"]
14+
[3.214577, "o", "total 16\r\ndrwxr-xr-x 3 ben ben 4096 6 feb 22:47 cpputest\r\n-rw-r--r-- 1 ben ben 229 6 feb 22:47 dfetch.yaml\r\ndrwxr-xr-x 4 ben ben 4096 6 feb 22:47 jsmn\r\ndrwxr-xr-x 2 ben ben 4096 6 feb 22:47 patches\r\n"]
15+
[3.218604, "o", "$ "]
16+
[4.222973, "o", "\u001b"]
17+
[4.403419, "o", "[1"]
18+
[4.493893, "o", "mc"]
19+
[4.58377, "o", "at"]
20+
[4.674053, "o", " "]
21+
[4.764255, "o", "df"]
22+
[4.854424, "o", "et"]
23+
[4.944634, "o", "ch"]
24+
[5.034809, "o", ".y"]
25+
[5.124896, "o", "a"]
26+
[5.30509, "o", "ml"]
27+
[5.395647, "o", "\u001b["]
28+
[5.486193, "o", "0m"]
29+
[6.488975, "o", "\r\n"]
30+
[6.492929, "o", "manifest:\r\n version: 0.0\r\n\r\n remotes:\r\n - name: github\r\n url-base: https://github.com/\r\n\r\n projects:\r\n - name: cpputest\r\n dst: cpputest/src/\r\n repo-path: cpputest/cpputest.git\r\n tag: v3.4\r\n patch: patches/cpputest.patch\r\n\r\n"]
31+
[6.499152, "o", "$ "]
32+
[7.502713, "o", "\u001b"]
33+
[7.683188, "o", "[1"]
34+
[7.773653, "o", "mc"]
35+
[7.863918, "o", "at"]
36+
[7.954348, "o", " p"]
37+
[8.044801, "o", "at"]
38+
[8.135135, "o", "ch"]
39+
[8.225701, "o", "es"]
40+
[8.315853, "o", "/c"]
41+
[8.406361, "o", "pp"]
42+
[8.587196, "o", "u"]
43+
[8.677545, "o", "te"]
44+
[8.767967, "o", "st"]
45+
[8.858375, "o", ".p"]
46+
[8.948862, "o", "at"]
47+
[9.039212, "o", "ch"]
48+
[9.129207, "o", "\u001b["]
49+
[9.219685, "o", "0m"]
50+
[10.222297, "o", "\r\n"]
51+
[10.226448, "o", "diff --git a/README.md b/README.md\r\nindex 2655a7b..fc6084e 100644\r\n--- a/README.md\r\n+++ b/README.md\r\n@@ -3,7 +3,7 @@ CppUTest\r\n \r\n CppUTest unit testing and mocking framework for C/C++\r\n \r\n-[More information on the project page](http://cpputest.github.com)\r\n+[More information on the project page](http://cpputest.gitlab.com)\r\n \r\n [![Build Status](https://travis-ci.org/cpputest/cpputest.png?branch=master)](https://travis-ci.org/cpputest/cpputest)\r\n \r\n"]
52+
[10.232279, "o", "$ "]
53+
[11.235859, "o", "\u001b["]
54+
[11.416067, "o", "1m"]
55+
[11.506251, "o", "df"]
56+
[11.59644, "o", "et"]
57+
[11.686544, "o", "ch"]
58+
[11.777078, "o", " f"]
59+
[11.86725, "o", "or"]
60+
[11.957392, "o", "ma"]
61+
[12.04778, "o", "t-"]
62+
[12.138261, "o", "pa"]
63+
[12.319071, "o", "tch"]
64+
[12.409384, "o", " c"]
65+
[12.499765, "o", "pp"]
66+
[12.589991, "o", "ut"]
67+
[12.680419, "o", "es"]
68+
[12.770801, "o", "t "]
69+
[12.861241, "o", "--"]
70+
[12.951769, "o", "ou"]
71+
[13.042108, "o", "tp"]
72+
[13.222777, "o", "ut"]
73+
[13.313028, "o", "-di"]
74+
[13.403229, "o", "re"]
75+
[13.493634, "o", "ct"]
76+
[13.584047, "o", "or"]
77+
[13.674436, "o", "y "]
78+
[13.764898, "o", "fo"]
79+
[13.855297, "o", "rm"]
80+
[13.945458, "o", "at"]
81+
[14.126256, "o", "te"]
82+
[14.216761, "o", "d-"]
83+
[14.306729, "o", "pat"]
84+
[14.39705, "o", "ch"]
85+
[14.487593, "o", "es"]
86+
[14.577914, "o", "\u001b["]
87+
[14.668277, "o", "0m"]
88+
[15.670242, "o", "\r\n"]
89+
[15.859377, "o", "\u001b[1;34mDfetch (0.11.0)\u001b[0m \r\n"]
90+
[15.882698, "o", " \u001b[1;92mcpputest:\u001b[0m \r\n"]
91+
[15.883088, "o", " \u001b[1;34m> formatted patch written to formatted-patches/cpputest.patch\u001b[0m \r\n"]
92+
[15.927913, "o", "$ "]
93+
[16.930605, "o", "\u001b["]
94+
[17.110867, "o", "1m"]
95+
[17.200843, "o", "ca"]
96+
[17.291177, "o", "t "]
97+
[17.381424, "o", "fo"]
98+
[17.471661, "o", "rm"]
99+
[17.561872, "o", "at"]
100+
[17.651834, "o", "te"]
101+
[17.742221, "o", "d-"]
102+
[17.83269, "o", "pa"]
103+
[18.013331, "o", "tch"]
104+
[18.103427, "o", "es"]
105+
[18.193863, "o", "/c"]
106+
[18.284263, "o", "pp"]
107+
[18.374681, "o", "ut"]
108+
[18.465034, "o", "es"]
109+
[18.555439, "o", "t."]
110+
[18.645841, "o", "pa"]
111+
[18.736188, "o", "tc"]
112+
[18.916929, "o", "h\u001b"]
113+
[19.007093, "o", "[0m"]
114+
[20.00907, "o", "\r\n"]
115+
[20.012242, "o", "From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001\r\nFrom: Ben Spoor <ben.spoor@gmail.com>\r\nDate: Fri, 06 Feb 2026 21:47:50 +0000\r\nSubject: [PATCH] Patch for cpputest\r\n\r\nPatch for cpputest\r\n\r\ndiff --git a/README.md b/README.md\r\nindex 2655a7b..fc6084e 100644\r\n--- a/README.md\r\n+++ b/README.md\r\n@@ -3,7 +3,7 @@\r\n \r\n CppUTest unit testing and mocking framework for C/C++\r\n \r\n-[More information on the project page](http://cpputest.github.com)\r\n+[More information on the project page](http://cpputest.gitlab.com)\r\n \r\n [![Build Status](https://travis-ci.org/cpputest/cpputest.png?branch=master)](https://travis-ci.org/cpputest/cpputest)\r\n \r\n"]
116+
[23.02209, "o", "$ "]
117+
[23.023883, "o", "\u001b"]
118+
[23.204495, "o", "[1"]
119+
[23.294543, "o", "m\u001b"]
120+
[23.384966, "o", "[0"]
121+
[23.475353, "o", "m"]
122+
[23.475814, "o", "\r\n"]
123+
[23.479452, "o", "~/Programming/dfetch/doc/generate-casts\r\n"]

doc/manual.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Format patch
133133
:prog: dfetch
134134
:path: format-patch
135135

136-
.. asciinema:: asciicasts/format_patch.cast
136+
.. asciinema:: asciicasts/format-patch.cast
137137

138138
.. automodule:: dfetch.commands.format_patch
139139

0 commit comments

Comments
 (0)