Class: Rugged::Repository

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

Overview

Repository is an interface into a Git repository on-disk. It’s the primary interface between your app and the main Git objects Rugged makes available to you.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bare(path[, alternates]) ⇒ Object

Open a bare Git repository at path and return a Repository object representing it.

This is faster than Rugged::Repository.new, as it won’t attempt to perform any .git directory discovery, won’t try to load the config options to determine whether the repository is bare and won’t try to load the workdir.

Optionally, you can pass a list of alternate object folders.

Rugged::Repository.bare(path, ['./other/repo/.git/objects'])


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/rugged/rugged_repo.c', line 193

static VALUE rb_git_repo_open_bare(int argc, VALUE *argv, VALUE klass)
{
	git_repository *repo;
	int error = 0;
	VALUE rb_path, rb_alternates;

	rb_scan_args(argc, argv, "11", &rb_path, &rb_alternates);
	Check_Type(rb_path, T_STRING);

	error = git_repository_open_bare(&repo, StringValueCStr(rb_path));
	rugged_exception_check(error);

	load_alternates(repo, rb_alternates);

	return rugged_repo_new(klass, repo);
}

.clone_at(url, local_path[, options]) ⇒ Object

Clone a repository from url to local_path.

The following options can be passed in the options Hash:

:bare

If true, the clone will be created as a bare repository. Defaults to false.

:progress

A callback that will be executed to report clone progress information. It will be passed the amount of total_objects, indexed_objects, received_objects and received_bytes.

Example:

progress = lambda { |total_objects, indexed_objects, received_objects, received_bytes|
  # ...
}
Repository.clone_at("https://github.com/libgit2/rugged.git", "./some/dir", :progress => progress)


364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'ext/rugged/rugged_repo.c', line 364

static VALUE rb_git_repo_clone_at(int argc, VALUE *argv, VALUE klass)
{
	VALUE url, local_path, rb_options_hash;
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
	struct clone_fetch_callback_payload fetch_payload;
	git_repository *repo;
	int error;

	rb_scan_args(argc, argv, "21", &url, &local_path, &rb_options_hash);
	Check_Type(url, T_STRING);
	Check_Type(local_path, T_STRING);

	fetch_payload.proc = Qnil;
	fetch_payload.exception = Qnil;
	parse_clone_options(&options, rb_options_hash, &fetch_payload);

	error = git_clone(&repo, StringValueCStr(url), StringValueCStr(local_path), &options);
	if (RTEST(fetch_payload.exception))
		rb_exc_raise(fetch_payload.exception);
	rugged_exception_check(error);

	return rugged_repo_new(klass, repo);
}

.discover(path = nil, across_fs = true) ⇒ Object

Traverse path upwards until a Git working directory with a .git folder has been found, open it and return it as a Repository object.

If path is nil, the current working directory will be used as a starting point.

If across_fs is true, the traversal won’t stop when reaching a different device than the one that contained path (only applies to UNIX-based OSses).



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'ext/rugged/rugged_repo.c', line 905

static VALUE rb_git_repo_discover(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_path, rb_across_fs;
	char repository_path[GIT_PATH_MAX];
	int error, across_fs = 0;

	rb_scan_args(argc, argv, "02", &rb_path, &rb_across_fs);

	if (NIL_P(rb_path)) {
		VALUE rb_dir = rb_const_get(rb_cObject, rb_intern("Dir"));
		rb_path = rb_funcall(rb_dir, rb_intern("pwd"), 0);
	}

	if (!NIL_P(rb_across_fs)) {
		across_fs = rugged_parse_bool(rb_across_fs);
	}

	Check_Type(rb_path, T_STRING);

	error = git_repository_discover(
		repository_path, GIT_PATH_MAX,
		StringValueCStr(rb_path),
		across_fs,
		NULL
	);

	rugged_exception_check(error);
	return rb_str_new_utf8(repository_path);
}

.hash(buffer, type) ⇒ Object

Hash the contents of buffer as raw bytes (ignoring any encoding information) and adding the relevant header corresponding to type, and return a hex string representing the result from the hash.

Repository.hash('hello world', :commit) #=> "de5ba987198bcf2518885f0fc1350e5172cded78"

Repository.hash('hello_world', :tag) #=> "9d09060c850defbc7711d08b57def0d14e742f4e"


631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'ext/rugged/rugged_repo.c', line 631

static VALUE rb_git_repo_hash(VALUE self, VALUE rb_buffer, VALUE rb_type)
{
	int error;
	git_oid oid;

	Check_Type(rb_buffer, T_STRING);

	error = git_odb_hash(&oid,
		RSTRING_PTR(rb_buffer),
		RSTRING_LEN(rb_buffer),
		rugged_otype_get(rb_type)
	);
	rugged_exception_check(error);

	return rugged_create_oid(&oid);
}

.hash_file(path, type) ⇒ Object

Hash the contents of the file pointed at by path, assuming that it’d be stored in the ODB with the given type, and return a hex string representing the SHA1 OID resulting from the hash.

