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

Tag strings without a known encoding as ASCII-8BIT. #619

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions ext/rugged/rugged.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static VALUE rb_git_prettify_message(int argc, VALUE *argv, VALUE self)
StringValueCStr(rb_message), strip_comments, comment_char);

if (!error)
result = rb_enc_str_new(message.ptr, message.size, rb_utf8_encoding());
result = rb_str_new(message.ptr, message.size);

git_buf_free(&message);
rugged_exception_check(error);
Expand Down Expand Up @@ -391,7 +391,7 @@ VALUE rugged_strarray_to_rb_ary(git_strarray *str_array)
size_t i;

for (i = 0; i < str_array->count; ++i) {
rb_ary_push(rb_array, rb_str_new_utf8(str_array->strings[i]));
rb_ary_push(rb_array, rb_str_new2(str_array->strings[i]));
}

return rb_array;
Expand Down
3 changes: 1 addition & 2 deletions ext/rugged/rugged.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <git2.h>
#include <git2/odb_backend.h>

#define rb_str_new_utf8(str) rb_enc_str_new(str, strlen(str), rb_utf8_encoding())
#define CSTR2SYM(s) (ID2SYM(rb_intern((s))))

/*
Expand Down Expand Up @@ -81,7 +80,7 @@ VALUE rb_git_object_init(git_otype type, int argc, VALUE *argv, VALUE self);

VALUE rugged_raw_read(git_repository *repo, const git_oid *oid);

VALUE rugged_signature_new(const git_signature *sig, const char *encoding_name);
VALUE rugged_signature_new(const git_signature *sig);

VALUE rugged_repo_new(VALUE klass, git_repository *repo);
VALUE rugged_index_new(VALUE klass, VALUE owner, git_index *index);
Expand Down
4 changes: 2 additions & 2 deletions ext/rugged/rugged_blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ static VALUE rb_git_blame_hunk_fromC(const git_blame_hunk *hunk)

rb_hash_aset(rb_hunk, CSTR2SYM("final_commit_id"), rugged_create_oid(&(hunk->final_commit_id)));
rb_hash_aset(rb_hunk, CSTR2SYM("final_start_line_number"), UINT2NUM(hunk->final_start_line_number));
rb_hash_aset(rb_hunk, CSTR2SYM("final_signature"), hunk->final_signature ? rugged_signature_new(hunk->final_signature, NULL) : Qnil);
rb_hash_aset(rb_hunk, CSTR2SYM("final_signature"), hunk->final_signature ? rugged_signature_new(hunk->final_signature) : Qnil);

rb_hash_aset(rb_hunk, CSTR2SYM("orig_commit_id"), rugged_create_oid(&(hunk->orig_commit_id)));
rb_hash_aset(rb_hunk, CSTR2SYM("orig_path"), hunk->orig_path ? rb_str_new2(hunk->orig_path) : Qnil);
rb_hash_aset(rb_hunk, CSTR2SYM("orig_start_line_number"), UINT2NUM(hunk->orig_start_line_number));
rb_hash_aset(rb_hunk, CSTR2SYM("orig_signature"), hunk->orig_signature ? rugged_signature_new(hunk->orig_signature, NULL) : Qnil);
rb_hash_aset(rb_hunk, CSTR2SYM("orig_signature"), hunk->orig_signature ? rugged_signature_new(hunk->orig_signature) : Qnil);

rb_hash_aset(rb_hunk, CSTR2SYM("boundary"), hunk->boundary ? Qtrue : Qfalse);

Expand Down
4 changes: 2 additions & 2 deletions ext/rugged/rugged_branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static VALUE rb_git_branch_name(VALUE self)

rugged_exception_check(git_branch_name(&branch_name, branch));

return rb_str_new_utf8(branch_name);
return rb_str_new2(branch_name);
}

static VALUE rb_git_branch__remote_name(VALUE rb_repo, const char *canonical_name)
Expand All @@ -78,7 +78,7 @@ static VALUE rb_git_branch__remote_name(VALUE rb_repo, const char *canonical_nam
Data_Get_Struct(rb_repo, git_repository, repo);

if ((error = git_branch_remote_name(&remote_name, repo, canonical_name)) == GIT_OK)
result = rb_enc_str_new(remote_name.ptr, remote_name.size, rb_utf8_encoding());
result = rb_str_new(remote_name.ptr, remote_name.size);

git_buf_free(&remote_name);
rugged_exception_check(error);
Expand Down
2 changes: 1 addition & 1 deletion ext/rugged/rugged_branch_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static VALUE each_branch(int argc, VALUE *argv, VALUE self, int branch_names_onl
if (branch_names_only) {
git_reference *branch;
while (!exception && (error = git_branch_next(&branch, &branch_type, iter)) == GIT_OK) {
rb_protect(rb_yield, rb_str_new_utf8(git_reference_shorthand(branch)), &exception);
rb_protect(rb_yield, rb_str_new2(git_reference_shorthand(branch)), &exception);
}
} else {
git_reference *branch;
Expand Down
25 changes: 7 additions & 18 deletions ext/rugged/rugged_commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ VALUE rb_cRuggedCommit;
static VALUE rb_git_commit_message_GET(VALUE self)
{
git_commit *commit;
rb_encoding *encoding = rb_utf8_encoding();
rb_encoding *encoding = rb_ascii8bit_encoding();
const char *encoding_name;
const char *message;

Expand Down Expand Up @@ -82,9 +82,7 @@ static VALUE rb_git_commit_committer_GET(VALUE self)
git_commit *commit;
Data_Get_Struct(self, git_commit, commit);

return rugged_signature_new(
git_commit_committer(commit),
git_commit_message_encoding(commit));
return rugged_signature_new(git_commit_committer(commit));
}

/*
Expand All @@ -107,9 +105,7 @@ static VALUE rb_git_commit_author_GET(VALUE self)
git_commit *commit;
Data_Get_Struct(self, git_commit, commit);

return rugged_signature_new(
git_commit_author(commit),
git_commit_message_encoding(commit));
return rugged_signature_new(git_commit_author(commit));
Copy link
Member

Choose a reason for hiding this comment

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

This is its own can of worms, but for some use-cases we have assumed that the encoding of the signature is the same as of the commit message. This sometimes even holds true, and this is a place where we kinda have a good idea of what the encoding is likely to be, rather than no idea like in the other cases.

Copy link
Contributor

@tenderlove tenderlove Jul 8, 2016

Choose a reason for hiding this comment

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

I think this is probably an OK assumption to make. I haven't seen any exceptions regarding this in production (only the path and tag stuff)

}

/*
Expand Down Expand Up @@ -577,7 +573,7 @@ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)

if (error) goto cleanup;

rb_email_patch = rb_enc_str_new(email_patch.ptr, email_patch.size, rb_utf8_encoding());
rb_email_patch = rb_str_new(email_patch.ptr, email_patch.size);

cleanup:

Expand All @@ -598,9 +594,6 @@ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field)
{
git_buf header_field = { 0 };
git_commit *commit = NULL;

const char *encoding_name;
rb_encoding *encoding = rb_utf8_encoding();
VALUE rb_result;

int error;
Expand All @@ -617,11 +610,7 @@ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field)
rugged_exception_check(error);
}

encoding_name = git_commit_message_encoding(commit);
if (encoding_name != NULL)
encoding = rb_enc_find(encoding_name);

rb_result = rb_enc_str_new(header_field.ptr, header_field.size, encoding);
rb_result = rb_str_new(header_field.ptr, header_field.size);
git_buf_free(&header_field);
return rb_result;
}
Expand All @@ -640,7 +629,7 @@ static VALUE rb_git_commit_header(VALUE self)
Data_Get_Struct(self, git_commit, commit);

raw_header = git_commit_raw_header(commit);
return rb_str_new_utf8(raw_header);
return rb_str_new2(raw_header);
}

/*
Expand Down Expand Up @@ -753,7 +742,7 @@ static VALUE rb_git_commit_create_to_s(VALUE self, VALUE rb_repo, VALUE rb_data)

rugged_exception_check(error);

ret = rb_str_new_utf8(buf.ptr);
ret = rb_str_new(buf.ptr, buf.size);
git_buf_free(&buf);

return ret;
Expand Down
12 changes: 6 additions & 6 deletions ext/rugged/rugged_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
return Qnil;

rugged_exception_check(error);
rb_result = rb_str_new_utf8(buf.ptr);
rb_result = rb_str_new(buf.ptr, buf.size);
git_buf_free(&buf);

return rb_result;
Expand Down Expand Up @@ -188,15 +188,15 @@ static VALUE rb_git_config_delete(VALUE self, VALUE rb_key)

static int cb_config__each_key(const git_config_entry *entry, void *opaque)
{
rb_funcall((VALUE)opaque, rb_intern("call"), 1, rb_str_new_utf8(entry->name));
rb_funcall((VALUE)opaque, rb_intern("call"), 1, rb_str_new2(entry->name));
return GIT_OK;
}

static int cb_config__each_pair(const git_config_entry *entry, void *opaque)
{
rb_funcall((VALUE)opaque, rb_intern("call"), 2,
rb_str_new_utf8(entry->name),
rb_str_new_utf8(entry->value)
rb_str_new2(entry->name),
rb_str_new2(entry->value)
);

return GIT_OK;
Expand All @@ -205,8 +205,8 @@ static int cb_config__each_pair(const git_config_entry *entry, void *opaque)
static int cb_config__to_hash(const git_config_entry *entry, void *opaque)
{
rb_hash_aset((VALUE)opaque,
rb_str_new_utf8(entry->name),
rb_str_new_utf8(entry->value)
rb_str_new2(entry->name),
rb_str_new2(entry->value)
);

return GIT_OK;
Expand Down
2 changes: 1 addition & 1 deletion ext/rugged/rugged_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ static VALUE rb_git_indexentry_fromC(const git_index_entry *entry)

rb_entry = rb_hash_new();

rb_hash_aset(rb_entry, CSTR2SYM("path"), rb_str_new_utf8(entry->path));
rb_hash_aset(rb_entry, CSTR2SYM("path"), rb_str_new2(entry->path));
rb_hash_aset(rb_entry, CSTR2SYM("oid"), rugged_create_oid(&entry->id));

rb_hash_aset(rb_entry, CSTR2SYM("dev"), INT2FIX(entry->dev));
Expand Down
11 changes: 2 additions & 9 deletions ext/rugged/rugged_note.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ extern VALUE rb_cRuggedObject;

static VALUE rugged_git_note_message(const git_note *note)
{
const char *message;
message = git_note_message(note);

/*
* assume the note message is utf8 compatible, because that's
* the sensible thing to do.
*/
return rb_str_new_utf8(message);
return rb_str_new2(git_note_message(note));
}

