Skip to content

Commit

Permalink
Add an option for running elvis git-branch BRANCH|SHA. Similar to g…
Browse files Browse the repository at this point in the history
…it-hook but instead takes all changes since BRANCH or SHA

- Minor cleanup in git_SUITE
  • Loading branch information
onnovos committed Nov 25, 2016
1 parent eaf6abc commit fcedc82
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ process_commands(['install', 'git-hook' | Cmds], Config) ->
process_commands(['git-hook' | Cmds], Config) ->
elvis_git:run_hook(Config),
process_commands(Cmds, Config);
process_commands(['git-branch', Branch | Cmds], Config) ->
elvis_git:run_branch(atom_to_list(Branch), Config),
process_commands(Cmds, Config);
process_commands([], _Config) ->
ok;
process_commands([_Cmd | _Cmds], _Config) ->
Expand Down Expand Up @@ -142,6 +145,11 @@ rock [file...] Rock your socks off by running all rules to your source files.
git-hook Pre-commit Git Hook: Gets all staged files and runs the rules
specified in the configuration to those
files.
git-branch [branch|sha]
Rock your socks off by running all rules on source files that
have changed since branch or sha.
install git-hook
Installs Elvis as a pre-commit hook in your current working
directory, which should be a git repository.
Expand Down
24 changes: 24 additions & 0 deletions src/elvis_git.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

-export([
run_hook/1,
run_branch/2,
staged_files/0,
branch_files/1,
staged_content/1,
relative_position/2,
install_hook/0
Expand All @@ -11,6 +13,9 @@
-define(LIST_STAGED,
"git diff --name-status --staged | awk '$1 != \"D\" { print $2 }'").

-define(LIST_BRANCH_CHANGES(B),
"git diff --name-only --ignore-submodules=all --diff-filter=d " ++ B).

-define(STAGED_CONTENT(Path),
"git show :" ++ Path).

Expand All @@ -29,6 +34,25 @@ run_hook(Config) ->
ok -> ok
end.

-spec run_branch(string(), elvis_config:config()) -> ok.
run_branch(Branch, Config) ->
Files = elvis_git:branch_files(Branch),
Results = lists:map(fun(File) ->
elvis_core:rock_this(File, Config)
end, Files),
case lists:any(fun(Res) -> Res /= ok end, Results) of
true -> elvis_utils:erlang_halt(1);
false -> ok
end.

-spec branch_files(string()) -> [elvis_file:file()].
branch_files(Branch) ->
Cmd = ?LIST_BRANCH_CHANGES(Branch),
Output = list_to_binary(os:cmd(Cmd)),

Lines = binary:split(Output, <<"\n">>, [global]),
[binary_to_list(Path) || Path <- Lines, byte_size(Path) > 0].

-spec staged_files() -> [elvis_file:file()].
staged_files() ->
Cmd = ?LIST_STAGED,
Expand Down
38 changes: 31 additions & 7 deletions test/git_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
-export([
relative_position_from_patch/1,
check_staged_files/1,
ignore_deleted_files/1
ignore_deleted_files/1,
check_branch_files/1
]).

-define(EXCLUDED_FUNS,
Expand Down Expand Up @@ -66,16 +67,17 @@ relative_position_from_patch(_Config) ->

-spec check_staged_files(config()) -> any().
check_staged_files(_Config) ->
Filename = "../../temp_file_test",
ok = file:write_file(Filename, <<"sdsds">>, [append]),
FileName = random_file_name(),
FileLocation = "../../" ++ FileName,
ok = file:write_file(FileLocation, <<"sdsds">>, [append]),

_ = os:cmd("git add " ++ Filename),
_ = os:cmd("git add " ++ FileLocation),
StagedFiles = elvis_git:staged_files(),
FilterFun = fun (#{path := Path}) -> Path == "temp_file_test" end,
FilterFun = fun (#{path := Path}) -> Path == FileName end,
[_] = lists:filter(FilterFun, StagedFiles),
_ = os:cmd("git reset " ++ Filename),
_ = os:cmd("git reset " ++ FileLocation),

file:delete(Filename).
file:delete(FileLocation).

-spec ignore_deleted_files(config()) -> any().
ignore_deleted_files(Config) ->
Expand All @@ -92,3 +94,25 @@ ignore_deleted_files(Config) ->
StagedFiles = lists:sort([Path
|| #{path := Path} <- elvis_git:staged_files()]),
["test2", "test3"] = StagedFiles.

-spec check_branch_files(config()) -> any().
check_branch_files(_Config) ->
_ = os:cmd("git stash"),

FileName = random_file_name(),
FileLocation = "../../" ++ FileName,
ok = file:write_file(FileLocation, <<"sdsds">>, [append]),

_ = os:cmd("git add " ++ FileLocation),
_ = os:cmd("git commit -m \"some commit\""),
[FileName] = elvis_git:branch_files("HEAD^"),
_ = os:cmd("git reset --hard HEAD^"),

_ = os:cmd("git stash pop"),

file:delete(FileLocation).

-spec random_file_name() -> string().
random_file_name() ->
RandomString = integer_to_list(crypto:rand_uniform(0, 1 bsl 127)),
"test_file_" ++ RandomString.

0 comments on commit fcedc82

Please sign in to comment.