Repository.hash_file('foo.txt', :commit) #=> "de5ba987198bcf2518885f0fc1350e5172cded78"

Repository.hash_file('foo.txt', :tag) #=> "9d09060c850defbc7711d08b57def0d14e742f4e"


660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'ext/rugged/rugged_repo.c', line 660

static VALUE rb_git_repo_hashfile(VALUE self, VALUE rb_path, VALUE rb_type)
{
	int error;
	git_oid oid;

	Check_Type(rb_path, T_STRING);

	error = git_odb_hashfile(&oid,
		StringValueCStr(rb_path),
		rugged_otype_get(rb_type)
	);
	rugged_exception_check(error);

	return rugged_create_oid(&oid);
}

.init_at(path, is_bare = false) ⇒ Object

Initialize a Git repository in path. This implies creating all the necessary files on the FS, or re-initializing an already existing repository if the files have already been created.

The is_bare (optional, defaults to false) attribute specifies whether the Repository should be created on disk as bare or not. Bare repositories have no working directory and are created in the root of path. Non-bare repositories are created in a .git folder and use path as working directory.

Rugged::Repository.init_at('~/repository', :bare) #=> #<Rugged::Repository:0x108849488>


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/rugged/rugged_repo.c', line 270

static VALUE rb_git_repo_init_at(int argc, VALUE *argv, VALUE klass)
{
	git_repository *repo;
	VALUE rb_path, rb_is_bare;
	int error, is_bare = 0;

	rb_scan_args(argc, argv, "11", &rb_path, &rb_is_bare);
	Check_Type(rb_path, T_STRING);

	if (!NIL_P(rb_is_bare))
		is_bare = rb_is_bare ? 1 : 0;

	error = git_repository_init(&repo, StringValueCStr(rb_path), is_bare);
	rugged_exception_check(error);

	return rugged_repo_new(klass, repo);
}

.new(path, options = {}) ⇒ Object

Open a Git repository in the given path and return a Repository object representing it. An exception will be thrown if path doesn’t point to a valid repository. If you need to create a repository from scratch, use Rugged::Repository.init instead.

The path must point to either the actual folder (.git) of a Git repository, or to the directorly that contains the .git folder.

See also Rugged::Repository.discover and Rugged::Repository.bare.

The following options can be passed in the options Hash:

:alternates

A list of alternate object folders.

Examples:

Rugged::Repository.new('~/test/.git') #=> #<Rugged::Repository:0x108849488>
Rugged::Repository.new(path, :alternates => ['./other/repo/.git/objects'])


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/rugged/rugged_repo.c', line 234

static VALUE rb_git_repo_new(int argc, VALUE *argv, VALUE klass)
{
	git_repository *repo;
	int error = 0;
	VALUE rb_path, rb_options;

	rb_scan_args(argc, argv, "10:", &rb_path, &rb_options);
	Check_Type(rb_path, T_STRING);

	error = git_repository_open(&repo, StringValueCStr(rb_path));
	rugged_exception_check(error);

	if (!NIL_P(rb_options)) {
		/* Check for `:alternates` */
		load_alternates(repo, rb_hash_aref(rb_options, CSTR2SYM("alternates")));
	}

	return rugged_repo_new(klass, repo);
}

Instance Method Details

#ahead_behind(local, upstream) ⇒ Array

Returns a 2 element Array containing the number of commits that the upstream object is ahead and behind the local object.

local and upstream can either be strings containing SHA1 OIDs or Rugged::Object instances.

Returns:

  • (Array)


1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
# File 'ext/rugged/rugged_repo.c', line 1363

static VALUE rb_git_repo_ahead_behind(VALUE self, VALUE rb_local, VALUE rb_upstream) {
	git_repository *repo;
	int error;
	git_oid local, upstream;
	size_t ahead, behind;
	VALUE rb_result;

	Data_Get_Struct(self, git_repository, repo);

	error = rugged_oid_get(&local, repo, rb_local);
	rugged_exception_check(error);

	error = rugged_oid_get(&upstream, repo, rb_upstream);
	rugged_exception_check(error);

	error = git_graph_ahead_behind(&ahead, &behind, repo, &local, &upstream);
	rugged_exception_check(error);

	rb_result = rb_ary_new2(2);
	rb_ary_push(rb_result, INT2FIX((int) ahead));
	rb_ary_push(rb_result, INT2FIX((int) behind));
	return rb_result;
}

#bare?Boolean

Return whether a repository is bare or not. A bare repository has no working directory.

Returns:

  • (Boolean)


735
736
737
738
# File 'ext/rugged/rugged_repo.c', line 735

static VALUE rb_git_repo_is_bare(VALUE self)
{
	RB_GIT_REPO_GETTER(is_bare);
}

#blob_at(revision, path) ⇒ Object

Get the blob at a path for a specific revision.

revision - The String SHA1. path - The String file path.

Returns a String.



152
153
154
155
156
157
158
159
160
161
# File 'lib/rugged/repository.rb', line 152

