Skip to content

Commit bb6821d

Browse files
authored
Merge pull request #9 from JuliaPackaging/aa/docstrings
Add docstrings for all commands
2 parents 4b7a327 + fd9a3fd commit bb6821d

File tree

1 file changed

+139
-5
lines changed

1 file changed

+139
-5
lines changed

src/Git.jl

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ depsjl = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
1212
isfile(depsjl) ? include(depsjl) : error("Git.jl not properly installed. " *
1313
"Please run\nPkg.build(\"Git\")")
1414

15+
"""
16+
Git.dir(d)
17+
18+
Return the path to the default `.git` for the given repository directory, or the
19+
path to use in place of the default `.git`.
20+
"""
1521
function dir(d)
1622
g = joinpath(d,".git")
1723
isdir(g) && return g
1824
normpath(d, Base.readchomp(setenv(`$gitcmd rev-parse --git-dir`, dir=d)))
1925
end
2026

27+
"""
28+
Git.git([d])
29+
30+
Return a Git command that refers to the work tree and directory given by `d`, or the
31+
current work tree and directory if `d` is not specified.
32+
"""
2133
git() = gitcmd
2234
function git(d)
2335
isempty(d) && return gitcmd
@@ -27,18 +39,55 @@ function git(d)
2739
`$gitcmd --git-dir=$work_tree` : `$gitcmd --work-tree=$work_tree --git-dir=$git_dir`
2840
end
2941

42+
"""
43+
Git.cmd(args; dir="")
44+
45+
Return a Git command from the given arguments, acting on the repository given in `dir`.
46+
"""
3047
cmd(args::Cmd; dir="") = `$(git(dir)) $args`
48+
49+
"""
50+
Git.run(args; dir="", out=STDOUT)
51+
52+
Execute the Git command from the given arguments `args` on the repository `dir`, writing
53+
the results to the output stream `out`.
54+
"""
3155
run(args::Cmd; dir="", out=STDOUT) = Base.run(pipeline(cmd(args,dir=dir), out))
56+
57+
"""
58+
Git.readstring(args; dir="")
59+
60+
Read the result of the Git command using the given arguments on the given repository
61+
as a string.
62+
"""
3263
readstring(args::Cmd; dir="") = Base.readstring(cmd(args,dir=dir))
64+
65+
"""
66+
Git.readchomp(args; dir="")
67+
68+
Read the result of the Git command using the given arguments on the given repository
69+
as a string, removing a single trailing newline if present.
70+
"""
3371
readchomp(args::Cmd; dir="") = Base.readchomp(cmd(args,dir=dir))
3472

73+
"""
74+
Git.success(args; dir="")
75+
76+
Determine whether the Git command using the given arguments on the given repository
77+
executed successfully.
78+
"""
3579
function success(args::Cmd; dir="")
3680
g = git(dir)
3781
Base.readchomp(`$g rev-parse --is-bare-repository`) == "false" &&
3882
Base.run(`$g update-index -q --really-refresh`)
3983
Base.success(`$g $args`)
4084
end
4185

86+
"""
87+
Git.version()
88+
89+
Return the version of Git being used by the package.
90+
"""
4291
function version()
4392
vs = split(readchomp(`version`), ' ')[3]
4493
ns = split(vs, '.')
@@ -49,32 +98,91 @@ function version()
4998
end
5099
end
51100

101+
"""
102+
Git.modules(args; dir="")
103+
104+
Apply the Git command with the given arguments on the given repository to the
105+
configuration file `.gitmodules` and read the result as a string.
106+
"""
52107
modules(args::Cmd; dir="") = readchomp(`config -f .gitmodules $args`, dir=dir)
108+
109+
"""
110+
Git.different(verA, verB, path; dir="")
111+
112+
Determine whether two trees are different with respect to the given path.
113+
"""
53114
different(verA::AbstractString, verB::AbstractString, path::AbstractString; dir="") =
54115
!success(`diff-tree --quiet $verA $verB -- $path`, dir=dir)
55116

117+
"""
118+
Git.dirty([paths]; dir="")
119+
120+
Determine whether the paths in the given repository are dirty, i.e. contain modified but
121+
uncommitted tracked files.
122+
"""
56123
dirty(; dir="") = !success(`diff-index --quiet HEAD`, dir=dir)
57-
staged(; dir="") = !success(`diff-index --quiet --cached HEAD`, dir=dir)
58-
unstaged(; dir="") = !success(`diff-files --quiet`, dir=dir)
59124
dirty(paths; dir="") = !success(`diff-index --quiet HEAD -- $paths`, dir=dir)
125+
126+
"""
127+
Git.staged([paths]; dir="")
128+
129+
Determine whether the paths in the given repository contain staged files.
130+
"""
131+
staged(; dir="") = !success(`diff-index --quiet --cached HEAD`, dir=dir)
60132
staged(paths; dir="") = !success(`diff-index --quiet --cached HEAD -- $paths`, dir=dir)
133+
134+
"""
135+
Git.unstaged([paths]; dir="")
136+
137+
Determine whether the paths in the given repository contain unstaged files.
138+
"""
139+
unstaged(; dir="") = !success(`diff-files --quiet`, dir=dir)
61140
unstaged(paths; dir="") = !success(`diff-files --quiet -- $paths`, dir=dir)
62141

