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
33e62773
Commit
33e62773
authored
8 years ago
by
Georg Seibt
Browse files
Options
Downloads
Patches
Plain Diff
clone git repositories
parent
b4baeab5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
+101
-0
101 additions, 0 deletions
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java
+22
-0
22 additions, 0 deletions
src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java
with
123 additions
and
0 deletions
src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java
0 → 100644
+
101
−
0
View file @
33e62773
package
de.uni_passau.fim.seibt.gitwrapper.repo
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Optional
;
import
java.util.Scanner
;
import
java.util.function.Function
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.apache.commons.io.IOUtils
;
public
class
Git
{
public
static
final
int
EXIT_SUCCESS
=
0
;
public
static
final
int
EXIT_FAIL
=
1
;
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
Git
.
class
.
getCanonicalName
());
private
static
final
Pattern
CLONING_INTO
=
Pattern
.
compile
(
"Cloning into '(.*)'\\.\\.\\."
);
private
class
ExecRes
{
private
int
code
;
private
String
output
;
}
private
String
git
;
public
Git
(
String
git
)
{
this
.
git
=
git
;
}
public
Optional
<
Repository
>
clone
(
File
parentDir
,
String
url
)
{
LOG
.
fine
(()
->
String
.
format
(
"Cloning '%s'."
,
url
));
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
())
{
LOG
.
warning
(()
->
String
.
format
(
"No output while cloning '%s'."
,
url
));
return
null
;
}
Matcher
matcher
=
CLONING_INTO
.
matcher
(
sc
.
nextLine
());
if
(!
matcher
.
find
())
{
LOG
.
warning
(()
->
String
.
format
(
"Unexpected output while cloning '%s'"
,
url
));
return
null
;
}
String
name
=
matcher
.
group
(
1
);
File
repoDir
=
new
File
(
parentDir
,
name
);
LOG
.
fine
(()
->
"Cloned "
+
url
+
" into "
+
repoDir
.
getAbsolutePath
());
return
new
Repository
(
this
,
url
,
repoDir
);
};
return
res
.
map
(
toRepo
);
}
public
Optional
<
ExecRes
>
exec
(
File
workingDirectory
,
String
...
parameters
)
{
ProcessBuilder
b
=
new
ProcessBuilder
();
String
[]
cmdArray
=
new
String
[
parameters
.
length
+
1
];
cmdArray
[
0
]
=
git
;
System
.
arraycopy
(
parameters
,
0
,
cmdArray
,
1
,
parameters
.
length
);
b
.
command
(
cmdArray
);
b
.
directory
(
workingDirectory
);
b
.
redirectErrorStream
(
true
);
String
cmd
=
String
.
join
(
" "
,
b
.
command
());
LOG
.
fine
(()
->
String
.
format
(
"Executing '%s'."
,
cmd
));
try
{
ExecRes
res
=
new
ExecRes
();
Process
p
=
b
.
start
();
res
.
output
=
IOUtils
.
toString
(
p
.
getInputStream
());
res
.
code
=
p
.
waitFor
();
LOG
.
fine
(()
->
String
.
format
(
"Execution of '%s' returned exit code %d."
,
cmd
,
res
.
code
));
return
Optional
.
of
(
res
);
}
catch
(
IOException
|
InterruptedException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
e
,
()
->
String
.
format
(
"Exception executing '%s'."
,
cmd
));
return
Optional
.
empty
();
}
}
}
This diff is collapsed.
Click to expand it.
src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java
0 → 100644
+
22
−
0
View file @
33e62773
package
de.uni_passau.fim.seibt.gitwrapper.repo
;
import
java.io.File
;
public
class
Repository
{
private
Git
git
;
private
String
url
;
private
File
dir
;
Repository
(
Git
git
,
String
url
,
File
dir
)
{
this
.
git
=
git
;
this
.
url
=
url
;
this
.
dir
=
dir
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"Repository{url=%s, dir=%s}"
,
url
,
dir
);
}
}
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