static VALUE rugged_git_note_oid(const git_note* note)
Expand Down Expand Up @@ -358,7 +351,7 @@ static VALUE rb_git_note_default_ref_GET(VALUE self)
git_note_default_ref(&ref_name, repo)
);

rb_result = rb_enc_str_new(ref_name.ptr, ref_name.size, rb_utf8_encoding());
rb_result = rb_str_new(ref_name.ptr, ref_name.size);

git_buf_free(&ref_name);

Expand Down
2 changes: 1 addition & 1 deletion ext/rugged/rugged_rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ static VALUE rb_git_rebase_next(VALUE self)
}

if (operation->exec) {
val = rb_str_new_utf8(operation->exec);
val = rb_str_new2(operation->exec);
rb_hash_aset(hash, CSTR2SYM("exec"), val);
}

Expand Down
10 changes: 5 additions & 5 deletions ext/rugged/rugged_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static VALUE rb_git_ref_peel(VALUE self)
} else {
git_oid_tostr(oid, sizeof(oid), git_object_id(object));
git_object_free(object);
return rb_str_new_utf8(oid);
return rb_str_new2(oid);
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ static VALUE rb_git_ref_target_id(VALUE self)
if (git_reference_type(ref) == GIT_REF_OID) {
return rugged_create_oid(git_reference_target(ref));
} else {
return rb_str_new_utf8(git_reference_symbolic_target(ref));
return rb_str_new2(git_reference_symbolic_target(ref));
}
}

