11defmodule C.Git do
2- import C.Util , only: [ cmd: 2 , cmd: 3 , open_in_editor: 1 ]
2+ import C.Util , only: [ cmd: 2 , cmd: 3 , result: 2 , result: 3 , open_in_editor: 1 ]
33
44 defmodule Commit do
55 defstruct [ :hash , :subject , :body ,
@@ -25,8 +25,18 @@ defmodule C.Git do
2525 branch_name
2626 end
2727
28+ def branch_names ( ) do
29+ with { :ok , lines } <- result ( "git" , ~w{ for-each-ref --shell --format='%(refname)' refs/heads/} ) ,
30+ do: { :ok , String . split ( lines , "\n " ) |> Enum . map ( & clean_branch_name / 1 ) }
31+ end
32+ def clean_branch_name ( branch_name ) do
33+ branch_name
34+ |> String . replace_prefix ( "''refs/heads/" , "" )
35+ |> String . replace_suffix ( "''" , "" )
36+ end
37+
2838 def show_in_editor ( commit_with_file_path ) do
29- [ commit , file_path ] = String . split ( commit_with_file_path , ":" , parts: 2 )
39+ [ _commit , file_path ] = String . split ( commit_with_file_path , ":" , parts: 2 )
3040 tmp_dir = System . tmp_dir ( )
3141 file_name = Path . basename ( file_path )
3242 tmp_file_path = Path . join ( tmp_dir , file_name )
@@ -39,7 +49,7 @@ defmodule C.Git do
3949 def search_history ( keywords ) do
4050 with { :ok , commits } <- list_commits ( ) ,
4151 commit_hashes <- Enum . map ( commits , & Map . get ( & 1 , :hash ) ) ,
42- { :ok , raw } <- cmd ( "git" , [ "grep" ] ++ @ grep_opts ++ match_patterns ( keywords ) ++ commit_hashes ) ,
52+ { :ok , raw } <- result ( "git" , [ "grep" ] ++ @ grep_opts ++ match_patterns ( keywords ) ++ commit_hashes ) ,
4353 entries <- parse_search_history ( raw , commits ) ,
4454 do: filter_to_latest ( entries )
4555 end
@@ -74,7 +84,7 @@ defmodule C.Git do
7484 fields = [ :hash , :author_date , :author_name , :subject ]
7585 format = rev_parse_format ( fields )
7686 filter = [ "head" ]
77- with { :ok , commit_string } <- cmd ( "git" , [ "rev-list" ] ++ filter ++ [ "--format=#{ format } " ] , [ dir: dir ] ) ,
87+ with { :ok , commit_string } <- result ( "git" , [ "rev-list" ] ++ filter ++ [ "--format=#{ format } " ] , [ dir: dir ] ) ,
7888 do: { :ok , parse_commits ( commit_string , fields ) }
7989 end
8090
@@ -136,18 +146,18 @@ defmodule C.Git do
136146 end
137147
138148 def git_url ( remote_name ) do
139- with { :ok , url } <- cmd ( "git" , [ "ls-remote" , "--get-url" , remote_name ] ) , do: url
149+ with { :ok , url } <- result ( "git" , [ "ls-remote" , "--get-url" , remote_name ] ) , do: url
140150 end
141151
142152 def set_config ( key , value ) do
143153 cmd ( "git" , [ "config" , "--global" , key , value ] )
144154 end
145155 def get_config ( key ) do
146- cmd ( "git" , [ "config" , "--get" , key ] )
156+ result ( "git" , [ "config" , "--get" , key ] )
147157 end
148158
149159 def match_patterns ( keywords ) do
150160 Enum . flat_map ( keywords , fn k -> [ "-e" , k ] end )
151161 end
152-
162+
153163end
0 commit comments