142+
"""
143+
Git.iscommit(name; dir="")
144+
145+
Determine whether `name` refers to a commit in the repository `dir`. `name` can be a
146+
single SHA1 or a vector of SHA1s.
147+
"""
63148
iscommit(name; dir="") = success(`cat-file commit $name`, dir=dir)
149+
function iscommit(sha1s::Vector; dir="")
150+
indexin(sha1s,split(readchomp(`log --all --format=%H`, dir=dir),"\n")).!=0
151+
end
152+
153+
"""
154+
Git.attached(; dir="")
155+
156+
Determine whether HEAD is attached to a commit in the given respository.
157+
"""
64158
attached(; dir="") = success(`symbolic-ref -q HEAD`, dir=dir)
159+
160+
"""
161+
Git.branch(; dir="")
162+
163+
Return the name of the current active branch in the given repository.
164+
"""
65165
branch(; dir="") = readchomp(`rev-parse --symbolic-full-name --abbrev-ref HEAD`, dir=dir)
166+
167+
"""
168+
Git.head(; dir="")
169+
170+
Return the commit to which HEAD currently refers.
171+
"""
66172
head(; dir="") = readchomp(`rev-parse HEAD`, dir=dir)
67173

68-
function iscommit(sha1s::Vector; dir="")
69-
indexin(sha1s,split(readchomp(`log --all --format=%H`, dir=dir),"\n")).!=0
70-
end
71174

72175
immutable State
73176
head::Compat.UTF8String
74177
index::Compat.UTF8String
75178
work::Compat.UTF8String
76179
end
77180

181+
"""
182+
Git.snapshot(; dir="")
183+
184+
Return a `State` object that captures a snapshot of the given repository.
185+
"""
78186
function snapshot(; dir="")
79187
head = readchomp(`rev-parse HEAD`, dir=dir)
80188
index = readchomp(`write-tree`, dir=dir)
@@ -90,6 +198,11 @@ function snapshot(; dir="")
90198
State(head, index, work)
91199
end
92200

201+
"""
202+
Git.restore(s::State; dir="")
203+
204+
Restore the given repository to the state `s`.
205+
"""
93206
function restore(s::State; dir="")
94207
run(`reset -q --`, dir=dir) # unstage everything
95208
run(`read-tree $(s.work)`, dir=dir) # move work tree to index
@@ -99,6 +212,12 @@ function restore(s::State; dir="")
99212
run(`reset -q --soft $(s.head)`, dir=dir) # restore head
100213
end
101214

215+
"""
216+
Git.transact(f; dir="")
217+
218+
Attempt to execute the function `f`. If this fails, the repository is restored to its
219+
state prior to execution.
220+
"""
102221
function transact(f::Function; dir="")
103222
state = snapshot(dir=dir)
104223
try f() catch
@@ -107,6 +226,11 @@ function transact(f::Function; dir="")
107226
end
108227
end
109228

229+
"""
230+
Git.is_ancestor_of(a, b; dir="")
231+
232+
Determine whether the commit `a` is an ancestor of the commit `b` in the given repository.
233+
"""
110234
function is_ancestor_of(a::AbstractString, b::AbstractString; dir="")
111235
A = readchomp(`rev-parse $a`, dir=dir)
112236
readchomp(`merge-base $A $b`, dir=dir) == A
@@ -115,6 +239,11 @@ end
115239
const GITHUB_REGEX =
116240
r"^(?:git@|git://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i
117241

242+
"""
243+
Git.set_remote_url(url; remote="origin", dir="")
244+
245+
Add a remote `remote` to the given repository from the URL `url`.
246+
"""
118247
function set_remote_url(url::AbstractString; remote::AbstractString="origin", dir="")
119248
run(`config remote.$remote.url $url`, dir=dir)
120249
m = match(GITHUB_REGEX,url)
@@ -123,6 +252,11 @@ function set_remote_url(url::AbstractString; remote::AbstractString="origin", di
123252
push != url && run(`config remote.$remote.pushurl $push`, dir=dir)
124253
end
125254

255+
"""
256+
Git.normalize_url(url)
257+
258+
Normalize the given URL to a valid GitHub repository URL.
259+
"""
126260
function normalize_url(url::AbstractString)
127261
m = match(GITHUB_REGEX,url)
128262
m === nothing ? url : "git://github.com/$(m.captures[1]).git"

0 commit comments

Comments
 (0)