def blob_at(revision, path)
  tree = Rugged::Commit.lookup(self, revision).tree
  begin
    blob_data = tree.path(path)
  rescue Rugged::TreeError
    return nil
  end
  blob = Rugged::Blob.lookup(self, blob_data[:oid])
  (blob.type == :blob) ? blob : nil
end

#branchesObject

All the branches in the repository

Returns an Enumerable::Enumerator containing Rugged::Branch objects



124
125
126
# File 'lib/rugged/repository.rb', line 124

def branches
  Rugged::Branch.each(self)
end

#closenil

Frees all the resources used by this repository immediately. The repository can still be used after this call. Resources will be opened as necessary.

It is not required to call this method explicitly. Repositories are closed automatically before garbage collection

Returns:

  • (nil)


1301
1302
1303
1304
1305
1306
1307
1308
1309
# File 'ext/rugged/rugged_repo.c', line 1301

static VALUE rb_git_repo_close(VALUE self)
{
	git_repository *repo;
	Data_Get_Struct(self, git_repository, repo);

	git_repository__cleanup(repo);

	return Qnil;
}

#configObject

Return a Rugged::Config object representing this repository’s config.



474
475
476
477
# File 'ext/rugged/rugged_repo.c', line 474

static VALUE rb_git_repo_get_config(VALUE self)
{
	RB_GIT_REPO_OWNED_GET(rb_cRuggedConfig, config);
}

#config=(cfg) ⇒ Object

Set the configuration file for this Repository. cfg must be a instance of Rugged::Config. This config file will be used internally by all operations that need to lookup configuration settings on repo.

Note that it’s not necessary to set the config for any repository; by default repositories are loaded with their relevant config files on the filesystem, and the corresponding global and system files if they can be found.



463
464
465
466
# File 'ext/rugged/rugged_repo.c', line 463

static VALUE rb_git_repo_set_config(VALUE self, VALUE rb_data)
{
	RB_GIT_REPO_OWNED_SET(rb_cRuggedConfig, config);
}

#create_branch(name, sha_or_ref = "HEAD") ⇒ Object

Create a new branch in the repository

name - The name of the branch (without a full reference path) sha_or_ref - The target of the branch; either a String representing an OID or a reference name, or a Rugged::Object instance.

Returns a Rugged::Branch object



135
136
137
138
139
140
141
142
143
144
# File 'lib/rugged/repository.rb', line 135

def create_branch(name, sha_or_ref = "HEAD")
  case sha_or_ref
  when Rugged::Object
    target = sha_or_ref.oid
  else
    target = rev_parse_oid(sha_or_ref)
  end

  Branch.create(self, name, target)
end

#notes_default_refString

Get the default notes reference for a repository:

Returns a new String object.

repo.default_notes_ref #=> "refs/notes/commits"

Returns:

  • (String)


344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'ext/rugged/rugged_note.c', line 344

static VALUE rb_git_note_default_ref_GET(VALUE self)
{
	git_repository *repo = NULL;
	const char * ref_name;

	Data_Get_Struct(self, git_repository, repo);

	rugged_exception_check(
		git_note_default_ref(&ref_name, repo)
	);

	return rb_str_new_utf8(ref_name);
}

#diff(left, right, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rugged/repository.rb', line 20

def diff(left, right, opts = {})
  left = rev_parse(left) if left.kind_of?(String)
  right = rev_parse(right) if right.kind_of?(String)

  if !left.is_a?(Rugged::Tree) && !left.is_a?(Rugged::Commit) && !left.nil?
    raise TypeError, "Expected a Rugged::Tree or Rugged::Commit instance"
  end

  if !right.is_a?(Rugged::Tree) && !right.is_a?(Rugged::Commit) && !right.nil?
    raise TypeError, "Expected a Rugged::Tree or Rugged::Commit instance"
  end

  if left
    left.diff(right, opts)
  elsif right
    right.diff(left, opts.merge(:reverse => !opts[:reverse]))
  end
end

#diff_workdir(left, opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rugged/repository.rb', line 39

def diff_workdir(left, opts = {})
  left = rev_parse(left) if left.kind_of?(String)

  if !left.is_a?(Rugged::Tree) && !left.is_a?(Rugged::Commit)
    raise TypeError, "Expected a Rugged::Tree or Rugged::Commit instance"
  end

  left.diff_workdir(opts)
end

#each_id {|id| ... } ⇒ Object #each_idIterator

Call the given block once with every object ID found in repo and all its alternates. Object IDs are passed as 40-character strings.

Overloads:

  • #each_id {|id| ... } ⇒ Object

    Yields:

    • (id)
  • #each_idIterator

    Returns:

    • (Iterator)


1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'ext/rugged/rugged_repo.c', line 1053

static VALUE rb_git_repo_each_id(VALUE self)
{
	git_repository *repo;
	git_odb *odb;
	int error, exception = 0;

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_id"));

	Data_Get_Struct(self, git_repository, repo);

	error = git_repository_odb(&odb, repo);
	rugged_exception_check(error);

	error = git_odb_foreach(odb, &rugged__each_id_cb, &exception);
	git_odb_free(odb);

	if (exception)
		rb_jump_tag(exception);
	rugged_exception_check(error);

	return Qnil;
}

