@matheusbsbs / matheus-bagliano / commits / 3dfaf7bcfd
Fork 0

http-push: add support for deleting remote branches

Processes new command-line arguments -d and -D to remove a remote branch if the following conditions are met: - one branch name is present on the command line - the specified branch name matches exactly one remote branch name - the remote HEAD is a symref - the specified branch is not the remote HEAD - the remote HEAD resolves to an object that exists locally (-d only) - the specified branch resolves to an object that exists locally (-d only) - the specified branch is an ancestor of the remote HEAD (-d only) Signed-off-by: Nick Hengeveld <nickh@reactrix.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Mar 20, 2006 at 10:31 UTC 3dfaf7bcfd116ac180df8405e700a20d7159702b
1 file changed +218 -1
http-push.c
+218 -1
@@ -7,6 +7,7 @@
7 #include "http.h"
8 #include "refs.h"
9 #include "revision.h"
10 +#include "exec_cmd.h"
11
12 #include <expat.h>
13
@@ -32,6 +33,7 @@ enum XML_Status {
33 #define DAV_PROPFIND "PROPFIND"
34 #define DAV_PUT "PUT"
35 #define DAV_UNLOCK "UNLOCK"
36 +#define DAV_DELETE "DELETE"
37
38 /* DAV lock flags */
39 #define DAV_PROP_LOCKWR (1u << 0)
@@ -64,6 +66,9 @@ enum XML_Status {
66 #define FETCHING (1u << 7)
67 #define PUSHING (1u << 8)
68
69 +/* We allow "recursive" symbolic refs. Only within reason, though */
70 +#define MAXDEPTH 5
71 +
72 static int pushing = 0;
73 static int aborted = 0;
74 static char remote_dir_exists[256];
@@ -2103,6 +2108,197 @@ static int remote_exists(const char *path)
2108 return -1;
2109 }
2110
2111 +static void fetch_symref(char *path, char **symref, unsigned char *sha1)
2112 +{
2113 + char *url;
2114 + struct buffer buffer;
2115 + struct active_request_slot *slot;
2116 + struct slot_results results;
2117 +
2118 + url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2119 + sprintf(url, "%s%s", remote->url, path);
2120 +
2121 + buffer.size = 4096;
2122 + buffer.posn = 0;
2123 + buffer.buffer = xmalloc(buffer.size);
2124 +
2125 + slot = get_active_slot();
2126 + slot->results = &results;
2127 + curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2128 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2129 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2130 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2131 + if (start_active_slot(slot)) {
2132 + run_active_slot(slot);
2133 + if (results.curl_result != CURLE_OK) {
2134 + die("Couldn't get %s for remote symref\n%s",
2135 + url, curl_errorstr);
2136 + }
2137 + } else {
2138 + die("Unable to start remote symref request");
2139 + }
2140 + free(url);
2141 +
2142 + if (*symref != NULL)
2143 + free(*symref);
2144 + *symref = NULL;
2145 + memset(sha1, 0, 20);
2146 +
2147 + if (buffer.posn == 0)
2148 + return;
2149 +
2150 + /* If it's a symref, set the refname; otherwise try for a sha1 */
2151 + if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
2152 + *symref = xcalloc(buffer.posn - 5, 1);
2153 + strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 6);
2154 + } else {
2155 + get_sha1_hex(buffer.buffer, sha1);
2156 + }
2157 +
2158 + free(buffer.buffer);
2159 +}
2160 +
2161 +static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2162 +{
2163 + int pipe_fd[2];
2164 + pid_t merge_base_pid;
2165 + char line[PATH_MAX + 20];
2166 + unsigned char merge_sha1[20];
2167 + int verified = 0;
2168 +
2169 + if (pipe(pipe_fd) < 0)
2170 + die("Verify merge base: pipe failed");
2171 +
2172 + merge_base_pid = fork();
2173 + if (!merge_base_pid) {
2174 + static const char *args[] = {
2175 + "merge-base",
2176 + "-a",
2177 + NULL,
2178 + NULL,
2179 + NULL
2180 + };
2181 + args[2] = strdup(sha1_to_hex(head_sha1));
2182 + args[3] = sha1_to_hex(branch_sha1);
2183 +
2184 + dup2(pipe_fd[1], 1);
2185 + close(pipe_fd[0]);
2186 + close(pipe_fd[1]);
2187 + execv_git_cmd(args);
2188 + die("merge-base setup failed");
2189 + }
2190 + if (merge_base_pid < 0)
2191 + die("merge-base fork failed");
2192 +
2193 + dup2(pipe_fd[0], 0);
2194 + close(pipe_fd[0]);
2195 + close(pipe_fd[1]);
2196 + while (fgets(line, sizeof(line), stdin) != NULL) {
2197 + if (get_sha1_hex(line, merge_sha1))
2198 + die("expected sha1, got garbage:\n %s", line);
2199 + if (!memcmp(branch_sha1, merge_sha1, 20)) {
2200 + verified = 1;
2201 + break;
2202 + }
2203 + }
2204 +
2205 + return verified;
2206 +}
2207 +
2208 +static int delete_remote_branch(char *pattern, int force)
2209 +{
2210 + struct ref *refs = remote_refs;
2211 + struct ref *remote_ref = NULL;
2212 + unsigned char head_sha1[20];
2213 + char *symref = NULL;
2214 + int match;
2215 + int patlen = strlen(pattern);
2216 + int i;
2217 + struct active_request_slot *slot;
2218 + struct slot_results results;
2219 + char *url;
2220 +
2221 + /* Find the remote branch(es) matching the specified branch name */
2222 + for (match = 0; refs; refs = refs->next) {
2223 + char *name = refs->name;
2224 + int namelen = strlen(name);
2225 + if (namelen < patlen ||
2226 + memcmp(name + namelen - patlen, pattern, patlen))
2227 + continue;
2228 + if (namelen != patlen && name[namelen - patlen - 1] != '/')
2229 + continue;
2230 + match++;
2231 + remote_ref = refs;
2232 + }
2233 + if (match == 0)
2234 + return error("No remote branch matches %s", pattern);
2235 + if (match != 1)
2236 + return error("More than one remote branch matches %s",
2237 + pattern);
2238 +
2239 + /*
2240 + * Remote HEAD must be a symref (not exactly foolproof; a remote
2241 + * symlink to a symref will look like a symref)
2242 + */
2243 + fetch_symref("HEAD", &symref, head_sha1);
2244 + if (!symref)
2245 + return error("Remote HEAD is not a symref");
2246 +
2247 + /* Remote branch must not be the remote HEAD */
2248 + for (i=0; symref && i<MAXDEPTH; i++) {
2249 + if (!strcmp(remote_ref->name, symref))
2250 + return error("Remote branch %s is the current HEAD",
2251 + remote_ref->name);
2252 + fetch_symref(symref, &symref, head_sha1);
2253 + }
2254 +
2255 + /* Run extra sanity checks if delete is not forced */
2256 + if (!force) {
2257 + /* Remote HEAD must resolve to a known object */
2258 + if (symref)
2259 + return error("Remote HEAD symrefs too deep");
2260 + if (is_zero_sha1(head_sha1))
2261 + return error("Unable to resolve remote HEAD");
2262 + if (!has_sha1_file(head_sha1))
2263 + return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2264 +
2265 + /* Remote branch must resolve to a known object */
2266 + if (is_zero_sha1(remote_ref->old_sha1))
2267 + return error("Unable to resolve remote branch %s",
2268 + remote_ref->name);
2269 + if (!has_sha1_file(remote_ref->old_sha1))
2270 + return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
2271 +
2272 + /* Remote branch must be an ancestor of remote HEAD */
2273 + if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2274 + return error("The branch '%s' is not a strict subset of your current HEAD.\nIf you are sure you want to delete it, run:\n\t'git http-push -D %s %s'", remote_ref->name, remote->url, pattern);
2275 + }
2276 + }
2277 +
2278 + /* Send delete request */
2279 + fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2280 + url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2281 + sprintf(url, "%s%s", remote->url, remote_ref->name);
2282 + slot = get_active_slot();
2283 + slot->results = &results;
2284 + curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2285 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2286 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2287 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2288 + if (start_active_slot(slot)) {
2289 + run_active_slot(slot);
2290 + free(url);
2291 + if (results.curl_result != CURLE_OK)
2292 + return error("DELETE request failed (%d/%ld)\n",
2293 + results.curl_result, results.http_code);
2294 + } else {
2295 + free(url);
2296 + return error("Unable to start DELETE request");
2297 + }
2298 +
2299 + return 0;
2300 +}
2301 +
2302 int main(int argc, char **argv)
2303 {
2304 struct transfer_request *request;
@@ -2112,6 +2308,8 @@ int main(int argc, char **argv)
2308 struct remote_lock *ref_lock = NULL;
2309 struct remote_lock *info_ref_lock = NULL;
2310 struct rev_info revs;
2311 + int delete_branch = 0;
2312 + int force_delete = 0;
2313 int objects_to_send;
2314 int rc = 0;
2315 int i;
@@ -2138,7 +2336,15 @@ int main(int argc, char **argv)
2336 push_verbosely = 1;
2337 continue;
2338 }
2141 - usage(http_push_usage);
2339 + if (!strcmp(arg, "-d")) {
2340 + delete_branch = 1;
2341 + continue;
2342 + }
2343 + if (!strcmp(arg, "-D")) {
2344 + delete_branch = 1;
2345 + force_delete = 1;
2346 + continue;
2347 + }
2348 }
2349 if (!remote->url) {
2350 remote->url = arg;
@@ -2158,6 +2364,9 @@ int main(int argc, char **argv)
2364 if (!remote->url)
2365 usage(http_push_usage);
2366
2367 + if (delete_branch && nr_refspec != 1)
2368 + die("You must specify only one branch name when deleting a remote branch");
2369 +
2370 memset(remote_dir_exists, -1, 256);
2371
2372 http_init();
@@ -2193,6 +2402,14 @@ int main(int argc, char **argv)
2402 fprintf(stderr, "Fetching remote heads...\n");
2403 get_dav_remote_heads();
2404
2405 + /* Remove a remote branch if -d or -D was specified */
2406 + if (delete_branch) {
2407 + if (delete_remote_branch(refspec[0], force_delete) == -1)
2408 + fprintf(stderr, "Unable to delete remote branch %s\n",
2409 + refspec[0]);
2410 + goto cleanup;
2411 + }
2412 +
2413 /* match them up */
2414 if (!remote_tail)
2415 remote_tail = &remote_refs;