Class: Rugged::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/rugged/object.rb,
ext/rugged/rugged_object.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(repo, oid) ⇒ Object .lookup(repo, oid) ⇒ Object

Find and return the git object inside repo with the given oid.

oid can either have be the complete, 40 character string or any unique prefix.


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/rugged/rugged_object.c', line 217

VALUE rb_git_object_lookup(VALUE klass, VALUE rb_repo, VALUE rb_hex)
{
  git_object *object;
  git_otype type;
  git_oid oid;
  int error;
  int oid_length;

  git_repository *repo;

  type = class2otype(klass);

  if (type == GIT_OBJ_BAD)
    type = GIT_OBJ_ANY;

  Check_Type(rb_hex, T_STRING);
  oid_length = (int)RSTRING_LEN(rb_hex);

  rugged_check_repo(rb_repo);

  if (oid_length > GIT_OID_HEXSZ)
    rb_raise(rb_eTypeError, "The given OID is too long");

  Data_Get_Struct(rb_repo, git_repository, repo);

  error = git_oid_fromstrn(&oid, RSTRING_PTR(rb_hex), oid_length);
  rugged_exception_check(error);

  if (oid_length < GIT_OID_HEXSZ)
    error = git_object_lookup_prefix(&object, repo, &oid, oid_length, type);
  else
    error = git_object_lookup(&object, repo, &oid, type);

  rugged_exception_check(error);

  return rugged_object_new(rb_repo, object);
}

.new(repo, oid) ⇒ Object .lookup(repo, oid) ⇒ Object

Find and return the git object inside repo with the given oid.

oid can either have be the complete, 40 character string or any unique prefix.


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/rugged/rugged_object.c', line 217

VALUE rb_git_object_lookup(VALUE klass, VALUE rb_repo, VALUE rb_hex)
{
  git_object *object;
  git_otype type;
  git_oid oid;
  int error;
  int oid_length;

  git_repository *repo;

  type = class2otype(klass);

  if (type == GIT_OBJ_BAD)
    type = GIT_OBJ_ANY;

  Check_Type(rb_hex, T_STRING);
  oid_length = (int)RSTRING_LEN(rb_hex);

  rugged_check_repo(rb_repo);

  if (oid_length > GIT_OID_HEXSZ)
    rb_raise(rb_eTypeError, "The given OID is too long");

  Data_Get_Struct(rb_repo, git_repository, repo);

  error = git_oid_fromstrn(&oid, RSTRING_PTR(rb_hex), oid_length);
  rugged_exception_check(error);

  if (oid_length < GIT_OID_HEXSZ)
    error = git_object_lookup_prefix(&object, repo, &oid, oid_length, type);
  else
    error = git_object_lookup(&object, repo, &oid, type);

  rugged_exception_check(error);

  return rugged_object_new(rb_repo, object);
}

.rev_parse(repo, str) ⇒ Object

Find and return a single object inside repo as specified by the git revision string str.

See git-scm.com/docs/git-rev-parse.html#_specifying_revisions or man gitrevisions for information on the accepted syntax.

Raises a Rugged::InvalidError if str does not contain a valid revision string.


293
294
295
296
# File 'ext/rugged/rugged_object.c', line 293

VALUE rb_git_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec)
{
  return rugged_object_rev_parse(rb_repo, rb_spec, 1);
}

.rev_parse_oid(repo, str) ⇒ Object

Find and return the id of the object inside repo as specified by the git revision string str.

See git-scm.com/docs/git-rev-parse.html#_specifying_revisions or man gitrevisions for information on the accepted syntax.

Raises a Rugged::InvalidError if str does not contain a valid revision string.


309
310
311
312
# File 'ext/rugged/rugged_object.c', line 309

VALUE rb_git_object_rev_parse_oid(VALUE klass, VALUE rb_repo, VALUE rb_spec)
{
  return rugged_object_rev_parse(rb_repo, rb_spec, 0);
}

Instance Method Details

#<=>(other) ⇒ Object


3
4
5
# File 'lib/rugged/object.rb', line 3

def <=>(other)
  self.oid <=> other.oid
end

#==(other) ⇒ Object

Returns true only if object and other are both instances or subclasses of Rugged::Object and have the same object id, false otherwise.


320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/rugged/rugged_object.c', line 320

static VALUE rb_git_object_equal(VALUE self, VALUE other)
{
  git_object *a, *b;

  if (!rb_obj_is_kind_of(other, rb_cRuggedObject))
    return Qfalse;

  Data_Get_Struct(self, git_object, a);
  Data_Get_Struct(other, git_object, b);

  return git_oid_cmp(git_object_id(a), git_object_id(b)) == 0 ? Qtrue : Qfalse;
}

#create_note(data = {}) ⇒ Object

Write a new note to object, with the given data arguments, passed as a Hash:

  • :message: the content of the note to add to the object

  • :committer: (optional) a hash with the signature for the committer defaults to the signature from the configuration

  • :author: (optional) a hash with the signature for the author defaults to the signature from the configuration

  • :ref: (optional): canonical name of the reference to use, defaults to “refs/notes/commits”

  • :force: (optional): overwrite existing note (disabled by default)

When the note is successfully written to disk, its oid will be returned as a hex String.