#each_note(notes_ref = "refs/notes/commits") {|note_blob, annotated_object| ... } ⇒ Object #each_note(notes_ref = "refs/notes/commits") ⇒ Object

Call the given block once for each note_blob/annotated_object pair in repository

  • notes_ref: (optional): cannonical name of the reference to use defaults to “refs/notes/commits”

If no block is given, an Enumerator is returned.

@repo.each_note do |note_blob, annotated_object|
  puts "#{note_blob.oid} => #{annotated_object.oid}"
end

Overloads:

  • #each_note(notes_ref = "refs/notes/commits") {|note_blob, annotated_object| ... } ⇒ Object

    Yields:

    • (note_blob, annotated_object)


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rugged/rugged_note.c', line 304

static VALUE rb_git_note_each(int argc, VALUE *argv, VALUE self)
{
	git_repository *repo;
	const char *notes_ref = NULL;
	int error;
	struct rugged_cb_payload payload = { self, 0 };
	VALUE rb_notes_ref;

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

	if (!rb_block_given_p()) {
		return rb_funcall(self, rb_intern("to_enum"), 3, CSTR2SYM("each_note"), self, rb_notes_ref);
	}

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

	Data_Get_Struct(self, git_repository, repo);

	error = git_note_foreach(repo, notes_ref, &cb_note__each, &payload);

	if (payload.exception)
		rb_jump_tag(payload.exception);
	rugged_exception_check(error);

	return Qnil;
}

#empty?Boolean

Return whether a repository is empty or not. An empty repository has just been initialized and has no commits yet.

Returns:

  • (Boolean)


748
749
750
751
# File 'ext/rugged/rugged_repo.c', line 748

static VALUE rb_git_repo_is_empty(VALUE self)
{
	RB_GIT_REPO_GETTER(is_empty);
}

#include?(oid) ⇒ Boolean #exists?(oid) ⇒ Boolean

Return whether an object with the given SHA1 OID (represented as a 40-character string) exists in the repository.

repo.include?("d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f") #=> true

Overloads:

  • #include?(oid) ⇒ Boolean

    Returns:

    • (Boolean)
  • #exists?(oid) ⇒ Boolean

    Returns:

    • (Boolean)


530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'ext/rugged/rugged_repo.c', line 530

static VALUE rb_git_repo_exists(VALUE self, VALUE hex)
{
	git_repository *repo;
	git_odb *odb;
	git_oid oid;
	int error;
	VALUE rb_result;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(hex, T_STRING);

	error = git_oid_fromstr(&oid, StringValueCStr(hex));
	rugged_exception_check(error);

	error = git_repository_odb(&odb, repo);
	rugged_exception_check(error);

	rb_result = git_odb_exists(odb, &oid) ? Qtrue : Qfalse;
	git_odb_free(odb);

	return rb_result;
}

#headObject

Retrieve and resolve the reference pointed at by the repository’s HEAD.

Returns nil if HEAD is missing.



803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'ext/rugged/rugged_repo.c', line 803

static VALUE rb_git_repo_get_head(VALUE self)
{
	git_repository *repo;
	git_reference *head;
	int error;

	Data_Get_Struct(self, git_repository, repo);

	error = git_repository_head(&head, repo);
	if (error == GIT_ENOTFOUND)
		return Qnil;
	else
		rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, self, head);
}

#head=(str) ⇒ Object

Make the repository’s HEAD point to the specified reference.



781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'ext/rugged/rugged_repo.c', line 781

static VALUE rb_git_repo_set_head(VALUE self, VALUE rb_head)
{
	git_repository *repo;
	int error;

	Data_Get_Struct(self, git_repository, repo);

	Check_Type(rb_head, T_STRING);
	error = git_repository_set_head(repo, StringValueCStr(rb_head));
	rugged_exception_check(error);

	return Qnil;
}

#head_detached?Boolean

Return whether the HEAD of a repository is detached or not.

Returns:

  • (Boolean)


759
760
761
762
# File 'ext/rugged/rugged_repo.c', line 759

static VALUE rb_git_repo_head_detached(VALUE self)
{
	RB_GIT_REPO_GETTER(head_detached);
}

#head_orphan?Boolean

Return whether the HEAD of a repository is orphaned or not.

Returns:

  • (Boolean)


770
771
772
773
# File 'ext/rugged/rugged_repo.c', line 770

static VALUE rb_git_repo_head_orphan(VALUE self)
{
	RB_GIT_REPO_GETTER(head_orphan);
}

#include?(oid) ⇒ Boolean #exists?(oid) ⇒ Boolean

Return whether an object with the given SHA1 OID (represented as a 40-character string) exists in the repository.

repo.include?("d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f") #=> true

