Skip to content

Commit 436eb58

Browse files
committed
start of git start command
1 parent aa3a8a6 commit 436eb58

7 files changed

Lines changed: 97 additions & 64 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ erl_crash.dump
1919
.DS_Store
2020

2121
c
22+
.elixir_ls

lib/cli.ex

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule C.CLI do
2-
import C.Util, only: [cmd: 2]
2+
alias C.Git.Repo, as: Repo
33

44
def main(args) do
55
{_opts, args, _} = OptionParser.parse(args)
@@ -8,6 +8,21 @@ defmodule C.CLI do
88
execute(cmd, rest_args)
99
end
1010

11+
def master_branch(), do: "master"
12+
def origin, do: "origin"
13+
14+
def repo(), do: Repo.new(dir: File.cwd!(), master_branch: master_branch())
15+
16+
def execute("start", branch_name_parts) do
17+
branch_name = Enum.join(branch_name_parts, "-")
18+
Repo.ensure_changes_committed!(repo())
19+
Repo.pull(repo(), origin(), master_branch())
20+
Repo.create_branch(repo(), branch_name, master_branch())
21+
end
22+
def execute("branches", _) do
23+
{:ok, branches} = C.Git.branch_names()
24+
IO.inspect(branches)
25+
end
1126
def execute("look", keywords) do
1227
C.Git.search_history(keywords)
1328
|> format_look()
@@ -22,7 +37,7 @@ defmodule C.CLI do
2237
end
2338
def execute("files", patterns) do
2439
pattern = Enum.join([""] ++ patterns ++ [""], "*")
25-
{:ok, matches} = C.Util.cmd("find", [".", "-type", "f", "-path", pattern])
40+
{:ok, matches} = C.Util.result("find", [".", "-type", "f", "-path", pattern])
2641
IO.puts(matches)
2742
end
2843
def execute("req", _) do
@@ -45,7 +60,9 @@ defmodule C.CLI do
4560
search_uri = C.ExURI.merge_query_params("https://github.com/search", params) <> "&q=" <> Enum.join(keywords, "+")
4661
C.Util.open_in_browser(search_uri)
4762
end
48-
def execute("pwd", _), do: cmd("pwd", [])
63+
def execute(name, _) do
64+
raise "Unknown recognized command #{name}"
65+
end
4966

5067
def get_path([]), do: File.cwd!()
5168
def get_path([path]), do: path

lib/ex_uri.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule C.ExURI do
2-
2+
33
def merge_query_params(%URI{query: nil}=uri, query_params) do
44
uri = %{uri | query: ""}
55
merge_query_params(uri, query_params)
@@ -29,6 +29,5 @@ defmodule C.ExURI do
2929

3030
defp merge_resolve(_k, v1, nil), do: v1
3131
defp merge_resolve(_k, _v1, v2), do: v2
32-
33-
end
34-
32+
33+
end

lib/git.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule 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+
153163
end

lib/git/repo.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
defmodule C.Git.Repo do
2+
defstruct [:dir, master_branch: "master"]
3+
alias C.Git.Repo, as: R
4+
import C.Util, only: [cmd: 2, cmd: 3, result: 2, result: 3, open_in_editor: 1]
5+
6+
def new(opts), do: struct(R, opts)
7+
8+
def pull(%R{dir: dir}, origin_name, branch_name) do
9+
cmd("git", ["pull", origin_name, "#{branch_name}:#{branch_name}"], dir: dir)
10+
end
11+
12+
def create_branch(%R{dir: dir}, branch_name, source) do
13+
cmd("git", ["checkout", "-b", branch_name, source], dir: dir)
14+
end
15+
def checkout_branch(%R{dir: dir}, branch_name) when is_binary(branch_name) do
16+
cmd("git", ["checkout", branch_name], dir: dir)
17+
end
18+
19+
def ensure_changes_committed!(repo) do
20+
case uncommitted_changes?(repo) do
21+
true -> raise "You have uncommitted changes. Please stash them or commit them first."
22+
false -> nil
23+
end
24+
end
25+
26+
def checkout(%R{dir: dir}, %{hash: hash}), do: checkout(hash, dir)
27+
def checkout(%R{dir: dir}, commit_hash) when is_binary(commit_hash) do
28+
cmd("git", ["checkout", commit_hash], dir: dir)
29+
end
30+
31+
def uncommitted_changes?(%R{dir: dir}) do
32+
case result("git", ["status", "--porcelain"], dir: dir) do
33+
{:ok, v} when v != "" -> true
34+
_ -> false
35+
end
36+
end
37+
38+
def stash!(%R{dir: dir}) do
39+
cmd("git", ["stash"], dir: dir)
40+
end
41+
42+
def stash_pop!(%R{dir: dir}) do
43+
cmd("git", ["stash", "pop"], dir: dir)
44+
end
45+
46+
end

