Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 234 lines (180 sloc) 5.517 kb
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "commit.h"
28 #include "tag.h"
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
29 #include "signature.h"
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
30 #include "revwalk.h"
44908fe Vicent Marti Change the library include file
vmg authored
31 #include "git2/object.h"
32 #include "git2/repository.h"
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
33 #include "git2/signature.h"
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
34
35 void git_tag__free(git_tag *tag)
36 {
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
37 git_signature_free(tag->tagger);
48c27f8 Vicent Marti Implement reference counting for git_objects
vmg authored
38 git_object_close(tag->target);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
39 free(tag->message);
40 free(tag->tag_name);
41 free(tag);
42 }
43
d45b4a9 Vicent Marti Add support for in-memory objects
vmg authored
44 const git_oid *git_tag_id(git_tag *c)
45 {
46 return git_object_id((git_object *)c);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
47 }
48
f49a2e4 Vicent Marti Give object structures more descriptive names
vmg authored
49 const git_object *git_tag_target(git_tag *t)
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
50 {
5851901 Vicent Marti Fix internal memory management on the library
vmg authored
51 assert(t);
48c27f8 Vicent Marti Implement reference counting for git_objects
vmg authored
52 GIT_OBJECT_INCREF(t->target);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
53 return t->target;
54 }
55
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
56 void git_tag_set_target(git_tag *tag, git_object *target)
57 {
58 assert(tag && target);
59
48c27f8 Vicent Marti Implement reference counting for git_objects
vmg authored
60 git_object_close(tag->target);
61 GIT_OBJECT_INCREF(target);
62
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
63 tag->object.modified = 1;
64 tag->target = target;
65 tag->type = git_object_type(target);
66 }
67
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
68 git_otype git_tag_type(git_tag *t)
69 {
5851901 Vicent Marti Fix internal memory management on the library
vmg authored
70 assert(t);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
71 return t->type;
72 }
73
74 const char *git_tag_name(git_tag *t)
75 {
5851901 Vicent Marti Fix internal memory management on the library
vmg authored
76 assert(t);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
77 return t->tag_name;
78 }
79
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
80 void git_tag_set_name(git_tag *tag, const char *name)
81 {
82 assert(tag && name);
83
84 /* TODO: sanity check? no newlines in message */
85 tag->object.modified = 1;
86
87 if (tag->tag_name)
88 free(tag->tag_name);
89
90 tag->tag_name = git__strdup(name);
91 }
92
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
93 const git_signature *git_tag_tagger(git_tag *t)
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
94 {
95 return t->tagger;
96 }
97
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
98 void git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig)
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
99 {
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
100 assert(tag && tagger_sig);
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
101 tag->object.modified = 1;
102
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
103 git_signature_free(tag->tagger);
104 tag->tagger = git_signature_dup(tagger_sig);
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
105 }
106
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
107 const char *git_tag_message(git_tag *t)
108 {
5851901 Vicent Marti Fix internal memory management on the library
vmg authored
109 assert(t);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
110 return t->message;
111 }
112
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
113 void git_tag_set_message(git_tag *tag, const char *message)
114 {
115 assert(tag && message);
116
117 tag->object.modified = 1;
118
119 if (tag->message)
120 free(tag->message);
121
122 tag->message = git__strdup(message);
123 }
124
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
125 static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
126 {
127 static const char *tag_types[] = {
128 NULL, "commit\n", "tree\n", "blob\n", "tag\n"
129 };
130
131 git_oid target_oid;
132 unsigned int i, text_len;
133 char *search;
1795f87 Vicent Marti Improve error handling
vmg authored
134 int error;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
135
1795f87 Vicent Marti Improve error handling
vmg authored
136 if ((error = git__parse_oid(&target_oid, &buffer, buffer_end, "object ")) < 0)
137 return error;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
138
139 if (buffer + 5 >= buffer_end)
140 return GIT_EOBJCORRUPTED;
141
142 if (memcmp(buffer, "type ", 5) != 0)
143 return GIT_EOBJCORRUPTED;
144 buffer += 5;
145
146 tag->type = GIT_OBJ_BAD;
147
148 for (i = 1; i < ARRAY_SIZE(tag_types); ++i) {
149 size_t type_length = strlen(tag_types[i]);
150
151 if (buffer + type_length >= buffer_end)
152 return GIT_EOBJCORRUPTED;
153
154 if (memcmp(buffer, tag_types[i], type_length) == 0) {
155 tag->type = i;
156 buffer += type_length;
157 break;
158 }
159 }
160
161 if (tag->type == GIT_OBJ_BAD)
162 return GIT_EOBJCORRUPTED;
163
48c27f8 Vicent Marti Implement reference counting for git_objects
vmg authored
164 git_object_close(tag->target);
5de079b Vicent Marti Change the object creation/lookup API
vmg authored
165 error = git_object_lookup(&tag->target, tag->object.repo, &target_oid, tag->type);
1795f87 Vicent Marti Improve error handling
vmg authored
166 if (error < 0)
167 return error;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
168
169 if (buffer + 4 >= buffer_end)
170 return GIT_EOBJCORRUPTED;
171
172 if (memcmp(buffer, "tag ", 4) != 0)
173 return GIT_EOBJCORRUPTED;
174 buffer += 4;
175
176 search = memchr(buffer, '\n', buffer_end - buffer);
177 if (search == NULL)
178 return GIT_EOBJCORRUPTED;
179
180 text_len = search - buffer;
181
182 if (tag->tag_name != NULL)
183 free(tag->tag_name);
184
185 tag->tag_name = git__malloc(text_len + 1);
186 memcpy(tag->tag_name, buffer, text_len);
187 tag->tag_name[text_len] = '\0';
188
189 buffer = search + 1;
190
191 if (tag->tagger != NULL)
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
192 git_signature_free(tag->tagger);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
193
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
194 tag->tagger = git__malloc(sizeof(git_signature));
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
195
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
196 if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0)
1795f87 Vicent Marti Improve error handling
vmg authored
197 return error;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
198
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
199 text_len = buffer_end - ++buffer;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
200
201 if (tag->message != NULL)
202 free(tag->message);
203
204 tag->message = git__malloc(text_len + 1);
205 memcpy(tag->message, buffer, text_len);
206 tag->message[text_len] = '\0';
207
6f02c3b nulltoken Small source code readability improvements.
nulltoken authored
208 return GIT_SUCCESS;
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
209 }
210
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
211 int git_tag__writeback(git_tag *tag, git_odb_source *src)
212 {
213 if (tag->target == NULL || tag->tag_name == NULL || tag->tagger == NULL)
1795f87 Vicent Marti Improve error handling
vmg authored
214 return GIT_EMISSINGOBJDATA;
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
215
216 git__write_oid(src, "object", git_object_id(tag->target));
d12299f Vicent Marti Change include structure for the project
vmg authored
217 git__source_printf(src, "type %s\n", git_object_type2string(tag->type));
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
218 git__source_printf(src, "tag %s\n", tag->tag_name);
638c2ca Vicent Marti Rename 'git_person' to 'git_signature'
vmg authored
219 git_signature__write(src, "tagger", tag->tagger);
ec25391 Vicent Marti Add write-back support for Tag files
vmg authored
220
221 if (tag->message != NULL)
222 git__source_printf(src, "\n%s", tag->message);
223
224 return GIT_SUCCESS;
225 }
226
227
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
228 int git_tag__parse(git_tag *tag)
229 {
5851901 Vicent Marti Fix internal memory management on the library
vmg authored
230 assert(tag && tag->object.source.open);
6b1eab3 Vicent Marti Fix MSVC warnings and errors
vmg authored
231 return parse_tag_buffer(tag, tag->object.source.raw.data, (char *)tag->object.source.raw.data + tag->object.source.raw.len);
f875804 Vicent Marti Add loading and parsing of tag objects
vmg authored
232 }
233
Something went wrong with that request. Please try again.