Overloads:

  • #include?(oid) ⇒ Boolean

    Returns:

    • (Boolean)
  • #exists?(oid) ⇒ Boolean

    Returns:

    • (Boolean)


530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'ext/rugged/rugged_repo.c', line 530

static VALUE rb_git_repo_exists(VALUE self, VALUE hex)
{
	git_repository *repo;
	git_odb *odb;
	git_oid oid;
	int error;
	VALUE rb_result;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(hex, T_STRING);

	error = git_oid_fromstr(&oid, StringValueCStr(hex));
	rugged_exception_check(error);

	error = git_repository_odb(&odb, repo);
	rugged_exception_check(error);

	rb_result = git_odb_exists(odb, &oid) ? Qtrue : Qfalse;
	git_odb_free(odb);

	return rb_result;
}

#indexObject

Return the default index for this repository.



445
446
447
448
# File 'ext/rugged/rugged_repo.c', line 445

static VALUE rb_git_repo_get_index(VALUE self)
{
	RB_GIT_REPO_OWNED_GET(rb_cRuggedIndex, index);
}

#index=(idx) ⇒ Object

Set the index for this Repository. idx must be a instance of Rugged::Index. This index will be used internally by all operations that use the Git index on repo.

Note that it’s not necessary to set the index for any repository; by default repositories are loaded with the index file that can be located on the .git folder in the filesystem.



434
435
436
437
# File 'ext/rugged/rugged_repo.c', line 434

static VALUE rb_git_repo_set_index(VALUE self, VALUE rb_data)
{
	RB_GIT_REPO_OWNED_SET(rb_cRuggedIndex, index);
}

#inspectObject

Pretty formatting of a Repository.

Returns a very pretty String.



9
10
11
# File 'lib/rugged/repository.rb', line 9

def inspect
  "#<Rugged::Repository:#{object_id} {path: #{path.inspect}}>"
end

#last_commitObject

Get the most recent commit from this repo.

Returns a Rugged::Commit object.



16
17
18
# File 'lib/rugged/repository.rb', line 16

def last_commit
  self.lookup self.head.target
end

#lookup(oid) ⇒ Object

Look up a SHA1.

Returns one of the four classes that inherit from Rugged::Object.



67
68
69
# File 'lib/rugged/repository.rb', line 67

def lookup(oid)
  Rugged::Object.lookup(self, oid)
end

#merge_base(oid1, oid2, ...) ⇒ Object #merge_base(ref1, ref2, ...) ⇒ Object #merge_base(commit1, commit2, ...) ⇒ Object

Find a merge base, given two or more commits or oids. Returns nil if a merge base is not found.



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/rugged/rugged_repo.c', line 488

static VALUE rb_git_repo_merge_base(VALUE self, VALUE rb_args)
{
	int error = GIT_OK, i;
	git_repository *repo;
	git_oid base, *input_array = xmalloc(sizeof(git_oid) * RARRAY_LEN(rb_args));
	int len = (int)RARRAY_LEN(rb_args);

	if (len < 2)
		rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", len);

	Data_Get_Struct(self, git_repository, repo);

	for (i = 0; !error && i < len; ++i) {
		error = rugged_oid_get(&input_array[i], repo, rb_ary_entry(rb_args, i));
	}

	if (error) {
		xfree(input_array);
		rugged_exception_check(error);
	}

	error = git_merge_base_many(&base, repo, input_array, len);
	xfree(input_array);

	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);

	return rugged_create_oid(&base);
}

#namespaceString

Returns the active namespace for the repository.

Returns:

  • (String)


1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
# File 'ext/rugged/rugged_repo.c', line 1342

static VALUE rb_git_repo_get_namespace(VALUE self)
{
	git_repository *repo;
	const char *namespace;

	Data_Get_Struct(self, git_repository, repo);

	namespace = git_repository_get_namespace(repo);
	return namespace ? rb_str_new_utf8(namespace) : Qnil;
}

#namespace=(new_namespace) ⇒ Object

Sets the active namespace for the repository. If set to nil, no namespace will be active.



1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'ext/rugged/rugged_repo.c', line 1318

static VALUE rb_git_repo_set_namespace(VALUE self, VALUE rb_namespace)
{
	git_repository *repo;
	int error;

	Data_Get_Struct(self, git_repository, repo);

	if (!NIL_P(rb_namespace)) {
		Check_Type(rb_namespace, T_STRING);
		error = git_repository_set_namespace(repo, StringValueCStr(rb_namespace));
	} else {
		error = git_repository_set_namespace(repo, NULL);
	}
	rugged_exception_check(error);

	return Qnil;
}

#pathObject

Return the full, normalized path to this repository. For non-bare repositories, this is the path of the actual .git folder, not the working directory.

repo.path #=> "/home/foo/workthing/.git"


829
830
831
832
833
834
# File 'ext/rugged/rugged_repo.c', line 829

static VALUE rb_git_repo_path(VALUE self)
{
	git_repository *repo;
	Data_Get_Struct(self, git_repository, repo);
	return rb_str_new_utf8(git_repository_path(repo));
}

