Skip to content

Commit fe6405e

Browse files
authored
Merge pull request #714 from libgit2/move-status-to-ruby
Move Rugged::Repository#status to Ruby
2 parents 44727ed + daff726 commit fe6405e

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

ext/rugged/rugged_repo.c

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,58 +1511,26 @@ static int rugged__status_cb(const char *path, unsigned int flags, void *payload
15111511
return GIT_OK;
15121512
}
15131513

1514-
/*
1515-
* call-seq:
1516-
* repo.status { |file, status_data| block }
1517-
* repo.status(path) -> status_data
1518-
*
1519-
* Returns the status for one or more files in the working directory
1520-
* of the repository. This is equivalent to the +git status+ command.
1521-
*
1522-
* The returned +status_data+ is always an array containing one or more
1523-
* status flags as Ruby symbols. Possible flags are:
1524-
*
1525-
* - +:index_new+: the file is new in the index
1526-
* - +:index_modified+: the file has been modified in the index
1527-
* - +:index_deleted+: the file has been deleted from the index
1528-
* - +:worktree_new+: the file is new in the working directory
1529-
* - +:worktree_modified+: the file has been modified in the working directory
1530-
* - +:worktree_deleted+: the file has been deleted from the working directory
1531-
*
1532-
* If a +block+ is given, status information will be gathered for every
1533-
* single file on the working dir. The +block+ will be called with the
1534-
* status data for each file.
1535-
*
1536-
* repo.status { |file, status_data| puts "#{file} has status: #{status_data.inspect}" }
1537-
*
1538-
* results in, for example:
1539-
*
1540-
* src/diff.c has status: [:index_new, :worktree_new]
1541-
* README has status: [:worktree_modified]
1542-
*
1543-
* If a +path+ is given instead, the function will return the +status_data+ for
1544-
* the file pointed to by path, or raise an exception if the path doesn't exist.
1545-
*
1546-
* +path+ must be relative to the repository's working directory.
1547-
*
1548-
* repo.status('src/diff.c') #=> [:index_new, :worktree_new]
1549-
*/
1550-
static VALUE rb_git_repo_status(int argc, VALUE *argv, VALUE self)
1514+
static VALUE rb_git_repo_file_status(VALUE self, VALUE rb_path)
15511515
{
1516+
unsigned int flags;
15521517
int error;
1553-
VALUE rb_path;
15541518
git_repository *repo;
15551519

15561520
Data_Get_Struct(self, git_repository, repo);
1521+
Check_Type(rb_path, T_STRING);
1522+
error = git_status_file(&flags, repo, StringValueCStr(rb_path));
1523+
rugged_exception_check(error);
15571524

1558-
if (rb_scan_args(argc, argv, "01", &rb_path) == 1) {
1559-
unsigned int flags;
1560-
Check_Type(rb_path, T_STRING);
1561-
error = git_status_file(&flags, repo, StringValueCStr(rb_path));
1562-
rugged_exception_check(error);
1525+
return flags_to_rb(flags);
1526+
}
15631527

1564-
return flags_to_rb(flags);
1565-
}
1528+
static VALUE rb_git_repo_file_each_status(VALUE self)
1529+
{
1530+
int error;
1531+
git_repository *repo;
1532+
1533+
Data_Get_Struct(self, git_repository, repo);
15661534

15671535
if (!rb_block_given_p())
15681536
rb_raise(rb_eRuntimeError,
@@ -2605,7 +2573,8 @@ void Init_rugged_repo(void)
26052573
rb_define_method(rb_cRuggedRepo, "path", rb_git_repo_path, 0);
26062574
rb_define_method(rb_cRuggedRepo, "workdir", rb_git_repo_workdir, 0);
26072575
rb_define_method(rb_cRuggedRepo, "workdir=", rb_git_repo_set_workdir, 1);
2608-
rb_define_method(rb_cRuggedRepo, "status", rb_git_repo_status, -1);
2576+
rb_define_private_method(rb_cRuggedRepo, "file_status", rb_git_repo_file_status, 1);
2577+
rb_define_private_method(rb_cRuggedRepo, "each_status", rb_git_repo_file_each_status, 0);
26092578

26102579
rb_define_method(rb_cRuggedRepo, "index", rb_git_repo_get_index, 0);
26112580
rb_define_method(rb_cRuggedRepo, "index=", rb_git_repo_set_index, 1);

lib/rugged/repository.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,49 @@ def checkout(target, options = {})
5353
end
5454
end
5555

56+
###
57+
# call-seq:
58+
# repo.status { |file, status_data| block }
59+
# repo.status(path) -> status_data
60+
#
61+
# Returns the status for one or more files in the working directory
62+
# of the repository. This is equivalent to the +git status+ command.
63+
#
64+
# The returned +status_data+ is always an array containing one or more
65+
# status flags as Ruby symbols. Possible flags are:
66+
#
67+
# - +:index_new+: the file is new in the index
68+
# - +:index_modified+: the file has been modified in the index
69+
# - +:index_deleted+: the file has been deleted from the index
70+
# - +:worktree_new+: the file is new in the working directory
71+
# - +:worktree_modified+: the file has been modified in the working directory
72+
# - +:worktree_deleted+: the file has been deleted from the working directory
73+
#
74+
# If a +block+ is given, status information will be gathered for every
75+
# single file on the working dir. The +block+ will be called with the
76+
# status data for each file.
77+
#
78+
# repo.status { |file, status_data| puts "#{file} has status: #{status_data.inspect}" }
79+
#
80+
# results in, for example:
81+
#
82+
# src/diff.c has status: [:index_new, :worktree_new]
83+
# README has status: [:worktree_modified]
84+
#
85+
# If a +path+ is given instead, the function will return the +status_data+ for
86+
# the file pointed to by path, or raise an exception if the path doesn't exist.
87+
#
88+
# +path+ must be relative to the repository's working directory.
89+
#
90+
# repo.status('src/diff.c') #=> [:index_new, :worktree_new]
91+
def status(file = nil, &block)
92+
if file
93+
file_status file
94+
else
95+
each_status(&block)
96+
end
97+
end
98+
5699
def diff(left, right, opts = {})
57100
left = rev_parse(left) if left.kind_of?(String)
58101
right = rev_parse(right) if right.kind_of?(String)

0 commit comments

Comments
 (0)