Skip to content

Commit 11fbead

Browse files
authored
Merge pull request libgit2#4705 from libgit2/ethomson/apply
Patch (diff) application
2 parents 2f5f3cf + 4e746d8 commit 11fbead

File tree

21 files changed

+3697
-21
lines changed

21 files changed

+3697
-21
lines changed

include/git2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define INCLUDE_git_git_h__
1010

1111
#include "git2/annotated_commit.h"
12+
#include "git2/apply.h"
1213
#include "git2/attr.h"
1314
#include "git2/blob.h"
1415
#include "git2/blame.h"

include/git2/apply.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_git_apply_h__
8+
#define INCLUDE_git_apply_h__
9+
10+
#include "common.h"
11+
#include "types.h"
12+
#include "oid.h"
13+
#include "diff.h"
14+
15+
/**
16+
* @file git2/apply.h
17+
* @brief Git patch application routines
18+
* @defgroup git_apply Git patch application routines
19+
* @ingroup Git
20+
* @{
21+
*/
22+
GIT_BEGIN_DECL
23+
24+
/**
25+
* When applying a patch, callback that will be made per delta (file).
26+
*
27+
* When the callback:
28+
* - returns < 0, the apply process will be aborted.
29+
* - returns > 0, the delta will not be applied, but the apply process
30+
* continues
31+
* - returns 0, the delta is applied, and the apply process continues.
32+
*
33+
* @param delta The delta to be applied
34+
* @param payload User-specified payload
35+
*/
36+
typedef int (*git_apply_delta_cb)(
37+
const git_diff_delta *delta,
38+
void *payload);
39+
40+
/**
41+
* When applying a patch, callback that will be made per hunk.
42+
*
43+
* When the callback:
44+
* - returns < 0, the apply process will be aborted.
45+
* - returns > 0, the hunk will not be applied, but the apply process
46+
* continues
47+
* - returns 0, the hunk is applied, and the apply process continues.
48+
*
49+
* @param hunk The hunk to be applied
50+
* @param payload User-specified payload
51+
*/
52+
typedef int (*git_apply_hunk_cb)(
53+
const git_diff_hunk *hunk,
54+
void *payload);
55+
56+
/**
57+
* Apply options structure
58+
*
59+
* Initialize with `GIT_APPLY_OPTIONS_INIT`. Alternatively, you can
60+
* use `git_apply_init_options`.
61+
*
62+
* @see git_apply_to_tree, git_apply
63+
*/
64+
typedef struct {
65+
unsigned int version;
66+
67+
git_apply_delta_cb delta_cb;
68+
git_apply_hunk_cb hunk_cb;
69+
void *payload;
70+
} git_apply_options;
71+
72+
#define GIT_APPLY_OPTIONS_VERSION 1
73+
#define GIT_APPLY_OPTIONS_INIT {GIT_APPLY_OPTIONS_VERSION}
74+
75+
/**
76+
* Apply a `git_diff` to a `git_tree`, and return the resulting image
77+
* as an index.
78+
*
79+
* @param out the postimage of the application
80+
* @param repo the repository to apply
81+
* @param preimage the tree to apply the diff to
82+
* @param diff the diff to apply
83+
* @param options the options for the apply (or null for defaults)
84+
*/
85+
GIT_EXTERN(int) git_apply_to_tree(
86+
git_index **out,
87+
git_repository *repo,
88+
git_tree *preimage,
89+
git_diff *diff,
90+
const git_apply_options *options);
91+
92+
typedef enum {
93+
/**
94+
* Apply the patch to the workdir, leaving the index untouched.
95+
* This is the equivalent of `git apply` with no location argument.
96+
*/
97+
GIT_APPLY_LOCATION_WORKDIR = 0,
98+
99+
/**
100+
* Apply the patch to the index, leaving the working directory
101+
* untouched. This is the equivalent of `git apply --cached`.
102+
*/
103+
GIT_APPLY_LOCATION_INDEX = 1,
104+
105+
/**
106+
* Apply the patch to both the working directory and the index.
107+
* This is the equivalent of `git apply --index`.
108+
*/
109+
GIT_APPLY_LOCATION_BOTH = 2,
110+
} git_apply_location_t;
111+
112+
/**
113+
* Apply a `git_diff` to the given repository, making changes directly
114+
* in the working directory, the index, or both.
115+
*
116+
* @param repo the repository to apply to
117+
* @param diff the diff to apply
118+
* @param location the location to apply (workdir, index or both)
119+
* @param options the options for the apply (or null for defaults)
120+
*/
121+
GIT_EXTERN(int) git_apply(
122+
git_repository *repo,
123+
git_diff *diff,
124+
git_apply_location_t location,
125+
const git_apply_options *options);
126+
127+
/** @} */
128+
GIT_END_DECL
129+
#endif

include/git2/errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef enum {
5757
GIT_RETRY = -32, /**< Internal only */
5858
GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */
5959
GIT_EINDEXDIRTY = -34, /**< Unsaved changes in the index would be overwritten */
60+
GIT_EAPPLYFAIL = -35, /**< Patch application failed */
6061
} git_error_code;
6162

6263
/**

0 commit comments

Comments
 (0)