From 575ed70dfc29bf8b5b90044ebdeae1de5205c4bb Mon Sep 17 00:00:00 2001 From: Vector Li Date: Tue, 10 Jan 2023 12:04:30 +0800 Subject: [PATCH 1/3] Fetch default branch 'main' instead of 'master' Support to fetch branch 'master' as well if it fails to fetch branch 'main' as the default branch of some old/legacy repos is 'master'. Signed-off-by: Vector Li --- src/fetch_git.c | 19 ++++++++++++++++--- src/fetch_git.h | 4 +++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/fetch_git.c b/src/fetch_git.c index 5182d896..0e26e241 100644 --- a/src/fetch_git.c +++ b/src/fetch_git.c @@ -230,9 +230,22 @@ myopen(FetchData *fetch_data, GError **error) "While writing to %s: ", fetch_data->url->host); goto error; } - write_succeeded = packet_write(fetch_data->ostream, &tmp_error, "argument %s:%s\0", - fetch_data->url->query == NULL ? GIT_BRANCH : fetch_data->url->query, - fetch_data->url->fragment == NULL ? "" : fetch_data->url->fragment + fragment_offset); + + // get all branches and have a try one by one + char *branches[GIT_BRANCHES_SIZE + 1] = {}; + char *branch = strtok(GIT_BRANCHES, GIT_BRANCHES_DELIMITER); + int i = 0; + while (branch != NULL && i < GIT_BRANCHES_SIZE) { + branches[i++] = branch; + branch = strtok(NULL, GIT_BRANCHES_DELIMITER); + } + for (i = 0; i < GIT_BRANCHES_SIZE; i++) { + write_succeeded = packet_write(fetch_data->ostream, &tmp_error, "argument %s:%s\0", + fetch_data->url->query == NULL ? branches[i]: fetch_data->url->query, + fetch_data->url->fragment == NULL ? "" : fetch_data->url->fragment + fragment_offset); + if (write_succeeded) + break; + } if (!write_succeeded) { g_propagate_prefixed_error(error, tmp_error, "While writing to %s: ", fetch_data->url->host); diff --git a/src/fetch_git.h b/src/fetch_git.h index 399cb479..4b033873 100644 --- a/src/fetch_git.h +++ b/src/fetch_git.h @@ -19,7 +19,9 @@ #define _RESTRAINT_FETCH_GIT_H #define GIT_PORT 9418 -#define GIT_BRANCH "master" +#define GIT_BRANCHES_DELIMITER "," +#define GIT_BRANCHES "main,master" +#define GIT_BRANCHES_SIZE 2 #define HDR_LEN_SIZE 4 #include From a01c29ba37d48ce673511f55d7a72fefd8ff19f2 Mon Sep 17 00:00:00 2001 From: Vector Li Date: Wed, 11 Jan 2023 17:18:54 +0800 Subject: [PATCH 2/3] Use global variable instead of macros Note that a global variable is used as it can be initialized as: static const gchar *g_git_branches[] = {"main", "master"}; which is much simpler than using macros. Signed-off-by: Vector Li --- src/fetch_git.c | 21 ++++++++++----------- src/fetch_git.h | 3 --- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/fetch_git.c b/src/fetch_git.c index 0e26e241..296a8874 100644 --- a/src/fetch_git.c +++ b/src/fetch_git.c @@ -1,4 +1,4 @@ -/* +/* This file is part of Restraint. Restraint is free software: you can redistribute it and/or modify @@ -30,6 +30,11 @@ #include "fetch.h" #include "fetch_git.h" +// The main branch ("main") is fetched by default, but we also support to +// fetch the legacy one (i.e. "master") because it is required by some old +// repositories. +static const gchar *g_git_branches[] = {"main", "master"}; + static gint packet_length(const gchar *linelen) { @@ -231,17 +236,11 @@ myopen(FetchData *fetch_data, GError **error) goto error; } - // get all branches and have a try one by one - char *branches[GIT_BRANCHES_SIZE + 1] = {}; - char *branch = strtok(GIT_BRANCHES, GIT_BRANCHES_DELIMITER); - int i = 0; - while (branch != NULL && i < GIT_BRANCHES_SIZE) { - branches[i++] = branch; - branch = strtok(NULL, GIT_BRANCHES_DELIMITER); - } - for (i = 0; i < GIT_BRANCHES_SIZE; i++) { + // traverse git branches one by one + for (gint i = 0; i < sizeof (g_git_branches) / sizeof (const gchar *); i++) { + gchar *branch = (gchar *)(g_git_branches[i]); write_succeeded = packet_write(fetch_data->ostream, &tmp_error, "argument %s:%s\0", - fetch_data->url->query == NULL ? branches[i]: fetch_data->url->query, + fetch_data->url->query == NULL ? branch: fetch_data->url->query, fetch_data->url->fragment == NULL ? "" : fetch_data->url->fragment + fragment_offset); if (write_succeeded) break; diff --git a/src/fetch_git.h b/src/fetch_git.h index 4b033873..83a73fb6 100644 --- a/src/fetch_git.h +++ b/src/fetch_git.h @@ -19,9 +19,6 @@ #define _RESTRAINT_FETCH_GIT_H #define GIT_PORT 9418 -#define GIT_BRANCHES_DELIMITER "," -#define GIT_BRANCHES "main,master" -#define GIT_BRANCHES_SIZE 2 #define HDR_LEN_SIZE 4 #include From c940af1f2c98de053ee90b4a878ff95a55380882 Mon Sep 17 00:00:00 2001 From: Vector Li Date: Thu, 12 Jan 2023 10:07:38 +0800 Subject: [PATCH 3/3] Use macro GIT_BRANCHES Signed-off-by: Vector Li --- src/fetch_git.c | 5 +---- src/fetch_git.h | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/fetch_git.c b/src/fetch_git.c index 296a8874..b87bb97a 100644 --- a/src/fetch_git.c +++ b/src/fetch_git.c @@ -30,10 +30,7 @@ #include "fetch.h" #include "fetch_git.h" -// The main branch ("main") is fetched by default, but we also support to -// fetch the legacy one (i.e. "master") because it is required by some old -// repositories. -static const gchar *g_git_branches[] = {"main", "master"}; +static const gchar *g_git_branches[] = {GIT_BRANCHES}; static gint packet_length(const gchar *linelen) diff --git a/src/fetch_git.h b/src/fetch_git.h index 83a73fb6..5d3ee979 100644 --- a/src/fetch_git.h +++ b/src/fetch_git.h @@ -18,6 +18,12 @@ #ifndef _RESTRAINT_FETCH_GIT_H #define _RESTRAINT_FETCH_GIT_H +/* + * The main branch ("main") is fetched by default, but we also support to + * fetch the legacy one (i.e. "master") because it is required by some old + * repositories. + */ +#define GIT_BRANCHES "main", "master" #define GIT_PORT 9418 #define HDR_LEN_SIZE 4