MCPcopy Index your code
hub / github.com/git/git / parse_single_patch

Function parse_single_patch

apply.c:1912–1978  ·  view source on GitHub ↗

* We have seen "diff --git a/... b/..." header (or a traditional patch * header). Read hunks that belong to this patch into fragments and hang * them to the given patch structure. * * The (fragment->patch, fragment->size) pair points into the memory given * by the caller, not a copy, when we return. * * Returns: * -1 in case of error, * the number of bytes in the patch otherwise. */

Source from the content-addressed store, hash-verified

1910 * the number of bytes in the patch otherwise.
1911 */
1912static int parse_single_patch(struct apply_state *state,
1913 const char *line,
1914 unsigned long size,
1915 struct patch *patch)
1916{
1917 unsigned long offset = 0;
1918 unsigned long oldlines = 0, newlines = 0, context = 0;
1919 struct fragment **fragp = &patch->fragments;
1920
1921 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1922 struct fragment *fragment;
1923 int len;
1924
1925 CALLOC_ARRAY(fragment, 1);
1926 fragment->linenr = state->linenr;
1927 len = parse_fragment(state, line, size, patch, fragment);
1928 if (len <= 0) {
1929 free(fragment);
1930 return error(_("corrupt patch at %s:%d"),
1931 state->patch_input_file, state->linenr);
1932 }
1933 fragment->patch = line;
1934 fragment->size = len;
1935 oldlines += fragment->oldlines;
1936 newlines += fragment->newlines;
1937 context += fragment->leading + fragment->trailing;
1938
1939 *fragp = fragment;
1940 fragp = &fragment->next;
1941
1942 offset += len;
1943 line += len;
1944 size -= len;
1945 }
1946
1947 /*
1948 * If something was removed (i.e. we have old-lines) it cannot
1949 * be creation, and if something was added it cannot be
1950 * deletion. However, the reverse is not true; --unified=0
1951 * patches that only add are not necessarily creation even
1952 * though they do not have any old lines, and ones that only
1953 * delete are not necessarily deletion.
1954 *
1955 * Unfortunately, a real creation/deletion patch do _not_ have
1956 * any context line by definition, so we cannot safely tell it
1957 * apart with --unified=0 insanity. At least if the patch has
1958 * more than one hunk it is not creation or deletion.
1959 */
1960 if (patch->is_new < 0 &&
1961 (oldlines || (patch->fragments && patch->fragments->next)))
1962 patch->is_new = 0;
1963 if (patch->is_delete < 0 &&
1964 (newlines || (patch->fragments && patch->fragments->next)))
1965 patch->is_delete = 0;
1966
1967 if (0 < patch->is_new && oldlines)
1968 return error(_("new file %s depends on old contents"), patch->new_name);
1969 if (0 < patch->is_delete && newlines)

Callers 1

parse_chunkFunction · 0.85

Calls 3

parse_fragmentFunction · 0.85
errorFunction · 0.85
fprintf_lnFunction · 0.85

Tested by

no test coverage detected