Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GitWrapper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Georg Seibt
GitWrapper
Commits
7ae542a9
Commit
7ae542a9
authored
8 years ago
by
Georg Seibt
Browse files
Options
Downloads
Patches
Plain Diff
only clone if it is necessary
parent
8c0610b4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
+40
-9
40 additions, 9 deletions
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
with
40 additions
and
9 deletions
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
+
40
−
9
View file @
7ae542a9
...
...
@@ -20,6 +20,7 @@ public class Git {
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
Git
.
class
.
getCanonicalName
());
private
static
final
Pattern
CLONING_INTO
=
Pattern
.
compile
(
"Cloning into '(.*)'\\.\\.\\."
);
private
static
final
Pattern
ALREADY_EXISTS
=
Pattern
.
compile
(
"fatal: destination path '(.*)' already exists and is not an empty directory\\."
);
private
class
ExecRes
{
private
int
code
;
...
...
@@ -37,13 +38,6 @@ public class Git {
Optional
<
ExecRes
>
res
=
exec
(
parentDir
,
"clone"
,
url
);
Function
<
ExecRes
,
Repository
>
toRepo
=
execRes
->
{
if
(
execRes
.
code
!=
EXIT_SUCCESS
)
{
// the repo may already exist (resulting in code 128) - pull it? delete it?
LOG
.
warning
(()
->
String
.
format
(
"Exit code %d indicates failure while cloning '%s'."
,
execRes
.
code
,
url
));
return
null
;
}
Scanner
sc
=
new
Scanner
(
execRes
.
output
);
if
(!
sc
.
hasNextLine
())
{
...
...
@@ -51,7 +45,14 @@ public class Git {
return
null
;
}
Matcher
matcher
=
CLONING_INTO
.
matcher
(
sc
.
nextLine
());
String
firstLine
=
sc
.
nextLine
();
Matcher
matcher
;
if
(
execRes
.
code
==
EXIT_SUCCESS
)
{
matcher
=
CLONING_INTO
.
matcher
(
firstLine
);
}
else
{
matcher
=
ALREADY_EXISTS
.
matcher
(
firstLine
);
}
if
(!
matcher
.
find
())
{
LOG
.
warning
(()
->
String
.
format
(
"Unexpected output while cloning '%s'."
,
url
));
...
...
@@ -60,8 +61,20 @@ public class Git {
String
name
=
matcher
.
group
(
1
);
File
repoDir
=
new
File
(
parentDir
,
name
);
String
repoPath
=
repoDir
.
getAbsolutePath
();
if
(
execRes
.
code
==
EXIT_SUCCESS
)
{
LOG
.
fine
(()
->
String
.
format
(
"Cloned '%s' into '%s'."
,
url
,
repoPath
));
}
else
{
LOG
.
fine
(()
->
String
.
format
(
"Found '%s' which would be produced for '%s'"
,
repoPath
,
url
));
LOG
.
fine
(()
->
String
.
format
(
"Cloned '%s' into '%s'."
,
url
,
repoDir
.
getAbsolutePath
()));
if
(!
isGitDir
(
repoDir
))
{
LOG
.
warning
(()
->
String
.
format
(
"'%s' is not a git repository."
,
repoPath
));
return
null
;
}
LOG
.
fine
(()
->
String
.
format
(
"'%s' is a git repository. Assuming it is a clone of '%s'."
,
repoPath
,
url
));
}
return
new
Repository
(
this
,
url
,
repoDir
);
};
...
...
@@ -69,6 +82,24 @@ public class Git {
return
res
.
map
(
toRepo
);
}
/**
* Returns whether the given directory (or any of its parent directories) is a git repository. Returns false if
* <code>directory</code> is not a directory.
*
* @param directory
* the directory to check
* @return true iff the given directory (or any of its parent directories) is a git repository
*/
public
boolean
isGitDir
(
File
directory
)
{
if
(!
directory
.
isDirectory
())
{
return
false
;
}
Optional
<
ExecRes
>
status
=
exec
(
directory
,
"rev-parse"
,
"--is-inside-git-dir"
);
return
status
.
map
(
res
->
res
.
code
==
EXIT_SUCCESS
).
orElse
(
false
);
}
public
Optional
<
ExecRes
>
exec
(
File
workingDirectory
,
String
...
parameters
)
{
ProcessBuilder
b
=
new
ProcessBuilder
();
String
[]
cmdArray
=
new
String
[
parameters
.
length
+
1
];
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment