Skip to content

Commit 8f37b58

Browse files
authored
Merge pull request #783 from libgit2/cmn/gitfile
Expose methods to detect .gitignore and .gitattributes files
2 parents 7ba25e2 + 638c8b3 commit 8f37b58

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

ext/rugged/rugged.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,48 @@ VALUE rb_merge_file_result_fromC(const git_merge_file_result *result)
515515
return rb_result;
516516
}
517517

518+
static VALUE rb_git_path_is_dotgit_modules(VALUE self, VALUE rb_path)
519+
{
520+
const char *path;
521+
int is_dotgit;
522+
523+
Check_Type(rb_path, T_STRING);
524+
525+
path = StringValueCStr(rb_path);
526+
527+
is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_GENERIC);
528+
529+
return is_dotgit ? Qtrue : Qfalse;
530+
}
531+
532+
static VALUE rb_git_path_is_dotgit_ignore(VALUE self, VALUE rb_path)
533+
{
534+
const char *path;
535+
int is_dotgit;
536+
537+
Check_Type(rb_path, T_STRING);
538+
539+
path = StringValueCStr(rb_path);
540+
541+
is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITIGNORE, GIT_PATH_FS_GENERIC);
542+
543+
return is_dotgit ? Qtrue : Qfalse;
544+
}
545+
546+
static VALUE rb_git_path_is_dotgit_attributes(VALUE self, VALUE rb_path)
547+
{
548+
const char *path;
549+
int is_dotgit;
550+
551+
Check_Type(rb_path, T_STRING);
552+
553+
path = StringValueCStr(rb_path);
554+
555+
is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITATTRIBUTES, GIT_PATH_FS_GENERIC);
556+
557+
return is_dotgit ? Qtrue : Qfalse;
558+
}
559+
518560
void Init_rugged(void)
519561
{
520562
rb_mRugged = rb_define_module("Rugged");
@@ -544,6 +586,9 @@ void Init_rugged(void)
544586
rb_define_module_function(rb_mRugged, "prettify_message", rb_git_prettify_message, -1);
545587
rb_define_module_function(rb_mRugged, "__cache_usage__", rb_git_cache_usage, 0);
546588
rb_define_module_function(rb_mRugged, "signature_from_buffer", rb_git_signature_from_buffer, -1);
589+
rb_define_module_function(rb_mRugged, "dotgit_modules?", rb_git_path_is_dotgit_modules, 1);
590+
rb_define_module_function(rb_mRugged, "dotgit_ignore?", rb_git_path_is_dotgit_ignore, 1);
591+
rb_define_module_function(rb_mRugged, "dotgit_attributes?", rb_git_path_is_dotgit_attributes, 1);
547592

548593
Init_rugged_reference();
549594
Init_rugged_reference_collection();

ext/rugged/rugged.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <assert.h>
2626
#include <git2.h>
2727
#include <git2/odb_backend.h>
28+
#include <git2/sys/path.h>
2829

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

test/dotgit_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "test_helper"
2+
3+
class DotgitTest < Rugged::TestCase
4+
def test_dotgit
5+
assert Rugged.dotgit_modules?(".gitmodules")
6+
assert Rugged.dotgit_modules?(".git\xe2\x80\x8cmodules")
7+
assert Rugged.dotgit_modules?("GITMOD~1")
8+
assert Rugged.dotgit_modules?("gi7eba~9")
9+
10+
refute Rugged.dotgit_modules?("gitmodules")
11+
refute Rugged.dotgit_modules?("GI7EBA~0")
12+
13+
assert Rugged.dotgit_ignore?(".gitignore")
14+
refute Rugged.dotgit_ignore?(".gitignores")
15+
16+
assert Rugged.dotgit_attributes?(".gitattributes")
17+
refute Rugged.dotgit_attributes?(".gittattributtes")
18+
19+
end
20+
end

0 commit comments

Comments
 (0)