git
Utilities to get data out of git repositories.
This file defines a base class, GitRepo, which uses straight-up
calling git commands, and needs Git to be installed.
Example usage:
from diffannotator.utils.git import GitRepo files = GitRepo('path/to/git/repo').list_files('HEAD') # 'HEAD' is the default ... ...
This implementation / backend retrieves data by calling git via
subprocess.Popen or subprocess.run, and parsing the output.
WARNING: at the time this backend does not have error handling implemented; it would simply return empty result, without any notification about the error (like incorrect repository path, or incorrect commit)!!!
AuthorStat ¶
Bases: NamedTuple
Parsed result of 'git shortlog -c -s'
Source code in src/diffannotator/utils/git.py
66 67 68 69 | |
ChangeSet ¶
Bases: PatchSet
Commit changes, together with commit data
Note that changeset can come from a commit, or from a diff between two different commits (tree-ish)
Source code in src/diffannotator/utils/git.py
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 | |
__init__ ¶
__init__(
patch_source: Union[StringIO, TextIO, str],
commit_id: str,
prev: Optional[str] = None,
newline: Optional[str] = "\n",
*args,
**kwargs
)
ChangeSet class constructor
| PARAMETER | DESCRIPTION |
|---|---|
patch_source
|
patch source to be parsed by PatchSet (parent class)
TYPE:
|
commit_id
|
oid of the "after" commit (tree-ish) for the change
TYPE:
|
prev
|
previous state, when ChangeSet is generated with .unidiff(),
or
TYPE:
|
newline
|
determines how to parse newline characters from the stream,
and how the stream was opened (one of None i.e. universal
newline, '', '\n', '\r', and '\r\n' - same as
TYPE:
|
*args
|
passed to PatchSet constructor
DEFAULT:
|
**kwargs
|
passed to PatchSet constructor (recommended); PatchSet uses
if
DEFAULT:
|
Source code in src/diffannotator/utils/git.py
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 | |
from_filename
classmethod
¶
from_filename(
filename: Union[str, Path],
encoding: str = DEFAULT_ENCODING,
errors: Optional[str] = ENCODING_ERRORS,
newline: Optional[str] = "\n",
) -> ChangeSet
Return a ChangeSet instance given a diff filename.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
path to a diff file with the patch to parse
TYPE:
|
encoding
|
the name of the encoding used to decode or encode the diff file
TYPE:
|
errors
|
specifies how encoding and decoding errors are to be handled
(one of None, 'strict', 'ignore', 'replace',
'backslashreplace', or 'surrogateescape' - same as
TYPE:
|
newline
|
determines how to parse newline characters from the stream
(one of None, '', '\n', '\r', and '\r\n' - same as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ChangeSet
|
instance of ChangeSet class |
Source code in src/diffannotator/utils/git.py
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 | |
DiffSide ¶
Bases: Enum
Enum to be used for side parameter of GitRepo.list_changed_files
Source code in src/diffannotator/utils/git.py
51 52 53 54 55 56 | |
GitFileMode ¶
Bases: StrEnum
Enumeration of file modes in git diff
To be used to compare the result of get_patched_file_mode() function
against, to check whether the file is a regular file, an executable file,
a symlink, or a submodule.
Source code in src/diffannotator/utils/git.py
218 219 220 221 222 223 224 225 226 227 228 229 | |
GitRepo ¶
Class representing Git repository, for performing operations on
| ATTRIBUTE | DESCRIPTION |
|---|---|
repo |
stores Path to the Git repository
TYPE:
|
Source code in src/diffannotator/utils/git.py
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 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 | |
batch_command
property
¶
batch_command: Popen
Persistent connection to git cat-file --batch-command --buffer
In --batch-command mode, git cat-file will read commands from stdin,
one per line, and print information based on the command given. Namely,
the 'info' command followed by an object will print information about
the object the same way --batch-check would, and the 'contents' command
followed by an object prints contents in the same way --batch would.
Because the --buffer option is used, you can send multiple commands
in a batch, and you must end the input with the 'flush' command.
This is more efficient when running 'info' or 'object' commands
on large number of objects.
TODO: make the use of --buffer option configurable (make it method, not property).
| RETURNS | DESCRIPTION |
|---|---|
Popen
|
Persistent (cached) connection to the |
__init__ ¶
__init__(repo_dir: PathLike)
Constructor for GitRepo class
| PARAMETER | DESCRIPTION |
|---|---|
repo_dir
|
path to the Git repository
TYPE:
|
Source code in src/diffannotator/utils/git.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 | |
are_valid_objects ¶
are_valid_objects(
objects: Iterable[str],
object_type: Optional[str] = "commit",
single_use: bool = False,
) -> list[None | bool]
Check which of given objects are present in the repository
You can ensure that objects not only exist but are of a specific
type ("commit" by default); setting object_type to None turns off
this check.
| PARAMETER | DESCRIPTION |
|---|---|
objects
|
List of object identifiers to check. Often used with a list of commit identifiers (SHA-1, branches, or tags).
TYPE:
|
object_type
|
One of "commit", "tree", "blob", "tag", or None.
If not None, the type is used to restrict the type of object:
only objects of given
TYPE:
|
single_use
|
If True, do not keep the connection to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[None | bool]
|
For each element of |
Source code in src/diffannotator/utils/git.py
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 | |
changed_lines_extents ¶
changed_lines_extents(
commit: str = "HEAD",
prev: Optional[str] = None,
side: DiffSide = DiffSide.POST,
) -> tuple[
dict[str, list[tuple[int, int]]],
dict[str, list[PatchLine]],
PatchSet,
]
List target line numbers of changed files as extents, for each changed file
For each changed file that appears in side side of the diff between
given commits, it returns list of side line numbers (e.g. target line
numbers for post=DiffSide.POST).
Line numbers are returned compressed as extents, that is list of tuples of start and end range. For example, if target line numbers would be [1, 2, 3, 7, 10, 11], then their extent list would be [(1, 3), (7, 7), (10, 11)].
To make it easier to mesh with other parts of computation, and to avoid reparsing diffs, also return parsed patch lines (diff lines).
Uses :func:GitRepo.unidiff to parse git diff between prev and commit.
Used by :func:GitRepo.changes_survival.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
later (second) of two commits to compare, defaults to 'HEAD', that is the current commit
TYPE:
|
prev
|
earlier (first) of two commits to compare,
defaults to None, which means comparing to parent of
TYPE:
|
side
|
Whether to use names of files in post-image (after changes) with side=DiffSide.POST, or pre-image names (before changes) with side=DiffSide.PRE. Renames are detected by Git. Defaults to DiffSide.POST, which is currently the only value supported.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(dict[str, list[tuple[int, int]]], dict[str, list[Line]], PatchSet)
|
two dicts, with changed files names as keys, first with information about change lines extents, second with parsed change lines (only for added lines), and unidiff.PatchSet to avoid recomputing diffs |
Source code in src/diffannotator/utils/git.py
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | |
changes_survival ¶
changes_survival(
commit: str,
prev: Optional[str] = None,
addition_optimization: bool = False,
) -> tuple[dict, dict]
Find what revision each line of commit changes was modified since
This performs reverse blame for each file modified in diff between
commit and prev, which is still present in the post-image of diff
(which means that file deletions are excluded), limited to lines changed
in that file.
Returns 2-element tuple, with per-path information about blamed commits as the first element, and per-path list of blame information for each changed line.
Example of results:
({
'file': {
'd860e7a706ac8aa45089111495ad7fe0004cbbfa': {
'author': 'A U Thor',
'author-email': '<author@example.com>',
'author-time': '1693601010',
'author-tz': '-0600',
# ...
'filename': 'file',
'summary': 'Changed file'
}
}
},[
{
'commit': 'd860e7a706ac8aa45089111495ad7fe0004cbbfa',
'line': 'contents of 1st line',
'final': 1,
'original': 1,
'original_filename': 'file',
# ...
}
])
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
later (second) of two commits to compare, defaults to 'HEAD', that is the current commit
TYPE:
|
prev
|
earlier (first) of two commits to compare,
defaults to None, which means comparing to parent of
TYPE:
|
addition_optimization
|
whether to blame whole file
for files that were added between
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(dict[str, dict], dict[str, list])
|
information about commits blamed, and blame information for each changed file |
Source code in src/diffannotator/utils/git.py
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 | |
check_merged_into ¶
check_merged_into(
commit: str, ref_pattern: Union[str, list[str]] = "HEAD"
) -> list[str]
List those refs among ref_pattern that contain given commit
This method can be used to check if a given commit is merged into
at least one ref matching ref_pattern using 'git for-each-ref --contains',
see https://git-scm.com/docs/git-for-each-ref
Return list of refs that contain given commit, or in other words list of refs that given commit is merged into.
Note that symbolic refs, such as 'HEAD', are expanded.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit to check if it is merged
TYPE:
|
ref_pattern
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
list of refs matching |
Source code in src/diffannotator/utils/git.py
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 | |
checkout_revision ¶
checkout_revision(commit: str) -> None
Check out given commit in a given repository
This would usually (and for some cases always) result in 'detached HEAD' situation, that is HEAD reference pointing directly to a commit, and not being on any named branch.
This function is called for its effects and does return nothing.
Notes
Changes the repository, and therefore requires write access to the repository.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit to check out in given repository.
TYPE:
|
Source code in src/diffannotator/utils/git.py
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 | |
clone_repository
classmethod
¶
clone_repository(
repository: PathLike,
directory: Optional[PathLike] = None,
working_dir: Optional[PathLike] = None,
reference_local_repository: Optional[PathLike] = None,
dissociate: bool = False,
make_path_absolute: bool = False,
) -> Union[GitRepo, None]
Clone a repository into a new directory, return cloned GitRepo
If there is non-empty directory preventing from cloning the repository,
the method assumes that it is because the repository was already cloned;
in this case it returns that directory as GitRepo.
| PARAMETER | DESCRIPTION |
|---|---|
repository
|
The (possibly remote) repository to clone from, usually a URL (ssh, git, http, or https) or a local path.
TYPE:
|
directory
|
The name of a new directory to clone into, optional. The
"human-ish" part of the source repository is used if
TYPE:
|
working_dir
|
The directory where to run the
TYPE:
|
reference_local_repository
|
Use
TYPE:
|
dissociate
|
whether to dissociate with
TYPE:
|
make_path_absolute
|
Ensure that returned
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Optional[GitRepo]
|
Cloned repository as |
Source code in src/diffannotator/utils/git.py
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 | |
close_batch_command ¶
close_batch_command() -> None
Close persistent connection to git cat-file --batch-command --buffer
Note that any access to the .batch_command property will re-create
the connection by starting a new persistent git cat-file process.
The .are_valid_objects() and .filter_valid_commits() methods
access the .batch_command property internally, so they would do
the same.
Source code in src/diffannotator/utils/git.py
915 916 917 918 919 920 921 922 923 924 925 | |
count_commits ¶
count_commits(
start_from: str = StartLogFrom.CURRENT,
until_commit: str = None,
first_parent: bool = False,
) -> int
Count number of commits in the repository
Starting from start_from, count number of commits, stopping
at until_commit if provided.
If first_parent is set to True, makes Git follow only the first
parent commit upon seeing a merge commit.
| PARAMETER | DESCRIPTION |
|---|---|
start_from
|
where to start from to follow 'parent' links
TYPE:
|
until_commit
|
where to stop following 'parent' links; also ensures that we follow ancestry path to it, optional
TYPE:
|
first_parent
|
follow only the first parent commit upon seeing a merge commit
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
number of commits |
Source code in src/diffannotator/utils/git.py
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 | |
create_tag ¶
create_tag(tag_name: str, commit: str = 'HEAD') -> None
Create lightweight tag (refs/tags/* ref) to the given commit
NOTE: does not support annotated tags for now; among others it would require deciding on tagger identity (at least for some backends).
| PARAMETER | DESCRIPTION |
|---|---|
tag_name
|
Name of tag to be created. Should follow
TYPE:
|
commit
|
Revision to be tagged. Defaults to 'HEAD'.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
|
Source code in src/diffannotator/utils/git.py
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 | |
diff_file_status ¶
diff_file_status(
commit: str = "HEAD",
prev: Optional[str] = None,
silence_errors: bool = True,
) -> dict[tuple[str, str], str]
Retrieve the status of file changes at the given revision in the repo
It returns in a structured way information equivalent to the one from calling 'git diff --name-status -r'.
Example output: { (None, 'added_file'): 'A', ('file_to_be_deleted', None): 'D', ('mode_changed', 'mode_changed'): 'M', ('modified', 'modified'): 'M', ('to_be_renamed', 'renamed'): 'R' }
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit for which to list changes for. Defaults to 'HEAD', that is the current commit.
TYPE:
|
prev
|
The commit for which to list changes from. If not set, then
changes are relative to the parent of the
TYPE:
|
silence_errors
|
Redirect stderr to /dev/null (e.g., about 'commit^' not existing)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[tuple[str, str], str]
|
Information about the status of each change. Returns a mapping (a dictionary), where the key is the pair (tuple) of pre-image and post-image pathname, and the value is a single letter denoting the status / type of the change. For new (added) files the pre-image path is Possible status letters are: - 'A': addition of a file, - 'C': copy of a file into a new one (not for all implementations), - 'D': deletion of a file, - 'M': modification of the contents or mode of a file, - 'R': renaming of a file, - 'T': change in the type of the file (untested). |
Source code in src/diffannotator/utils/git.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 | |
file_contents ¶
file_contents(
commit: str, path: str, encoding: Optional[str] = None
) -> str
Retrieve contents of given file at given revision / tree
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit for which to return file contents.
TYPE:
|
path
|
Path to a file, relative to the top-level of the repository
TYPE:
|
encoding
|
Encoding of the file (optional)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Contents of the file with given path at given revision |
Source code in src/diffannotator/utils/git.py
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 | |
filter_valid_commits ¶
filter_valid_commits(
commits: Iterable[str],
to_oid: bool = False,
single_use: bool = False,
) -> Iterable[str]
Filter out invalid commits from the given list of commits
Commit is considered invalid if it does not exist in the repository, or is not a commit.
| PARAMETER | DESCRIPTION |
|---|---|
commits
|
A list of commit identifiers to check
TYPE:
|
to_oid
|
Whether to convert elements in
TYPE:
|
single_use
|
If True, do not keep the connection to
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
str
|
Commit from |
| RETURNS | DESCRIPTION |
|---|---|
Iterable[str]
|
Subset of identifiers from |
Source code in src/diffannotator/utils/git.py
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 | |
find_commit_by_timestamp ¶
find_commit_by_timestamp(
timestamp: Union[str, int], start_commit: str = "HEAD"
) -> str
Find first commit in repository older than given date
| PARAMETER | DESCRIPTION |
|---|---|
timestamp
|
Date in UNIX epoch format, also known as timestamp format. Returned commit would be older than this date.
TYPE:
|
start_commit
|
The commit from which to start walking through commits, trying to find the one we want. Defaults to 'HEAD'
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Full SHA-1 identifier of found commit. WARNING: there is currently no support for error handling, among others for not finding any commit that fulfills the condition. At least it is not tested. |
Source code in src/diffannotator/utils/git.py
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
find_roots ¶
find_roots(
start_from: str = StartLogFrom.CURRENT,
) -> list[str]
Find root commits (commits without parents), starting from start_from
| PARAMETER | DESCRIPTION |
|---|---|
start_from
|
where to start from to follow 'parent' links
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
list of root commits, as SHA-1 |
Source code in src/diffannotator/utils/git.py
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
format_patch ¶
format_patch(
output_dir: Optional[PathLike] = None,
revision_range: Union[str, Iterable[str]] = (
"-1",
"HEAD",
),
) -> str
Generate patches out of specified revisions, saving them as individual files
| PARAMETER | DESCRIPTION |
|---|---|
output_dir
|
output directory for patches; if not set (the default), save patches in the current working directory
TYPE:
|
revision_range
|
arguments to pass to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
output from the |
Source code in src/diffannotator/utils/git.py
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
get_commit_metadata ¶
get_commit_metadata(
commit: str = "HEAD",
) -> dict[str, Union[str, dict, list]]
Retrieve metadata about given commit
Parameters
Parameters
commit : str
The commit to examine. Defaults to 'HEAD', that is the
current (most recent) commit.
Returns
Returns
dict
Information about selected parts of commit metadata, the
following format:
{
'id': 'f8ffd4067d1f1b902ae06c52db4867f57a424f38',
'parents': ['fe4a622e5202cd990c8ec853d56e25922f263243'],
'tree': '5347fe7b8606e7a164ab5cd355ee5d86c99796c0'
'author': {
'author': 'A U Thor <author@example.com>',
'name': 'A U Thor',
'email': 'author@example.com',
'timestamp': 1112912053,
'tz_info': '-0600',
},
'committer': {
'committer': 'C O Mitter <committer@example.com>'
'name': 'C O Mitter',
'email': 'committer@example.com',
'timestamp': 1693598847,
'tz_info': '+0200',
},
'message': 'Commit summary
Optional longer description ', }
TODO: use dataclass for result (for computed fields)
Source code in src/diffannotator/utils/git.py
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 | |
get_config ¶
get_config(
name: str, value_type: Optional[str] = None
) -> Union[str, None]
Query specific git config option
If there is no Git configuration variable named name,
then it returns None.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
name of the configuration option, for example, 'remote.origin.url' or 'user.name'
TYPE:
|
value_type
|
name of git type to canonicalize outgoing value, see https://git-scm.com/docs/git-config#Documentation/git- config.txt---typelttypegt optional
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
value of requested git configuration variable |
Source code in src/diffannotator/utils/git.py
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 | |
get_current_branch ¶
get_current_branch() -> Union[str, None]
Return short name of the current branch
It returns name of the branch, e.g. "main", rather than fully qualified name (full name), e.g. "refs/heads/main".
Will return None if there is no current branch, that is if repo is in the 'detached HEAD' state.
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
name of the current branch |
Source code in src/diffannotator/utils/git.py
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 | |
is_valid_commit ¶
is_valid_commit(commit: str) -> bool
Check if commit is present in the repository as a commit
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
reference to a commit, for example "HEAD" or "main", or "fc6db4e600d633d6fc206217e70641bbb78cbc53^"
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
whether |
Source code in src/diffannotator/utils/git.py
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 | |
list_authors_shortlog ¶
list_authors_shortlog(
start_from: str = StartLogFrom.ALL,
) -> list[Union[str, bytes]]
List all authors using git-shortlog
Summarizes the history of the project by providing the list of authors
together with their commit counts. Uses git shortlog --summary
internally.
| PARAMETER | DESCRIPTION |
|---|---|
start_from
|
where to start from to follow 'parent' links
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str | bytes]
|
list of authors together with their commit count, in the
'SPACE* |
Source code in src/diffannotator/utils/git.py
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 | |
list_changed_files ¶
list_changed_files(
commit: str = "HEAD", side: DiffSide = DiffSide.POST
) -> list[str]
Retrieve list of files changed at given revision in repo
NOTE: not tested for merge commits, especially "evil merges" with respect to file names.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit for which to list changes. Defaults to 'HEAD', that is the current commit. The changes are relative to commit^, that is the previous commit (first parent of the given commit).
TYPE:
|
side
|
Whether to use names of files in post-image (after changes) with side=DiffSide.POST, or pre-image names (before changes) with side=DiffSide.PRE. Renames are detected by Git.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
full path names of files changed in |
Source code in src/diffannotator/utils/git.py
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | |
list_core_authors ¶
list_core_authors(
start_from: str | StartLogFrom = StartLogFrom.ALL,
perc: float = 0.8,
) -> tuple[list[AuthorStat], float]
List core authors using git-shortlog, and their fraction of commits
Get list of authors contributions via 'git-shortlog' with
list_authors_shortlog, parse it with parse_shortlog_count,
and select core authors from this list with select_core_authors.
| PARAMETER | DESCRIPTION |
|---|---|
start_from
|
where to start from to follow 'parent' links
TYPE:
|
perc
|
fraction threshold for considering author a core author,
assumed to be 0.0 <=
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(list[AuthorStat], float)
|
list of core authors, sorted, and cumulative fraction of contributions of returned authors |
Source code in src/diffannotator/utils/git.py
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 | |
list_files ¶
list_files(commit: str = 'HEAD') -> list[str]
Retrieve list of files at given revision in a repository
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit for which to list all files. Defaults to 'HEAD', that is the current commit
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of full path names of all files in the repository. |
Source code in src/diffannotator/utils/git.py
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
list_tags ¶
list_tags() -> list[str]
Retrieve list of all tags in the repository
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of all tags in the repository. |
Source code in src/diffannotator/utils/git.py
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 | |
log_p ¶
log_p(
revision_range: Union[str, Iterable[str]] = ...,
wrap: Literal[True] = ...,
) -> Iterator[ChangeSet]
log_p(
revision_range: Union[str, Iterable[str]] = ...,
wrap: Literal[False] = ...,
) -> Iterator[str]
log_p(
revision_range: Union[str, Iterable[str]] = ...,
wrap: bool = ...,
) -> Union[Iterator[str], Iterator[ChangeSet]]
log_p(revision_range=('-1', 'HEAD'), wrap=True)
Generate commits with unified diffs for a given revision_range
If revision_range is not provided, it generates single most recent
commit on the current branch.
The wrap parameter controls the output format. If true (the
default), generate series of unidiff.PatchSet for commits changes.
If false, generate series of raw commit + unified diff of commit
changes (as str). This is similar to how unidiff() method works.
| PARAMETER | DESCRIPTION |
|---|---|
revision_range
|
arguments to pass to
DEFAULT:
|
wrap
|
whether to wrap the result in PatchSet / ChangeSet
DEFAULT:
|
| YIELDS | DESCRIPTION |
|---|---|
ChangeSet | str
|
the changes for given |
Source code in src/diffannotator/utils/git.py
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 | |
open_file ¶
open_file(commit: str, path: str) -> BufferedReader
Open given file at given revision / tree as binary file
Works as a context manager, like pathlib.Path.open():
>>> with GitRepo('/path/to/repo').open_file('v1', 'example_file') as fpb:
... contents = fpb.read().decode('utf8')
...
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
The commit for which to return file contents.
TYPE:
|
path
|
Path to a file, relative to the top-level of the repository
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BufferedReader
|
file object, opened in binary mode |
Source code in src/diffannotator/utils/git.py
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
resolve_symbolic_ref ¶
resolve_symbolic_ref(ref: str = 'HEAD') -> Union[str, None]
Return full name of reference ref symbolic ref points to
If ref is not symbolic reference (e.g. ref='HEAD' and detached
HEAD state) it returns None.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
name of the symbolic reference, e.g. "HEAD"
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
resolved |
Source code in src/diffannotator/utils/git.py
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 | |
reverse_blame ¶
reverse_blame(
commit: str,
file: str | PathLike,
ref_pattern: str = "HEAD",
line_extents: Optional[list[tuple[int, int]]] = None,
) -> tuple[dict, list]
Find what revision each line of file at commit was modified since
For each line of file (or subset of lines selected by line_extents),
find how long it did survive starting from commit, that is find the last
revision at which the line is yet present in the history.
The origin of lines is automatically followed across whole-file renames; currently there is no option to tell this function to follow lines as they are copied and pasted into another file (TODO).
In reverse blame, history is walked forward (from 'HEAD') instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
where to start examine history from
TYPE:
|
file
|
file to perform blame on (as a whole, or only selected lines)
TYPE:
|
ref_pattern
|
TYPE:
|
line_extents
|
which lines to blame, provided as list of line
ranges (as extents: [(
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(dict, list)
|
information about commits (dict) and information about lines (list) |
Source code in src/diffannotator/utils/git.py
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 | |
to_oid ¶
to_oid(obj: str) -> Union[str, None]
Convert object reference to object identifier
Returns None if object obj is not present in the repository
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
object reference, for example "HEAD" or "main^", see e.g. https://git-scm.com/docs/gitrevisions
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
SHA-1 identifier of object, or None if object is not found |
Source code in src/diffannotator/utils/git.py
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 | |
unidiff ¶
unidiff(
commit: str = ...,
prev: Optional[str] = ...,
wrap: Literal[True] = ...,
) -> ChangeSet
unidiff(
commit: str = ...,
prev: Optional[str] = ...,
*,
wrap: Literal[False]
) -> Union[str, bytes]
unidiff(
commit: str = ...,
prev: Optional[str] = ...,
wrap: bool = ...,
) -> Union[str, bytes, ChangeSet]
unidiff(commit='HEAD', prev=None, wrap=True)
Return unified diff between commit and prev
If prev is None (which is the default), return diff between the
commit and its first parent, or between the commit and the empty
tree if commit does not have any parents (if it is a root commit).
If wrap is True (which is the default), wrap the result in
unidiff.PatchSet to make it easier to extract information from
the diff. Otherwise, return diff as plain text.
| PARAMETER | DESCRIPTION |
|---|---|
commit
|
later (second) of two commits to compare, defaults to 'HEAD', that is the current commit
TYPE:
|
prev
|
earlier (first) of two commits to compare, defaults to None,
which means comparing to parent of
TYPE:
|
wrap
|
whether to wrap the result in PatchSet
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | bytes | ChangeSet
|
the changes between two arbitrary commits, |
Source code in src/diffannotator/utils/git.py
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 | |
StartLogFrom ¶
Bases: Enum
Enum to be used for special cases for starting point of 'git log'
Source code in src/diffannotator/utils/git.py
59 60 61 62 63 | |
changes_survival_perc ¶
changes_survival_perc(
lines_survival: dict[str, list[dict]],
) -> tuple[int, int]
Count fraction of surviving lines from GitRepo.changes_survival() output
| PARAMETER | DESCRIPTION |
|---|---|
lines_survival
|
the second element in tuple returned by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(int, int)
|
number of surviving lines and total number of lines |
Source code in src/diffannotator/utils/git.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
decode_c_quoted_str ¶
decode_c_quoted_str(text: str) -> str
C-style name unquoting
See unquote_c_style() function in 'quote.c' file in git/git source code https://github.com/git/git/blob/master/quote.c#L401
This is subset of escape sequences supported by C and C++ https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences
| PARAMETER | DESCRIPTION |
|---|---|
text
|
string which may be c-quoted
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
decoded string |
Source code in src/diffannotator/utils/git.py
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 | |
get_patched_file_mode ¶
get_patched_file_mode(
patched_file: PatchedFile,
side: DiffSide = DiffSide.POST,
) -> str | None
Extract pre- or post-image mode of a changed file from git diff, if possible
This function examines the extended git diff header of a given PatchedFile
in a PatchSet or ChangeSet created out of a git patch, in order to extract
the mode of the pre- or post-image of a changed file (according to the side
argument).
If there is not enough information in the header (for example if PatchSet was created from unified diff, and not from git diff), it returns None.
| PARAMETER | DESCRIPTION |
|---|---|
patched_file
|
unidiff.patch.PatchedFile element of unidiff.PatchSet or of ChangeSet, i.e., the result of parsing of a patch (of a unified diff) with unidiff library.
TYPE:
|
side
|
enum that select whether to return pre- or post-image mode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
Extended file mode in octal: '100644' for normal files, '100755' for
executable files ( |
Source code in src/diffannotator/utils/git.py
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 | |
maybe_close_subprocess ¶
maybe_close_subprocess(process: Optional[Popen]) -> None
Closes a subprocess safely to avoid resource warnings and resource starvation
This function ensures the safe termination of a subprocess by properly closing its
standard input and output streams, waiting for it to exit, and forcefully
killing it if necessary. It is designed to handle git cat-file --batch-command
and similar persistent subprocesses.
It is designed to be used as a weakref.finalize callback.
| PARAMETER | DESCRIPTION |
|---|---|
process
|
The subprocess instance to close. If None, the function does nothing.
TYPE:
|
Source code in src/diffannotator/utils/git.py
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | |
parse_shortlog_count ¶
parse_shortlog_count(
shortlog_lines: list[Union[str, bytes]],
) -> list[AuthorStat]
Parse the result of GitRepo.list_authors_shortlog() method
| PARAMETER | DESCRIPTION |
|---|---|
shortlog_lines
|
result of list_authors_shortlog()
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[AuthorStat]
|
list of parsed statistics, number of commits per author |
Source code in src/diffannotator/utils/git.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
select_core_authors ¶
select_core_authors(
authors_stats: list[AuthorStat], perc: float = 0.8
) -> tuple[list[AuthorStat], float]
Select sorted list of core authors from authors_list
Core authors are defined (like in World of Code) as those authors with
the greatest contribution count whose contribution sum up to more than
given perc fraction of contributions from all authors. Usually
number of contributions comes from 'git shortlog', and counts commits.
This function returns a tuple. First element is list of AuthorStat
named tuples, sorted by count field in decreasing order, so that their
contribution is minimal that covers perc fraction of all commits.
If there is tie at the last element, all tied authors are included.
Second element is actual fraction of all commits that selected authors'
contributions covers.
We have len(result[0]) <= len(authors_stats), and perc <= result[1].
| PARAMETER | DESCRIPTION |
|---|---|
authors_stats
|
all authors and their contribution statistics, for example result of feeding the result of list_authors_shortlog() method fed to parse_shortlog_count() function
TYPE:
|
perc
|
fraction threshold for considering author a core author,
assumed to be 0.0 <=
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(list[AuthorStat], float)
|
list of core authors, and cumulative fraction of contributions of returned authors |
Source code in src/diffannotator/utils/git.py
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 | |