#push(remote, refspecs) ⇒ Hash

Pushes the given refspecs to the given remote. Returns a hash that contains key-value pairs that reflect pushed refs and error messages, if applicable.

Example:

repo.push("origin", ["refs/heads/master", ":refs/heads/to_be_deleted"])

Returns:

  • (Hash)


1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
# File 'ext/rugged/rugged_repo.c', line 1218

static VALUE rb_git_repo_push(VALUE self, VALUE rb_remote, VALUE rb_refspecs)
{
	VALUE rb_refspec, rb_exception = Qnil, rb_result = rb_hash_new();
	git_repository *repo;
	git_remote *remote = NULL;
	git_push *push = NULL;

	int error = 0, i = 0;

	Check_Type(rb_refspecs, T_ARRAY);
	for (i = 0; i < RARRAY_LEN(rb_refspecs); ++i) {
		rb_refspec = rb_ary_entry(rb_refspecs, i);
		Check_Type(rb_refspec, T_STRING);
	}

	Data_Get_Struct(self, git_repository, repo);

	if (rb_obj_is_kind_of(rb_remote, rb_cRuggedRemote)) {
		Data_Get_Struct(rb_remote, git_remote, remote);
	} else if (TYPE(rb_remote) == T_STRING) {
		error = git_remote_load(&remote, repo, StringValueCStr(rb_remote));
		if (error) goto cleanup;
	} else {
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Remote instance");
	}

	error = git_push_new(&push, remote);
	if (error) goto cleanup;

	for (i = 0; !error && i < RARRAY_LEN(rb_refspecs); ++i) {
		rb_refspec = rb_ary_entry(rb_refspecs, i);
		error = git_push_add_refspec(push, StringValueCStr(rb_refspec));
	}
	if (error) goto cleanup;

	error = git_push_finish(push);

	if (error) {
		if (error == GIT_ENONFASTFORWARD) {
			rb_exception = rb_exc_new2(rb_eRuggedError, "non-fast-forward update rejected");
		} else if (error == -1) {
			rb_exception = rb_exc_new2(rb_eRuggedError, "could not push to repo (check for non-bare repo)");
		}

		goto cleanup;
	}

	if (!git_push_unpack_ok(push)) {
		rb_exception = rb_exc_new2(rb_eRuggedError, "the remote side did not unpack successfully");
		goto cleanup;
	}

	error = git_push_status_foreach(push, &rugged__push_status_cb, (void *)rb_result);
	if (error) goto cleanup;

	error = git_push_update_tips(push);

cleanup:
	git_push_free(push);

	// We can only free the remote if we have loaded it ourselves.
	if (!rb_obj_is_kind_of(rb_remote, rb_cRuggedRemote)) {
		git_remote_free(remote);
	}

	if (!NIL_P(rb_exception))
		rb_exc_raise(rb_exception);

	rugged_exception_check(error);

	return rb_result;
}

#read(oid) ⇒ String

Read and return the raw data of the object identified by the given oid.

Returns:

  • (String)


559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'ext/rugged/rugged_repo.c', line 559

static VALUE rb_git_repo_read(VALUE self, VALUE hex)
{
	git_repository *repo;
	git_oid oid;
	int error;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(hex, T_STRING);

	error = git_oid_fromstr(&oid, StringValueCStr(hex));
	rugged_exception_check(error);

	return rugged_raw_read(repo, &oid);
}

#read_header(oid) ⇒ Hash

Read and return the header information in repo‘s ODB for the object identified by the given oid.

Returns a Hash object with the following key/value pairs:

:type

A Symbol denoting the object’s type. Possible values are: :tree, :blob, :commit or :tag.

:len

A Number representing the object’s length, in bytes.

Returns:

  • (Hash)


589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'ext/rugged/rugged_repo.c', line 589

static VALUE rb_git_repo_read_header(VALUE self, VALUE hex)
{
	git_repository *repo;
	git_oid oid;
	git_odb *odb;
	git_otype type;
	size_t len;
	VALUE rb_hash;
	int error;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(hex, T_STRING);

	error = git_oid_fromstr(&oid, StringValueCStr(hex));
	rugged_exception_check(error);

	error = git_repository_odb(&odb, repo);
	rugged_exception_check(error);

	error = git_odb_read_header(&len, &type, odb, &oid);
	git_odb_free(odb);
	rugged_exception_check(error);

	rb_hash = rb_hash_new();
	rb_hash_aset(rb_hash, CSTR2SYM("type"), CSTR2SYM(git_object_type2string(type)));
	rb_hash_aset(rb_hash, CSTR2SYM("len"), INT2FIX(len));

	return rb_hash;
}

#ref(ref_name) ⇒ Object

Look up a single reference by name.

Example:

repo.ref 'refs/heads/master'
# => #<Rugged::Reference:2199125780 {name: "refs/heads/master",
       target: "25b5d3b40c4eadda8098172b26c68cf151109799"}>

Returns a Rugged::Reference.



94
95
96
# File 'lib/rugged/repository.rb', line 94

