Skip to content

Commit fe794b2

Browse files
committed
remote: remove git_push from the public API
Instead we provide git_remote_upload() and git_remote_update_tips() in order to have a parallel API for fetching and pushing.
1 parent 4eb97ef commit fe794b2

9 files changed

Lines changed: 210 additions & 179 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ v0.21 + 1
8888
* Rename git_remote_load() to git_remote_lookup() to bring it in line
8989
with the rest of the lookup functions.
9090

91-
* git_push_unpack_ok() has been removed and git_push_finish() now
92-
returns an error if the unpacking failed.
91+
* The git_push struct to perform a push has been replaced with
92+
git_remote_upload(). The refspecs and options are passed as a
93+
function argument. git_push_update_tips() is now also
94+
git_remote_update_tips() and the callbacks are in the same struct as
95+
the rest.
9396

9497
* Introduce git_merge_bases() and the git_oidarray type to expose all
9598
merge bases between two commits.

include/git2/push.h

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -59,116 +59,6 @@ typedef int (*git_push_transfer_progress)(
5959
size_t bytes,
6060
void* payload);
6161

62-
/**
63-
* Create a new push object
64-
*
65-
* @param out New push object
66-
* @param remote Remote instance
67-
*
68-
* @return 0 or an error code
69-
*/
70-
GIT_EXTERN(int) git_push_new(git_push **out, git_remote *remote);
71-
72-
/**
73-
* Set options on a push object
74-
*
75-
* @param push The push object
76-
* @param opts The options to set on the push object
77-
*
78-
* @return 0 or an error code
79-
*/
80-
GIT_EXTERN(int) git_push_set_options(
81-
git_push *push,
82-
const git_push_options *opts);
83-
84-
/**
85-
* Set the callbacks for a push
86-
*
87-
* @param push The push object
88-
* @param pack_progress_cb Function to call with progress information during
89-
* pack building. Be aware that this is called inline with pack building
90-
* operations, so performance may be affected.
91-
* @param pack_progress_cb_payload Payload for the pack progress callback.
92-
* @param transfer_progress_cb Function to call with progress information during
93-
* the upload portion of a push. Be aware that this is called inline with
94-
* pack building operations, so performance may be affected.
95-
* @param transfer_progress_cb_payload Payload for the network progress callback.
96-
* @return 0 or an error code
97-
*/
98-
GIT_EXTERN(int) git_push_set_callbacks(
99-
git_push *push,
100-
git_packbuilder_progress pack_progress_cb,
101-
void *pack_progress_cb_payload,
102-
git_push_transfer_progress transfer_progress_cb,
103-
void *transfer_progress_cb_payload);
104-
105-
/**
106-
* Add a refspec to be pushed
107-
*
108-
* @param push The push object
109-
* @param refspec Refspec string
110-
*
111-
* @return 0 or an error code
112-
*/
113-
GIT_EXTERN(int) git_push_add_refspec(git_push *push, const char *refspec);
114-
115-
/**
116-
* Update remote tips after a push
117-
*
118-
* @param push The push object
119-
* @param signature The identity to use when updating reflogs
120-
* @param reflog_message The message to insert into the reflogs. If NULL, the
121-
* default is "update by push".
122-
*
123-
* @return 0 or an error code
124-
*/
125-
GIT_EXTERN(int) git_push_update_tips(
126-
git_push *push,
127-
const git_signature *signature,
128-
const char *reflog_message);
129-
130-
/**
131-
* Perform the push
132-
*
133-
* This function will return an error in case of a protocol error or
134-
* the server being unable to unpack the data we sent.
135-
*
136-
* The return value does not reflect whether the server accepted or
137-
* refused any reference updates. Use `git_push_status_foreach()` in
138-
* order to find out which updates were accepted or rejected.
139-
*
140-
* @param push The push object
141-
*
142-
* @return 0 or an error code
143-
*/
144-
GIT_EXTERN(int) git_push_finish(git_push *push);
145-
146-
/**
147-
* Invoke callback `cb' on each status entry
148-
*
149-
* For each of the updated references, we receive a status report in the
150-
* form of `ok refs/heads/master` or `ng refs/heads/master <msg>`.
151-
* `msg != NULL` means the reference has not been updated for the given
152-
* reason.
153-
*
154-
* Return a non-zero value from the callback to stop the loop.
155-
*
156-
* @param push The push object
157-
* @param cb The callback to call on each object
158-
*
159-
* @return 0 on success, non-zero callback return value, or error code
160-
*/
161-
GIT_EXTERN(int) git_push_status_foreach(git_push *push,
162-
int (*cb)(const char *ref, const char *msg, void *data),
163-
void *data);
164-
165-
/**
166-
* Free the given push object
167-
*
168-
* @param push The push object
169-
*/
170-
GIT_EXTERN(void) git_push_free(git_push *push);
171-
17262
/** @} */
17363
GIT_END_DECL
17464
#endif

