@@ -12,12 +12,24 @@ depsjl = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
12
12
isfile (depsjl) ? include (depsjl) : error (" Git.jl not properly installed. " *
13
13
" Please run\n Pkg.build(\" Git\" )" )
14
14
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
+ """
15
21
function dir (d)
16
22
g = joinpath (d," .git" )
17
23
isdir (g) && return g
18
24
normpath (d, Base. readchomp (setenv (` $gitcmd rev-parse --git-dir` , dir= d)))
19
25
end
20
26
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
+ """
21
33
git () = gitcmd
22
34
function git (d)
23
35
isempty (d) && return gitcmd
@@ -27,18 +39,55 @@ function git(d)
27
39
` $gitcmd --git-dir=$work_tree ` : ` $gitcmd --work-tree=$work_tree --git-dir=$git_dir `
28
40
end
29
41
42
+ """
43
+ Git.cmd(args; dir="")
44
+
45
+ Return a Git command from the given arguments, acting on the repository given in `dir`.
46
+ """
30
47
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
+ """
31
55
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
+ """
32
63
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
+ """
33
71
readchomp (args:: Cmd ; dir= " " ) = Base. readchomp (cmd (args,dir= dir))
34
72
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
+ """
35
79
function success (args:: Cmd ; dir= " " )
36
80
g = git (dir)
37
81
Base. readchomp (` $g rev-parse --is-bare-repository` ) == " false" &&
38
82
Base. run (` $g update-index -q --really-refresh` )
39
83
Base. success (` $g $args ` )
40
84
end
41
85
86
+ """
87
+ Git.version()
88
+
89
+ Return the version of Git being used by the package.
90
+ """
42
91
function version ()
43
92
vs = split (readchomp (` version` ), ' ' )[3 ]
44
93
ns = split (vs, ' .' )
@@ -49,32 +98,91 @@ function version()
49
98
end
50
99
end
51
100
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
+ """
52
107
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
+ """
53
114
different (verA:: AbstractString , verB:: AbstractString , path:: AbstractString ; dir= " " ) =
54
115
! success (` diff-tree --quiet $verA $verB -- $path ` , dir= dir)
55
116
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
+ """
56
123
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)
59
124
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)
60
132
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)
61
140
unstaged (paths; dir= " " ) = ! success (` diff-files --quiet -- $paths ` , dir= dir)
62
141
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
+ """
63
148
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
+ """
64
158
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
+ """
65
165
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
+ """
66
172
head (; dir= " " ) = readchomp (` rev-parse HEAD` , dir= dir)
67
173
68
- function iscommit (sha1s:: Vector ; dir= " " )
69
- indexin (sha1s,split (readchomp (` log --all --format=%H` , dir= dir)," \n " )).!= 0
70
- end
71
174
72
175
immutable State
73
176
head:: Compat.UTF8String
74
177
index:: Compat.UTF8String
75
178
work:: Compat.UTF8String
76
179
end
77
180
181
+ """
182
+ Git.snapshot(; dir="")
183
+
184
+ Return a `State` object that captures a snapshot of the given repository.
185
+ """
78
186
function snapshot (; dir= " " )
79
187
head = readchomp (` rev-parse HEAD` , dir= dir)
80
188
index = readchomp (` write-tree` , dir= dir)
@@ -90,6 +198,11 @@ function snapshot(; dir="")
90
198
State (head, index, work)
91
199
end
92
200
201
+ """
202
+ Git.restore(s::State; dir="")
203
+
204
+ Restore the given repository to the state `s`.
205
+ """
93
206
function restore (s:: State ; dir= " " )
94
207
run (` reset -q --` , dir= dir) # unstage everything
95
208
run (` read-tree $(s. work) ` , dir= dir) # move work tree to index
@@ -99,6 +212,12 @@ function restore(s::State; dir="")
99
212
run (` reset -q --soft $(s. head) ` , dir= dir) # restore head
100
213
end
101
214
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
+ """
102
221
function transact (f:: Function ; dir= " " )
103
222
state = snapshot (dir= dir)
104
223
try f () catch
@@ -107,6 +226,11 @@ function transact(f::Function; dir="")
107
226
end
108
227
end
109
228
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
+ """
110
234
function is_ancestor_of (a:: AbstractString , b:: AbstractString ; dir= " " )
111
235
A = readchomp (` rev-parse $a ` , dir= dir)
112
236
readchomp (` merge-base $A $b ` , dir= dir) == A
115
239
const GITHUB_REGEX =
116
240
r" ^(?:git@|git://|https://(?:[\w\.\+\- ]+@)?)github.com[:/](([^/].+)/(.+?))(?:\. git)?$" i
117
241
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
+ """
118
247
function set_remote_url (url:: AbstractString ; remote:: AbstractString = " origin" , dir= " " )
119
248
run (` config remote.$remote .url $url ` , dir= dir)
120
249
m = match (GITHUB_REGEX,url)
@@ -123,6 +252,11 @@ function set_remote_url(url::AbstractString; remote::AbstractString="origin", di
123
252
push != url && run (` config remote.$remote .pushurl $push ` , dir= dir)
124
253
end
125
254
255
+ """
256
+ Git.normalize_url(url)
257
+
258
+ Normalize the given URL to a valid GitHub repository URL.
259
+ """
126
260
function normalize_url (url:: AbstractString )
127
261
m = match (GITHUB_REGEX,url)
128
262
m === nothing ? url : " git://github.com/$(m. captures[1 ]) .git"
0 commit comments