]> git.proxmox.com Git - libgit2.git/commitdiff
patch parse: dup the patch from the callers
authorEdward Thomson <ethomson@edwardthomson.com>
Thu, 24 Sep 2015 14:32:15 +0000 (10:32 -0400)
committerEdward Thomson <ethomson@github.com>
Thu, 26 May 2016 18:01:07 +0000 (13:01 -0500)
src/patch.c
src/patch_diff.c
src/patch_parse.c

index f05cfb21add351898e6a1aa689f58caad77e15ac..e14ffa9c01f4c0930d806a3f850667c6068df41d 100644 (file)
@@ -196,12 +196,6 @@ int git_patch_get_line_in_hunk(
 
 static void git_patch__free(git_patch *patch)
 {
-       git_array_clear(patch->lines);
-       git_array_clear(patch->hunks);
-
-       git__free((char *)patch->binary.old_file.data);
-       git__free((char *)patch->binary.new_file.data);
-
        if (patch->free_fn)
                patch->free_fn(patch);
 }
index 1a3aeda5e07b9c95b9395a323eea325831fe3369..b8adcf3e158bad085c8a221f11b9d4bb6d82e0e7 100644 (file)
@@ -25,6 +25,12 @@ static void patch_diff_free(git_patch *p)
 {
        git_patch_diff *patch = (git_patch_diff *)p;
 
+    git_array_clear(patch->base.lines);
+       git_array_clear(patch->base.hunks);
+
+       git__free((char *)patch->base.binary.old_file.data);
+       git__free((char *)patch->base.binary.new_file.data);
+
        git_diff_file_content__clear(&patch->ofile);
        git_diff_file_content__clear(&patch->nfile);
 
index 418ed1e0c6afac0043ef90013c6e07413268c437..4e4e0a68ac1720de674be1799b53fbe6f1192201 100644 (file)
@@ -10,6 +10,10 @@ typedef struct {
 
        git_patch_options opts;
 
+       /* the patch contents, which lines will point into. */
+       /* TODO: allow us to point somewhere refcounted. */
+       char *content;
+
        /* the paths from the `diff --git` header, these will be used if this is not
         * a rename (and rename paths are specified) or if no `+++`/`---` line specify
         * the paths.
@@ -523,7 +527,7 @@ static int parse_hunk_body(
        int newlines = hunk->hunk.new_lines;
 
        for (;
-       ctx->remain > 4 && (oldlines || newlines) &&
+               ctx->remain > 4 && (oldlines || newlines) &&
                memcmp(ctx->line, "@@ -", 4) != 0;
                parse_advance_line(ctx)) {
 
@@ -931,6 +935,12 @@ static void patch_parsed__free(git_patch *p)
        if (!patch)
                return;
 
+       git__free((char *)patch->base.binary.old_file.data);
+       git__free((char *)patch->base.binary.new_file.data);
+       git_array_clear(patch->base.hunks);
+       git_array_clear(patch->base.lines);
+       git__free(patch->base.delta);
+
        git__free(patch->old_prefix);
        git__free(patch->new_prefix);
        git__free(patch->header_old_path);
@@ -939,9 +949,7 @@ static void patch_parsed__free(git_patch *p)
        git__free(patch->rename_new_path);
        git__free(patch->old_path);
        git__free(patch->new_path);
-       git_array_clear(patch->base.hunks);
-       git_array_clear(patch->base.lines);
-       git__free(patch->base.delta);
+       git__free(patch->content);
        git__free(patch);
 }
 
@@ -969,10 +977,19 @@ int git_patch_from_patchfile(
        patch->base.free_fn = patch_parsed__free;
 
        patch->base.delta = git__calloc(1, sizeof(git_diff_delta));
+       GITERR_CHECK_ALLOC(patch->base.delta);
+
        patch->base.delta->status = GIT_DELTA_MODIFIED;
        patch->base.delta->nfiles = 2;
 
-       ctx.content = content;
+       if (content_len) {
+               patch->content = git__malloc(content_len);
+               GITERR_CHECK_ALLOC(patch->content);
+
+               memcpy(patch->content, content, content_len);
+       }
+
+       ctx.content = patch->content;
        ctx.content_len = content_len;
        ctx.remain = content_len;