Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/gitrekt/c_src/geef.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ static ErlNifFunc geef_funcs[] =
{"repository_get_odb", 1, geef_repository_odb, 0},
{"repository_get_index", 1, geef_repository_index, 0},
{"repository_get_config", 1, geef_repository_config, 0},
{"repository_set_head", 2, geef_repository_set_head, 0},
{"odb_object_hash", 2, geef_odb_hash, 0},
{"odb_object_exists?", 2, geef_odb_exists, 0},
{"odb_read", 2, geef_odb_read, 0},
Expand Down
26 changes: 25 additions & 1 deletion apps/gitrekt/c_src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,28 @@ geef_repository_index(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
enif_release_resource(index);

return enif_make_tuple2(env, atoms.ok, term_index);
}
}

ERL_NIF_TERM
geef_repository_set_head(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

We could use that for letting the repository owner/admin change the default branch in the repository settings.

{
int error;
geef_repository *repo;
ErlNifBinary bin;

if (!enif_get_resource(env, argv[0], geef_repository_type, (void **)&repo))
return enif_make_badarg(env);

if (!enif_inspect_binary(env, argv[1], &bin))
return enif_make_badarg(env);

if (!geef_terminate_binary(&bin))
return geef_oom(env);

error = git_repository_set_head(repo->repo, (char *)bin.data);
enif_release_binary(&bin);
if (error < 0)
return geef_error_struct(env, error);

return atoms.ok;
}
1 change: 1 addition & 0 deletions apps/gitrekt/c_src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ERL_NIF_TERM geef_repository_is_empty(ErlNifEnv *env, int argc, const ERL_NIF_TE
ERL_NIF_TERM geef_repository_odb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_repository_index(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_repository_config(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_repository_set_head(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

void geef_repository_free(ErlNifEnv *env, void *cd);

Expand Down
8 changes: 8 additions & 0 deletions apps/gitrekt/lib/gitrekt/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ defmodule GitRekt.Git do
raise Code.LoadError, file: nif_path() <> ".so"
end

@doc """
Make the `repo` HEAD point to the specified reference.
"""
@spec repository_set_head(repo, binary) :: :ok | {:error, term}
def repository_set_head(_repo, _refname) do
raise Code.LoadError, file: nif_path() <> ".so"
end

@doc """
Initializes a new repository at the given `path`.
"""
Expand Down
11 changes: 10 additions & 1 deletion apps/gitrekt/lib/gitrekt/git_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,16 @@ defmodule GitRekt.GitAgent do
{:ok, name, shorthand, oid} ->
{:ok, resolve_reference({name, shorthand, :oid, oid})}
{:error, reason} ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of error are you getting here? And how doe it happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of error are you getting here? And how doe it happen?

Refer to the third paragraph of this comment: #87 (comment)

Reproduction steps:

  1. Create a new repository Test on git.limo.
  2. Create a local repository Test with only one branch main as default branch.
  3. Add git.limo Test repository as local repository's remote and git push -u origin main.
  4. Access Test repository at git.limo on web.

eg: https://git.limo/edmondfrank/Test

{:error, reason}
case Git.reference_stream(handle, "refs/heads/*") do
{:ok, stream} ->
with %GitRef{name: name} =
ref <- Enum.at(Stream.map(stream, &resolve_reference_peel!(&1, :undefined, handle)), 0),
:ok <- Git.repository_set_head(handle, "refs/heads/#{name}") do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is a good idea ☺️.

I'd prefer to fix the issue upfront over implicitly updating the HEAD here.

{:ok, ref}
end || {:error, reason}
{:error, reason} ->
{:error, reason}
end
end
end

Expand Down