Skip to content

Commit d6617c7

Browse files
andre-rosagitster
authored andcommitted
Error out when user doesn't have access permission to the repository
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
1 parent 63d285c commit d6617c7

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

builtin-fetch--tool.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,14 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
511511
if (!strcmp("append-fetch-head", argv[1])) {
512512
int result;
513513
FILE *fp;
514+
char *filename;
514515

515516
if (argc != 8)
516517
return error("append-fetch-head takes 6 args");
517-
fp = fopen(git_path("FETCH_HEAD"), "a");
518+
filename = git_path("FETCH_HEAD");
519+
fp = fopen(filename, "a");
520+
if (!fp)
521+
return error("cannot open %s: %s\n", filename, strerror(errno));
518522
result = append_fetch_head(fp, argv[2], argv[3],
519523
argv[4], argv[5],
520524
argv[6], !!argv[7][0],
@@ -525,10 +529,14 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
525529
if (!strcmp("native-store", argv[1])) {
526530
int result;
527531
FILE *fp;
532+
char *filename;
528533

529534
if (argc != 5)
530535
return error("fetch-native-store takes 3 args");
531-
fp = fopen(git_path("FETCH_HEAD"), "a");
536+
filename = git_path("FETCH_HEAD");
537+
fp = fopen(filename, "a");
538+
if (!fp)
539+
return error("cannot open %s: %s\n", filename, strerror(errno));
532540
result = fetch_native_store(fp, argv[2], argv[3], argv[4],
533541
verbose, force);
534542
fclose(fp);

builtin-fetch.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,19 @@ static int update_local_ref(struct ref *ref,
255255
}
256256
}
257257

258-
static void store_updated_refs(const char *url, struct ref *ref_map)
258+
static int store_updated_refs(const char *url, struct ref *ref_map)
259259
{
260260
FILE *fp;
261261
struct commit *commit;
262262
int url_len, i, note_len, shown_url = 0;
263263
char note[1024];
264264
const char *what, *kind;
265265
struct ref *rm;
266+
char *filename = git_path("FETCH_HEAD");
266267

267-
fp = fopen(git_path("FETCH_HEAD"), "a");
268+
fp = fopen(filename, "a");
269+
if (!fp)
270+
return error("cannot open %s: %s\n", filename, strerror(errno));
268271
for (rm = ref_map; rm; rm = rm->next) {
269272
struct ref *ref = NULL;
270273

@@ -335,6 +338,7 @@ static void store_updated_refs(const char *url, struct ref *ref_map)
335338
}
336339
}
337340
fclose(fp);
341+
return 0;
338342
}
339343

340344
/*
@@ -404,7 +408,7 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
404408
if (ret)
405409
ret = transport_fetch_refs(transport, ref_map);
406410
if (!ret)
407-
store_updated_refs(transport->url, ref_map);
411+
ret |= store_updated_refs(transport->url, ref_map);
408412
transport_unlock_pack(transport);
409413
return ret;
410414
}
@@ -487,8 +491,13 @@ static int do_fetch(struct transport *transport,
487491
die("Don't know how to fetch from %s", transport->url);
488492

489493
/* if not appending, truncate FETCH_HEAD */
490-
if (!append)
491-
fclose(fopen(git_path("FETCH_HEAD"), "w"));
494+
if (!append) {
495+
char *filename = git_path("FETCH_HEAD");
496+
FILE *fp = fopen(filename, "w");
497+
if (!fp)
498+
return error("cannot open %s: %s\n", filename, strerror(errno));
499+
fclose(fp);
500+
}
492501

493502
ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
494503

0 commit comments

Comments
 (0)