lib/play.ex

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,2 @@
11
defmodule Play do
2-
@points [%{"end" => 14000, "message" => "And when the introduction keeps playing",
3-
"start" => 11000},
4-
%{"end" => 18000,
5-
"message" => "We go to the second transition of the introduction",
6-
"start" => 14000},
7-
%{"end" => 22000, "message" => "We switch to the design section",
8-
"start" => 18000},
9-
%{"end" => 26000, "message" => "And the design section will show a picture",
10-
"start" => 22000},
11-
%{"end" => 30000,
12-
"message" => "When I say the next word, we switch to the second picture",
13-
"start" => 26000},
14-
%{"end" => 34000, "message" => "And then the third picture", "start" => 30000},
15-
%{"end" => 37000, "message" => "Then we switch to the architectural section",
16-
"start" => 34000},
17-
%{"end" => 41000, "message" => "And you see a picture", "start" => 37000},
18-
%{"end" => 45000,
19-
"message" => "And you talk a little bit more and there is a blog post",
20-
"start" => 41000},
21-
%{"end" => 53000,
22-
"message" => "If you scroll down to the blog post it pauses what I say",
23-
"start" => 46000},
24-
%{"end" => 60000,
25-
"message" => "Now that you scroll back up, I resume, and we keep talking",
26-
"start" => 54000},
27-
%{"end" => 61000, "message" => "And we go to the roadmap section",
28-
"start" => 60000},
29-
%{"end" => 66000,
30-
"message" => "We're going through a little bit of a demo about the app model",
31-
"start" => 62000},
32-
%{"end" => 69000, "message" => "There's some fancy animation",
33-
"start" => 66000},
34-
%{"end" => 73000, "message" => "Talking about investors and paid users",
35-
"start" => 69000},
36-
%{"end" => 78000,
37-
"message" => "What happens when all of this goes horribly wrong",
38-
"start" => 73000}]
39-
40-
def run(base) do
41-
origin(@points, base)
42-
|> Enum.map(&Poison.encode!/1)
43-
|> Enum.each(&IO.puts/1)
44-
end
45-
46-
def origin(points, origin) do
47-
Enum.map(points, fn %{"start" => vstart, "end" => vend}=row ->
48-
%{row | "start" => vstart - origin, "end" => vend - origin}
49-
end)
50-
end
51-
end
2+
end

lib/util.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ defmodule C.Util do
44
@browser "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
55

66
def cmd(command, args, opts \\ []) do
7+
opts = opts ++ [err: :out, out: IO.binstream(:standard_io, :line)]
8+
IO.puts(Enum.join([command | args], " "))
9+
case Porcelain.exec(command, args, opts) do
10+
%{status: 1} -> exit(:shutdown)
11+
result -> result
12+
end
13+
end
14+
15+
def result(command, args, opts \\ []) do
716
opts = opts ++ [err: :out]
817
case Porcelain.exec(command, args, opts) do
918
%{status: 0, out: result} -> {:ok, String.trim(result)}

0 commit comments

Comments
 (0)