include/git2/remote.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ GIT_EXTERN(int) git_remote_ls(const git_remote_head ***out, size_t *size, git_r
319319
*/
320320
GIT_EXTERN(int) git_remote_download(git_remote *remote, const git_strarray *refspecs);
321321

322+
/**
323+
* Create a packfile and send it to the server
324+
*
325+
* Connect to the remote if it hasn't been done yet, negotiate with
326+
* the remote git which objects are missing, create a packfile with the missing objects and send it.
327+
*
328+
* @param remote the remote
329+
* @param refspecs the refspecs to use for this negotiation and
330+
* upload. Use NULL or an empty array to use the base refspecs
331+
* @return 0 or an error code
332+
*/
333+
GIT_EXTERN(int) git_remote_upload(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts);
334+
322335
/**
323336
* Check whether the remote is connected
324337
*
@@ -407,7 +420,7 @@ GIT_EXTERN(int) git_remote_fetch(
407420
* @param reflog_message message to use for the reflog of upated references
408421
*/
409422
GIT_EXTERN(int) git_remote_push(git_remote *remote,
410-
git_strarray *refspecs,
423+
const git_strarray *refspecs,
411424
const git_push_options *opts,
412425
const git_signature *signature, const char *reflog_message);
413426

src/push.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,114 @@ struct git_push {
5151
*/
5252
void git_push_status_free(push_status *status);
5353

54+
/**
55+
* Create a new push object
56+
*
57+
* @param out New push object
58+
* @param remote Remote instance
59+
*
60+
* @return 0 or an error code
61+
*/
62+
int git_push_new(git_push **out, git_remote *remote);
63+
64+
/**
65+
* Set options on a push object
66+
*
67+
* @param push The push object
68+
* @param opts The options to set on the push object
69+
*
70+
* @return 0 or an error code
71+
*/
72+
int git_push_set_options(
73+
git_push *push,
74+
const git_push_options *opts);
75+
76+
/**
77+
* Set the callbacks for a push
78+
*
79+
* @param push The push object
80+
* @param pack_progress_cb Function to call with progress information during
81+
* pack building. Be aware that this is called inline with pack building
82+
* operations, so performance may be affected.
83+
* @param pack_progress_cb_payload Payload for the pack progress callback.
84+
* @param transfer_progress_cb Function to call with progress information during
85+
* the upload portion of a push. Be aware that this is called inline with
86+
* pack building operations, so performance may be affected.
87+
* @param transfer_progress_cb_payload Payload for the network progress callback.
88+
* @return 0 or an error code
89+
*/
90+
int git_push_set_callbacks(
91+
git_push *push,
92+
git_packbuilder_progress pack_progress_cb,
93+
void *pack_progress_cb_payload,
94+
git_push_transfer_progress transfer_progress_cb,
95+
void *transfer_progress_cb_payload);
96+
97+
/**
98+
* Add a refspec to be pushed
99+
*
100+
* @param push The push object
101+
* @param refspec Refspec string
102+
*
103+
* @return 0 or an error code
104+
*/
105+
int git_push_add_refspec(git_push *push, const char *refspec);
106+
107+
/**
108+
* Update remote tips after a push
109+
*
110+
* @param push The push object
111+
* @param signature The identity to use when updating reflogs
112+
* @param reflog_message The message to insert into the reflogs. If NULL, the
113+
* default is "update by push".
114+
*
115+
* @return 0 or an error code
116+
*/
117+
int git_push_update_tips(
118+
git_push *push,
119+
const git_signature *signature,
120+
const char *reflog_message);
121+
122+
/**
123+
* Perform the push
124+
*
125+
* This function will return an error in case of a protocol error or
126+
* the server being unable to unpack the data we sent.
127+
*
128+
* The return value does not reflect whether the server accepted or
129+
* refused any reference updates. Use `git_push_status_foreach()` in
130+
* order to find out which updates were accepted or rejected.
131+
*
132+
* @param push The push object
133+
*
134+
* @return 0 or an error code
135+
*/
136+
int git_push_finish(git_push *push);
137+
138+
/**
139+
* Invoke callback `cb' on each status entry
140+
*
141+
* For each of the updated references, we receive a status report in the
142+
* form of `ok refs/heads/master` or `ng refs/heads/master <msg>`.
143+
* `msg != NULL` means the reference has not been updated for the given
144+
* reason.
145+
*
146+
* Return a non-zero value from the callback to stop the loop.
147+
*
148+
* @param push The push object
149+
* @param cb The callback to call on each object
150+
*
151+
* @return 0 on success, non-zero callback return value, or error code
152+
*/
153+
int git_push_status_foreach(git_push *push,
154+
int (*cb)(const char *ref, const char *msg, void *data),
155+
void *data);
156+
157+
/**
158+
* Free the given push object
159+
*
160+
* @param push The push object
161+
*/
162+
void git_push_free(git_push *push);
163+
54164
#endif

src/remote.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "refs.h"
1919
#include "refspec.h"
2020
#include "fetchhead.h"
21+
#include "push.h"
2122

2223
static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs);
2324

@@ -1275,6 +1276,11 @@ int git_remote_update_tips(
12751276
int error;
12761277
size_t i;
12771278

1279+
/* push has its own logic hidden away in the push object */
1280+
if (remote->push) {
1281+
return git_push_update_tips(remote->push, signature, reflog_message);
1282+
}
1283+
12781284
if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
12791285
return -1;
12801286

@@ -1355,6 +1361,7 @@ void git_remote_free(git_remote *remote)
13551361
free_refspecs(&remote->passive_refspecs);
13561362
git_vector_free(&remote->passive_refspecs);
13571363

1364+
git_push_free(remote->push);
13581365
git__free(remote->url);
13591366
git__free(remote->pushurl);
13601367
git__free(remote->name);
@@ -2117,22 +2124,29 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
21172124
return git_buf_puts(out, guess->name);
21182125
}
21192126

2120-
int git_remote_push(git_remote *remote, git_strarray *refspecs, const git_push_options *opts,
2121-
const git_signature *signature, const char *reflog_message)
2127+
int git_remote_upload(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts)
21222128
{
2123-
int error;
21242129
size_t i;
2125-
git_push *push = NULL;
2126-
git_remote_callbacks *cbs;
2130+
int error;
2131+
git_push *push;
21272132
git_refspec *spec;
2133+
git_remote_callbacks *cbs;
21282134

2129-
assert(remote && refspecs);
2135+
assert(remote);
21302136

2131-
if ((error = git_remote_connect(remote, GIT_DIRECTION_PUSH)) < 0)
2137+
if (!git_remote_connected(remote) &&
2138+
(error = git_remote_connect(remote, GIT_DIRECTION_PUSH)) < 0)
2139+
goto cleanup;
2140+
2141+
if (remote->push) {
2142+
git_push_free(remote->push);
2143+
remote->push = NULL;
2144+
}
2145+
2146+
if ((error = git_push_new(&remote->push, remote)) < 0)
21322147
return error;
21332148

2134-
if ((error = git_push_new(&push, remote)) < 0)
2135-
goto cleanup;
2149+
push = remote->push;
21362150

21372151
if (opts && (error = git_push_set_options(push, opts)) < 0)
21382152
goto cleanup;
@@ -2164,10 +2178,25 @@ int git_remote_push(git_remote *remote, git_strarray *refspecs, const git_push_o
21642178
(error = git_push_status_foreach(push, cbs->push_update_reference, cbs->payload)) < 0)
21652179
goto cleanup;
21662180

2167-
error = git_push_update_tips(push, signature, reflog_message);
2168-
21692181
cleanup:
2182+
return error;
2183+
}
2184+
2185+
int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts,
2186+
const git_signature *signature, const char *reflog_message)
2187+
{
2188+
int error;
2189+
2190+
assert(remote && refspecs);
2191+
2192+
if ((error = git_remote_connect(remote, GIT_DIRECTION_PUSH)) < 0)
2193+
return error;
2194+
2195+
if ((error = git_remote_upload(remote, refspecs, opts)) < 0)
2196+
return error;
2197+
2198+
error = git_remote_update_tips(remote, signature, reflog_message);
2199+
21702200
git_remote_disconnect(remote);
2171-
git_push_free(push);
21722201
return error;
21732202
}

src/remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct git_remote {
2828
void *transport_cb_payload;
2929
git_transport *transport;
3030
git_repository *repo;
31+
git_push *push;
3132
git_remote_callbacks callbacks;
3233
git_transfer_progress stats;
3334
unsigned int need_pack;

0 commit comments

Comments
 (0)