Skip to content
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

Merged
merged 3 commits into from
Jan 12, 2023

Conversation

idorax
Copy link

@idorax idorax commented Jan 10, 2023

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

@idorax idorax force-pushed the dev.huanli.20200109.270 branch from 5b59f14 to 254d5c4 Compare January 10, 2023 04:18
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]>
@idorax idorax force-pushed the dev.huanli.20200109.270 branch from 254d5c4 to 575ed70 Compare January 10, 2023 04:21
@idorax
Copy link
Author

idorax commented Jan 10, 2023

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
Copy link
Contributor

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.. :-)

Copy link
Author

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!

@idorax idorax force-pushed the dev.huanli.20200109.270 branch from 7cad23a to 7f0c42c Compare January 11, 2023 09:34
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]>
@idorax idorax force-pushed the dev.huanli.20200109.270 branch from 7f0c42c to a01c29b Compare January 11, 2023 09:40
p3ck
p3ck previously approved these changes Jan 11, 2023
Copy link
Contributor

@p3ck p3ck left a 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"
Copy link
Contributor

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"

Copy link
Author

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"};
Copy link
Contributor

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 };

Copy link
Author

@idorax idorax Jan 12, 2023

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]>
@p3ck p3ck merged commit 2b0d8ab into restraint-harness:master Jan 12, 2023
@idorax
Copy link
Author

idorax commented Feb 20, 2023

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants