-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch default branch 'main' instead of 'master' #279
Fetch default branch 'main' instead of 'master' #279
Conversation
5b59f14
to
254d5c4
Compare
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 <[email protected]>
254d5c4
to
575ed70
Compare
Hi @lulinqing, @p3ck and @jbastian, would you please help to review this PR? Thanks! |
src/fetch_git.h
Outdated
#define GIT_BRANCH "master" | ||
#define GIT_BRANCHES_DELIMITER "," | ||
#define GIT_BRANCHES "main,master" | ||
#define GIT_BRANCHES_SIZE 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't use #define, instead just create the array here:
const char *GIT_BRANCHES[] = {"main", "master"};
But I haven't done any C in a long time, the above may need some tweaking.. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @p3ck, I added static const gchar *g_git_branches[] = {"main", "master"};
to src/fetch_git.c according to your sugesstion, please help to review again, thanks!
7cad23a
to
7f0c42c
Compare
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 <[email protected]>
7f0c42c
to
a01c29b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. If you want to keep the #define to make it easier to add additional branches I gave a suggestion in the review. But I'll leave it up to you if you want to do that.
@@ -19,7 +19,6 @@ | |||
#define _RESTRAINT_FETCH_GIT_H | |||
|
|||
#define GIT_PORT 9418 | |||
#define GIT_BRANCH "master" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can still have a define here..
#define GIT_BRANCHES "main", "master"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! GIT_BRANCHES
is added to file src/fetch_git.h
.
src/fetch_git.c
Outdated
// 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"}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can still use a define here if you wish..
static const gchar *g_git_branches[] = { GIT_BRANCHES };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. BTW, I moved the comments from fetch_git.c
to fetch_git.h
, @p3ck, please help to take a look again, thanks!
Signed-off-by: Vector Li <[email protected]>
The patch for #270 $ git diff -r 7f4bacf8f1fd3194ecdea60538f4f412721a97ea 2b0d8ab718d65bff51e07dfe77dc8f3b694789ac
diff --git a/src/fetch_git.c b/src/fetch_git.c
index 5182d89..b87bb97 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,8 @@
#include "fetch.h"
#include "fetch_git.h"
+static const gchar *g_git_branches[] = {GIT_BRANCHES};
+
static gint
packet_length(const gchar *linelen)
{
@@ -230,9 +232,16 @@ 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);
+
+ // 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 ? branch: 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 399cb47..5d3ee97 100644
--- a/src/fetch_git.h
+++ b/src/fetch_git.h
@@ -18,8 +18,13 @@
#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 GIT_BRANCH "master"
#define HDR_LEN_SIZE 4
#include <libsoup/soup.h>
|
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'.
Fixes: #270