def ref(ref_name)
  Rugged::Reference.lookup(self, ref_name)
end

#ref_names(glob = nil) ⇒ Object



102
103
104
# File 'lib/rugged/repository.rb', line 102

def ref_names(glob = nil)
  Rugged::Reference.each_name(self, glob)
end

#refs(glob = nil) ⇒ Object



98
99
100
# File 'lib/rugged/repository.rb', line 98

def refs(glob = nil)
  Rugged::Reference.each(self, glob)
end

#remotesObject

All the remotes in the repository.

Returns an Enumerable::Enumerator containing all the Rugged::Remotes in the repository.



117
118
119
# File 'lib/rugged/repository.rb', line 117

def remotes
  Rugged::Remote.each(self)
end

#reset(target, reset_type) ⇒ nil

Sets the current head to the specified commit oid and optionally resets the index and working tree to match.

  • target: Rugged::Commit, Rugged::Tag or rev that resolves to a commit or tag object

  • reset_type: :soft, :mixed or :hard

:soft

the head will be moved to the commit.

:mixed

will trigger a :soft reset, plus the index will be replaced with the content of the commit tree.

:hard

will trigger a :mixed reset and the working directory will be replaced with the content of the index. (Untracked and ignored files will be left alone)

Examples:

repo.reset('origin/master', :hard) #=> nil

Returns:

  • (nil)


1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'ext/rugged/rugged_repo.c', line 1119

static VALUE rb_git_repo_reset(VALUE self, VALUE rb_target, VALUE rb_reset_type)
{
	git_repository *repo;
	int reset_type;
	git_object *target = NULL;
	int error;

	Data_Get_Struct(self, git_repository, repo);

	reset_type = parse_reset_type(rb_reset_type);
	target = rugged_object_get(repo, rb_target, GIT_OBJ_ANY);

	error = git_reset(repo, target, reset_type);

	git_object_free(target);
	rugged_exception_check(error);

	return Qnil;
}

#reset_path(pathspecs, target = nil) ⇒ nil

Updates entries in the index from the target commit tree, matching the given pathspecs.

Passing a nil target will result in removing entries in the index matching the provided pathspecs.

  • pathspecs: list of pathspecs to operate on (String or Array of String objects)

  • target(optional): Rugged::Commit, Rugged::Tag or rev that resolves to a commit or tag object.

Examples:

reset_path(File.join('subdir','file.txt'), '441034f860c1d5d90e4188d11ae0d325176869a8') #=> nil

Returns:

  • (nil)


1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'ext/rugged/rugged_repo.c', line 1155

static VALUE rb_git_repo_reset_path(int argc, VALUE *argv, VALUE self)
{
	git_repository *repo;
	git_object *target = NULL;
	git_strarray pathspecs;
	VALUE rb_target, rb_paths;
	VALUE rb_path_array;
	int i, error = 0;

	pathspecs.strings = NULL;
	pathspecs.count = 0;

	Data_Get_Struct(self, git_repository, repo);

	rb_scan_args(argc, argv, "11", &rb_paths, &rb_target);

	rb_path_array = rb_ary_to_ary(rb_paths);

	for (i = 0; i < RARRAY_LEN(rb_path_array); ++i)
		Check_Type(rb_ary_entry(rb_path_array, i), T_STRING);

	pathspecs.count = RARRAY_LEN(rb_path_array);
	pathspecs.strings = xmalloc(pathspecs.count * sizeof(char *));

	for (i = 0; i < RARRAY_LEN(rb_path_array); ++i) {
		VALUE fpath = rb_ary_entry(rb_path_array, i);
		pathspecs.strings[i] = StringValueCStr(fpath);
	}

	if (!NIL_P(rb_target))
		target = rugged_object_get(repo, rb_target, GIT_OBJ_ANY);

	error = git_reset_default(repo, target, &pathspecs);

	xfree(pathspecs.strings);
	git_object_free(target);

	rugged_exception_check(error);

	return Qnil;
}

#rev_parse(spec) ⇒ Object

Look up an object by a revision string.

Returns one of the four classes that inherit from Rugged::Object.



74
75
76
# File 'lib/rugged/repository.rb', line 74

def rev_parse(spec)
  Rugged::Object.rev_parse(self, spec)
end

#rev_parse_oid(spec) ⇒ Object

Look up an object by a revision string.

Returns the oid of the matched object as a String



81
82
83
# File 'lib/rugged/repository.rb', line 81

def rev_parse_oid(spec)
  Rugged::Object.rev_parse_oid(self, spec)
end

#status {|status_data| ... } ⇒ Object #status(path) ⇒ Object

Returns the status for one or more files in the working directory of the repository. This is equivalent to the git status command.

The returned status_data is always an array containing one or more status flags as Ruby symbols. Possible flags are:

  • :index_new: the file is new in the index

  • :index_modified: the file has been modified in the index

  • :index_deleted: the file has been deleted from the index

  • :worktree_new: the file is new in the working directory

  • :worktree_modified: the file has been modified in the working directory

  • :worktree_deleted: the file has been deleted from the working directory

