Skip to content

Commit

Permalink
Use global variable instead of macros
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Vector Li committed Jan 11, 2023
1 parent 575ed70 commit 7f0c42c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
20 changes: 9 additions & 11 deletions src/fetch_git.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of Restraint.
Restraint is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -30,6 +30,10 @@
#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 repos.
static const gchar *g_git_branches[] = {"main", "master"};

static gint
packet_length(const gchar *linelen)
{
Expand Down Expand Up @@ -231,17 +235,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;
Expand Down
3 changes: 0 additions & 3 deletions src/fetch_git.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <libsoup/soup.h>
Expand Down

0 comments on commit 7f0c42c

Please sign in to comment.