/
rugged_signature.c
96 lines (78 loc) · 2.93 KB
/
rugged_signature.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* The MIT License
*
* Copyright (c) 2013 GitHub, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "rugged.h"
VALUE rugged_signature_new(const git_signature *sig, const char *encoding_name)
{
VALUE rb_sig, rb_time;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *encoding = NULL;
if (encoding_name != NULL)
encoding = rb_enc_find(encoding_name);
#endif
rb_sig = rb_hash_new();
#if RUBY_API_VERSION_MINOR >= 9
/* Allocate the time with a the given timezone */
rb_time = rb_funcall(
rb_time_new(sig->when.time, 0),
rb_intern("getlocal"), 1,
INT2FIX(sig->when.offset * 60)
);
#else
rb_time = rb_time_new(sig->when.time, 0);
rb_hash_aset(rb_sig, CSTR2SYM("time_offset"), INT2FIX(sig->when.offset * 60));
#endif
rb_hash_aset(rb_sig, CSTR2SYM("name"), rugged_str_new2(sig->name, encoding));
rb_hash_aset(rb_sig, CSTR2SYM("email"), rugged_str_new2(sig->email, encoding));
rb_hash_aset(rb_sig, CSTR2SYM("time"), rb_time);
return rb_sig;
}
git_signature *rugged_signature_get(VALUE rb_sig)
{
int error;
VALUE rb_time, rb_unix_t, rb_offset, rb_name, rb_email, rb_time_offset;
git_signature *sig;
Check_Type(rb_sig, T_HASH);
rb_name = rb_hash_aref(rb_sig, CSTR2SYM("name"));
rb_email = rb_hash_aref(rb_sig, CSTR2SYM("email"));
rb_time = rb_hash_aref(rb_sig, CSTR2SYM("time"));
rb_time_offset = rb_hash_aref(rb_sig, CSTR2SYM("time_offset"));
Check_Type(rb_name, T_STRING);
Check_Type(rb_email, T_STRING);
if (!rb_obj_is_kind_of(rb_time, rb_cTime))
rb_raise(rb_eTypeError, "expected Time object");
rb_unix_t = rb_funcall(rb_time, rb_intern("tv_sec"), 0);
if (NIL_P(rb_time_offset)) {
rb_offset = rb_funcall(rb_time, rb_intern("utc_offset"), 0);
} else {
Check_Type(rb_time_offset, T_FIXNUM);
rb_offset = rb_time_offset;
}
error = git_signature_new(&sig,
StringValueCStr(rb_name),
StringValueCStr(rb_email),
NUM2LONG(rb_unix_t),
FIX2INT(rb_offset) / 60);
rugged_exception_check(error);
return sig;
}