If a block is given, status information will be gathered for every single file on the working dir. The block will be called with the status data for each file.

repo.status { |status_data| puts status_data.inspect }

results in, for example:

[:index_new, :worktree_new]
[:worktree_modified]

If a path is given instead, the function will return the status_data for the file pointed to by path, or raise an exception if the path doesn’t exist.

path must be relative to the repository’s working directory.

repo.status('src/diff.c') #=> [:index_new, :worktree_new]

Overloads:

  • #status {|status_data| ... } ⇒ Object

    Yields:

    • (status_data)


1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'ext/rugged/rugged_repo.c', line 1005

static VALUE rb_git_repo_status(int argc, VALUE *argv, VALUE self)
{
	int error;
	VALUE rb_path;
	git_repository *repo;

	Data_Get_Struct(self, git_repository, repo);

	if (rb_scan_args(argc, argv, "01", &rb_path) == 1) {
		unsigned int flags;
		Check_Type(rb_path, T_STRING);
		error = git_status_file(&flags, repo, StringValueCStr(rb_path));
		rugged_exception_check(error);

		return flags_to_rb(flags);
	}

	if (!rb_block_given_p())
		rb_raise(rb_eRuntimeError,
			"A block was expected for iterating through "
			"the repository contents.");

	error = git_status_foreach(
		repo,
		&rugged__status_cb,
		(void *)rb_block_proc()
	);

	rugged_exception_check(error);
	return Qnil;
}

#tags(pattern = "") ⇒ Object

All the tags in the repository.

Returns an Enumerable::Enumerator containing all the String tag names.



109
110
111
# File 'lib/rugged/repository.rb', line 109

def tags(pattern="")
  Rugged::Tag.each(self, pattern)
end

#walk(from, sorting = Rugged::SORT_DATE, &block) ⇒ Object

Walks over a set of commits using Rugged::Walker.

from - The String SHA1 to push onto Walker to begin our walk. sorting - The sorting order of the commits, as defined in the README. block - A block that we pass into walker#each.

Returns nothing if called with a block, otherwise returns an instance of Enumerable::Enumerator containing Rugged::Commit objects.



57
58
59
60
61
62
# File 'lib/rugged/repository.rb', line 57

def walk(from, sorting=Rugged::SORT_DATE, &block)
  walker = Rugged::Walker.new(self)
  walker.sorting(sorting)
  walker.push(from)
  walker.each(&block)
end

#workdirnil

Return the working directory for this repository, or nil if the repository is bare.

repo1.bare? #=> false
repo1.workdir #=> "/home/foo/workthing/"

repo2.bare? #=> true
repo2.workdir #=> nil

Returns:

  • (nil)


849
850
851
852
853
854
855
856
857
858
# File 'ext/rugged/rugged_repo.c', line 849

static VALUE rb_git_repo_workdir(VALUE self)
{
	git_repository *repo;
	const char *workdir;

	Data_Get_Struct(self, git_repository, repo);
	workdir = git_repository_workdir(repo);

	return workdir ? rb_str_new_utf8(workdir) : Qnil;
}

#workdir=(path) ⇒ Object

Sets the working directory of repo to path. All internal operations on repo that affect the working directory will instead use path.

The workdir can be set on bare repositories to temporarily turn them into normal repositories.

repo.bare? #=> true
repo.workdir = "/tmp/workdir"
repo.bare? #=> false
repo.checkout


876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'ext/rugged/rugged_repo.c', line 876

static VALUE rb_git_repo_set_workdir(VALUE self, VALUE rb_workdir)
{
	git_repository *repo;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(rb_workdir, T_STRING);

	rugged_exception_check(
		git_repository_set_workdir(repo, StringValueCStr(rb_workdir), 0)
	);

	return Qnil;
}

#write(buffer, type) ⇒ Object

Write the data contained in the buffer string as a raw object of the given type into the repository’s object database.

type can be either :tag, :commit, :tree or :blob.

Returns the newly created object’s oid.



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'ext/rugged/rugged_repo.c', line 687

static VALUE rb_git_repo_write(VALUE self, VALUE rb_buffer, VALUE rub_type)
{
	git_repository *repo;
	git_odb_stream *stream;

	git_odb *odb;
	git_oid oid;
	int error;

	git_otype type;

	Data_Get_Struct(self, git_repository, repo);
	Check_Type(rb_buffer, T_STRING);

	error = git_repository_odb(&odb, repo);
	rugged_exception_check(error);

	type = rugged_otype_get(rub_type);

	error = git_odb_open_wstream(&stream, odb, RSTRING_LEN(rb_buffer), type);
	git_odb_free(odb);
	rugged_exception_check(error);

	error = stream->write(stream, RSTRING_PTR(rb_buffer), RSTRING_LEN(rb_buffer));
	if (!error)
		error = stream->finalize_write(&oid, stream);

	stream->free(stream);
	rugged_exception_check(error);

	return rugged_create_oid(&oid);
}