Expand Down Expand Up @@ -219,7 +219,7 @@ static VALUE rb_git_ref_name(VALUE self)
{
git_reference *ref;
Data_Get_Struct(self, git_reference, ref);
return rb_str_new_utf8(git_reference_name(ref));
return rb_str_new2(git_reference_name(ref));
}

/*
Expand Down Expand Up @@ -266,11 +266,11 @@ static VALUE reflog_entry_new(const git_reflog_entry *entry)

rb_hash_aset(rb_entry,
CSTR2SYM("committer"),
rugged_signature_new(git_reflog_entry_committer(entry), NULL)
rugged_signature_new(git_reflog_entry_committer(entry))
);

if ((message = git_reflog_entry_message(entry)) != NULL) {
rb_hash_aset(rb_entry, CSTR2SYM("message"), rb_str_new_utf8(message));
rb_hash_aset(rb_entry, CSTR2SYM("message"), rb_str_new2(message));
}

return rb_entry;
Expand Down
2 changes: 1 addition & 1 deletion ext/rugged/rugged_reference_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static VALUE rb_git_reference_collection__each(int argc, VALUE *argv, VALUE self
if (only_names) {
const char *ref_name;
while (!exception && (error = git_reference_next_name(&ref_name, iter)) == GIT_OK) {
rb_protect(rb_yield, rb_str_new_utf8(ref_name), &exception);
rb_protect(rb_yield, rb_str_new2(ref_name), &exception);
}
} else {
git_reference *ref;
Expand Down
12 changes: 6 additions & 6 deletions ext/rugged/rugged_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static int push_update_reference_cb(const char *refname, const char *status, voi
struct rugged_remote_cb_payload *payload = data;

if (status != NULL)
rb_hash_aset(payload->result, rb_str_new_utf8(refname), rb_str_new_utf8(status));
rb_hash_aset(payload->result, rb_str_new2(refname), rb_str_new2(status));

return GIT_OK;
}
Expand All @@ -87,7 +87,7 @@ static int update_tips_cb(const char *refname, const git_oid *src, const git_oid
return 0;

rb_ary_push(args, payload->update_tips);
rb_ary_push(args, rb_str_new_utf8(refname));
rb_ary_push(args, rb_str_new2(refname));
rb_ary_push(args, git_oid_iszero(src) ? Qnil : rugged_create_oid(src));
rb_ary_push(args, git_oid_iszero(dest) ? Qnil : rugged_create_oid(dest));

Expand Down Expand Up @@ -225,7 +225,7 @@ static VALUE rugged_rhead_new(const git_remote_head *head)
rb_hash_aset(rb_head, CSTR2SYM("oid"), rugged_create_oid(&head->oid));
rb_hash_aset(rb_head, CSTR2SYM("loid"),
git_oid_iszero(&head->loid) ? Qnil : rugged_create_oid(&head->loid));
rb_hash_aset(rb_head, CSTR2SYM("name"), rb_str_new_utf8(head->name));
rb_hash_aset(rb_head, CSTR2SYM("name"), rb_str_new2(head->name));

return rb_head;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ static VALUE rb_git_remote_name(VALUE self)

name = git_remote_name(remote);

return name ? rb_str_new_utf8(name) : Qnil;
return name ? rb_str_new2(name) : Qnil;
}

/*
Expand All @@ -343,7 +343,7 @@ static VALUE rb_git_remote_url(VALUE self)
git_remote *remote;
Data_Get_Struct(self, git_remote, remote);

return rb_str_new_utf8(git_remote_url(remote));
return rb_str_new2(git_remote_url(remote));
}

/*
Expand All @@ -363,7 +363,7 @@ static VALUE rb_git_remote_push_url(VALUE self)
Data_Get_Struct(self, git_remote, remote);

push_url = git_remote_pushurl(remote);
return push_url ? rb_str_new_utf8(push_url) : Qnil;
return push_url ? rb_str_new2(push_url) : Qnil;
}

/*
Expand Down
4 changes: 2 additions & 2 deletions ext/rugged/rugged_remote_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static VALUE rb_git_remote_collection__each(VALUE self, int only_names)

if (only_names) {
for (i = 0; !exception && i < remotes.count; ++i) {
rb_protect(rb_yield, rb_str_new_utf8(remotes.strings[i]), &exception);
rb_protect(rb_yield, rb_str_new2(remotes.strings[i]), &exception);
}
} else {
for (i = 0; !exception && !error && i < remotes.count; ++i) {
Expand Down Expand Up @@ -272,7 +272,7 @@ static VALUE rb_git_remote_collection_rename(VALUE self, VALUE rb_name_or_remote
rugged_exception_check(error);

for (i = exception = 0; !exception && i < problems.count; ++i) {
rb_protect(rb_yield, rb_str_new_utf8(problems.strings[i]), &exception);
rb_protect(rb_yield, rb_str_new2(problems.strings[i]), &exception);
}

git_strarray_free(&problems);
Expand Down
Loading