author = {:email=>"[email protected]", :time=>Time.now, :name=>"Vicent Mart\303\255"}

obj.create_note(
  :author    => author,
  :committer => author,
  :message   => "Hello world\n\n",
  :ref       => 'refs/notes/builds'
)

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/rugged/rugged_note.c', line 127

static VALUE rb_git_note_create(VALUE self, VALUE rb_data)
{
  VALUE rb_ref, rb_message, rb_force;
  git_repository *repo = NULL;
  const char *notes_ref = NULL;

  VALUE owner;

  git_signature *author, *committer;
  git_oid note_oid;

  git_object *target = NULL;
  int error = 0;
  int force = 0;

  Check_Type(rb_data, T_HASH);

  Data_Get_Struct(self, git_object, target);

  owner = rugged_owner(self);
  Data_Get_Struct(owner, git_repository, repo);

  rb_ref = rb_hash_aref(rb_data, CSTR2SYM("ref"));

  rb_force = rb_hash_aref(rb_data, CSTR2SYM("force"));
  if (!NIL_P(rb_force))
    force = rugged_parse_bool(rb_force);

  if (!NIL_P(rb_ref)) {
    Check_Type(rb_ref, T_STRING);
    notes_ref = StringValueCStr(rb_ref);
  }

  rb_message = rb_hash_aref(rb_data, CSTR2SYM("message"));
  Check_Type(rb_message, T_STRING);

  committer = rugged_signature_get(
    rb_hash_aref(rb_data, CSTR2SYM("committer")), repo
  );

  author = rugged_signature_get(
    rb_hash_aref(rb_data, CSTR2SYM("author")), repo
  );

  error = git_note_create(
      &note_oid,
      repo,
      author,
      committer,
      notes_ref,
      git_object_id(target),
      StringValueCStr(rb_message),
      force);


  git_signature_free(author);
  git_signature_free(committer);

  rugged_exception_check(error);

  return rugged_create_oid(&note_oid);
}

#oidObject

Return the Object ID (a 40 character SHA1 hash) for object.


338
339
340
341
342
343
# File 'ext/rugged/rugged_object.c', line 338

static VALUE rb_git_object_oid_GET(VALUE self)
{
  git_object *object;
  Data_Get_Struct(self, git_object, object);
  return rugged_create_oid(git_object_id(object));
}

#read_rawObject

Returns the git object as a Rugged::OdbObject instance.


363
364
365
366
367
368
369
# File 'ext/rugged/rugged_object.c', line 363

static VALUE rb_git_object_read_raw(VALUE self)
{
  git_object *object;
  Data_Get_Struct(self, git_object, object);

  return rugged_raw_read(git_object_owner(object), git_object_id(object));
}

#remove_note(data = {}) ⇒ Boolean

Removes a note from object, with the given data arguments, passed as a Hash:

  • :committer (optional): a hash with the signature for the committer, defaults to the signature from the configuration

  • :author (optional): a hash with the signature for the author, defaults to the signature from the configuration

  • :ref: (optional): canonical name of the reference to use, defaults to “refs/notes/commits”

When the note is successfully removed true will be returned as a Boolean.

author = {:email=>"[email protected]", :time=>Time.now, :name=>"Vicent Mart\303\255"}

obj.remove_note(
  :author    => author,
  :committer => author,
  :ref       => 'refs/notes/builds'
)

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'ext/rugged/rugged_note.c', line 214

static VALUE rb_git_note_remove(int argc, VALUE *argv, VALUE self)
{
  int error = 0;
  const char *notes_ref = NULL;
  git_repository *repo = NULL;
  git_signature *author, *committer;
  git_object *target = NULL;
  VALUE rb_data;
  VALUE rb_ref = Qnil;
  VALUE rb_author = Qnil;
  VALUE rb_committer = Qnil;
  VALUE owner;

  Data_Get_Struct(self, git_object, target);

  owner = rugged_owner(self);
  Data_Get_Struct(owner, git_repository, repo);

  rb_scan_args(argc, argv, "01", &rb_data);

  if (!NIL_P(rb_data)) {
    Check_Type(rb_data, T_HASH);

    rb_ref = rb_hash_aref(rb_data, CSTR2SYM("ref"));
    if (!NIL_P(rb_ref)) {
      Check_Type(rb_ref, T_STRING);
      notes_ref = StringValueCStr(rb_ref);
    }

    rb_committer = rb_hash_aref(rb_data, CSTR2SYM("committer"));
    rb_author = rb_hash_aref(rb_data, CSTR2SYM("author"));
  }

  committer = rugged_signature_get(rb_committer, repo);
  author = rugged_signature_get(rb_author, repo);

  error = git_note_remove(
      repo,
      notes_ref,
      author,
      committer,
      git_object_id(target));

  git_signature_free(author);
  git_signature_free(committer);

  if (error == GIT_ENOTFOUND)
    return Qfalse;

  rugged_exception_check(error);

  return Qtrue;
}

#typeObject

Returns the object's type. Can be one of :commit, :tag, :tree or :blob.


350
351
352
353
354
355
356
# File 'ext/rugged/rugged_object.c', line 350

static VALUE rb_git_object_type_GET(VALUE self)
{
  git_object *object;
  Data_Get_Struct(self, git_object, object);

  return rugged_otype_new(git_object_type(object));
}