mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 18:03:14 +08:00
gj_common: Do not hardcode the 'master' branch
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <git2.h>
|
#include <git2.h>
|
||||||
|
|
||||||
@ -347,12 +348,30 @@ cleanup:
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: What if the 'HEAD" does not point to 'master'
|
char *gj_branch_name(git_repository *repo)
|
||||||
|
{
|
||||||
|
int err = 0;
|
||||||
|
git_reference *ref = NULL;
|
||||||
|
char *shorthand = NULL;
|
||||||
|
|
||||||
|
err = git_repository_head(&ref, repo);
|
||||||
|
if (err < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
shorthand = (char *)git_reference_shorthand(ref);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
git_reference_free(ref);
|
||||||
|
|
||||||
|
return shorthand;
|
||||||
|
}
|
||||||
|
|
||||||
int gj_git_push(const char *git_base_path)
|
int gj_git_push(const char *git_base_path)
|
||||||
{
|
{
|
||||||
int err = 0;
|
int err = 0;
|
||||||
git_repository *repo = NULL;
|
git_repository *repo = NULL;
|
||||||
git_remote *remote = NULL;
|
git_remote *remote = NULL;
|
||||||
|
char *name = NULL;
|
||||||
|
|
||||||
err = git_repository_open(&repo, git_base_path);
|
err = git_repository_open(&repo, git_base_path);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
@ -362,7 +381,15 @@ int gj_git_push(const char *git_base_path)
|
|||||||
if (err < 0)
|
if (err < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
char *name = "refs/heads/master";
|
// Get remote name
|
||||||
|
const char *branch_name = gj_branch_name(repo);
|
||||||
|
char *base_name = "refs/heads/";
|
||||||
|
|
||||||
|
int name_length = strlen(base_name) + strlen(branch_name);
|
||||||
|
name = (char *)malloc(name_length);
|
||||||
|
strcpy(name, base_name);
|
||||||
|
strcat(name, branch_name);
|
||||||
|
|
||||||
const git_strarray refs = {&name, 1};
|
const git_strarray refs = {&name, 1};
|
||||||
|
|
||||||
git_push_options options = GIT_PUSH_OPTIONS_INIT;
|
git_push_options options = GIT_PUSH_OPTIONS_INIT;
|
||||||
@ -376,6 +403,7 @@ int gj_git_push(const char *git_base_path)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
free(name);
|
||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
@ -434,6 +462,7 @@ int gj_git_pull(const char *git_base_path, const char *author_name, const char *
|
|||||||
git_commit *origin_head_commit = NULL;
|
git_commit *origin_head_commit = NULL;
|
||||||
git_tree *tree = NULL;
|
git_tree *tree = NULL;
|
||||||
git_oid tree_id;
|
git_oid tree_id;
|
||||||
|
char *name = NULL;
|
||||||
|
|
||||||
err = git_repository_open(&repo, git_base_path);
|
err = git_repository_open(&repo, git_base_path);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
@ -460,8 +489,16 @@ int gj_git_pull(const char *git_base_path, const char *author_name, const char *
|
|||||||
if (err < 0)
|
if (err < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
// FIXME: Do not hardcode the master branch!
|
// Get remote name
|
||||||
err = git_reference_lookup(&origin_head_ref, repo, "refs/remotes/origin/master");
|
const char *branch_name = gj_branch_name(repo);
|
||||||
|
char *base_name = "refs/remotes/origin/";
|
||||||
|
|
||||||
|
int name_length = strlen(base_name) + strlen(branch_name);
|
||||||
|
name = (char *)malloc(name_length);
|
||||||
|
strcpy(name, base_name);
|
||||||
|
strcat(name, branch_name);
|
||||||
|
|
||||||
|
err = git_reference_lookup(&origin_head_ref, repo, name);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
@ -606,6 +643,7 @@ cleanup:
|
|||||||
git_annotated_commit_free(origin_annotated_commit);
|
git_annotated_commit_free(origin_annotated_commit);
|
||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
free(name);
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user