Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
bb1f6087
Commit
bb1f6087
authored
Jun 19, 2012
by
Ben Straub
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add progress reporting to clone.
parent
764df57e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
54 deletions
+64
-54
include/git2/clone.h
+6
-3
src/clone.c
+48
-43
tests-clar/clone/clone.c
+10
-8
No files found.
include/git2/clone.h
View file @
bb1f6087
...
...
@@ -9,6 +9,7 @@
#include "common.h"
#include "types.h"
#include "indexer.h"
/**
...
...
@@ -25,10 +26,11 @@ GIT_BEGIN_DECL
*
* @param out pointer that will receive the resulting repository object
* @param origin_url repository to clone from
* @param dest_path local directory to clone to
* @param workdir_path local directory to clone to
* @param stats pointer to structure that receives progress information (may be NULL)
* @return 0 on success, GIT_ERROR otherwise (use git_error_last for information about the error)
*/
GIT_EXTERN
(
int
)
git_clone
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
);
GIT_EXTERN
(
int
)
git_clone
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
workdir_path
,
git_indexer_stats
*
stats
);
/**
* TODO
...
...
@@ -36,9 +38,10 @@ GIT_EXTERN(int) git_clone(git_repository **out, const char *origin_url, const ch
* @param out pointer that will receive the resulting repository object
* @param origin_url repository to clone from
* @param dest_path local directory to clone to
* @param stats pointer to structure that receives progress information (may be NULL)
* @return 0 on success, GIT_ERROR otherwise (use git_error_last for information about the error)
*/
GIT_EXTERN
(
int
)
git_clone_bare
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
);
GIT_EXTERN
(
int
)
git_clone_bare
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
,
git_indexer_stats
*
stats
);
/** @} */
GIT_END_DECL
...
...
src/clone.c
View file @
bb1f6087
...
...
@@ -9,6 +9,7 @@
#include "git2/clone.h"
#include "git2/remote.h"
#include "git2/revparse.h"
#include "common.h"
#include "remote.h"
...
...
@@ -17,90 +18,93 @@
GIT_BEGIN_DECL
static
int
git_checkout
(
git_repository
*
repo
,
git_commit
*
commit
,
git_indexer_stats
*
stats
)
{
return
0
;
}
/*
* submodules?
* filemodes?
*/
static
int
setup_remotes_and_fetch
(
git_repository
*
repo
,
const
char
*
origin_url
)
static
int
setup_remotes_and_fetch
(
git_repository
*
repo
,
const
char
*
origin_url
,
git_indexer_stats
*
stats
)
{
int
retcode
=
GIT_ERROR
;
git_remote
*
origin
=
NULL
;
git_off_t
bytes
=
0
;
git_indexer_stats
stats
=
{
0
};
if
(
!
git_remote_new
(
&
origin
,
repo
,
"origin"
,
origin_url
,
NULL
))
{
if
(
!
git_remote_save
(
origin
))
{
if
(
!
git_remote_connect
(
origin
,
GIT_DIR_FETCH
))
{
if
(
!
git_remote_download
(
origin
,
&
bytes
,
&
stats
))
{
if
(
!
git_remote_update_tips
(
origin
,
NULL
))
{
// TODO
// if (!git_checkout(...)) {
retcode
=
0
;
// }
}
git_indexer_stats
dummy_stats
;
if
(
!
stats
)
stats
=
&
dummy_stats
;
if
(
!
git_remote_add
(
&
origin
,
repo
,
"origin"
,
origin_url
))
{
if
(
!
git_remote_connect
(
origin
,
GIT_DIR_FETCH
))
{
if
(
!
git_remote_download
(
origin
,
&
bytes
,
stats
))
{
if
(
!
git_remote_update_tips
(
origin
,
NULL
))
{
retcode
=
0
;
}
git_remote_disconnect
(
origin
);
}
git_remote_disconnect
(
origin
);
}
git_remote_free
(
origin
);
}
}
return
retcode
;
}
int
git_clone
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
)
static
int
clone_internal
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
fullpath
,
git_indexer_stats
*
stats
,
int
is_bare
)
{
int
retcode
=
GIT_ERROR
;
git_repository
*
repo
=
NULL
;
char
fullpath
[
512
]
=
{
0
};
p_realpath
(
dest_path
,
fullpath
);
if
(
git_path_exists
(
fullpath
))
{
giterr_set
(
GITERR_INVALID
,
"Destination already exists: %s"
,
fullpath
);
return
GIT_ERROR
;
}
/* Initialize the dest/.git directory */
if
(
!
(
retcode
=
git_repository_init
(
&
repo
,
fullpath
,
0
)))
{
if
((
retcode
=
setup_remotes_and_fetch
(
repo
,
origin_url
))
<
0
)
{
if
(
!
(
retcode
=
git_repository_init
(
&
repo
,
fullpath
,
is_bare
)))
{
if
((
retcode
=
setup_remotes_and_fetch
(
repo
,
origin_url
,
stats
))
<
0
)
{
/* Failed to fetch; clean up */
git_repository_free
(
repo
);
git_futils_rmdir_r
(
fullpath
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
}
else
{
/* Fetched successfully, do a checkout */
/* if (!(retcode = git_checkout(...))) {} */
*
out
=
repo
;
retcode
=
0
;
}
}
return
retcode
;
}
int
git_clone_bare
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
,
git_indexer_stats
*
stats
)
{
char
fullpath
[
512
]
=
{
0
};
p_realpath
(
dest_path
,
fullpath
);
if
(
git_path_exists
(
fullpath
))
{
giterr_set
(
GITERR_INVALID
,
"Destination already exists: %s"
,
fullpath
);
return
GIT_ERROR
;
}
return
clone_internal
(
out
,
origin_url
,
fullpath
,
stats
,
1
);
}
int
git_clone_bare
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
dest_path
)
int
git_clone
(
git_repository
**
out
,
const
char
*
origin_url
,
const
char
*
workdir_path
,
git_indexer_stats
*
stats
)
{
int
retcode
=
GIT_ERROR
;
git_repository
*
repo
=
NULL
;
char
fullpath
[
512
]
=
{
0
};
p_realpath
(
dest
_path
,
fullpath
);
p_realpath
(
workdir
_path
,
fullpath
);
if
(
git_path_exists
(
fullpath
))
{
giterr_set
(
GITERR_INVALID
,
"Destination already exists: %s"
,
fullpath
);
return
GIT_ERROR
;
}
if
(
!
(
retcode
=
git_repository_init
(
&
repo
,
fullpath
,
1
)))
{
if
((
retcode
=
setup_remotes_and_fetch
(
repo
,
origin_url
))
<
0
)
{
/* Failed to fetch; clean up */
git_repository_free
(
repo
);
git_futils_rmdir_r
(
fullpath
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
}
else
{
/* Fetched successfully, do a checkout */
/* if (!(retcode = git_checkout(...))) {} */
*
out
=
repo
;
retcode
=
0
;
if
(
!
clone_internal
(
out
,
origin_url
,
workdir_path
,
stats
,
0
))
{
git_object
*
commit_to_checkout
=
NULL
;
if
(
!
git_revparse_single
(
&
commit_to_checkout
,
*
out
,
"master"
))
{
if
(
git_object_type
(
commit_to_checkout
)
==
GIT_OBJ_COMMIT
)
{
retcode
=
git_checkout
(
*
out
,
(
git_commit
*
)
commit_to_checkout
,
stats
);
}
}
}
...
...
@@ -109,4 +113,5 @@ int git_clone_bare(git_repository **out, const char *origin_url, const char *des
GIT_END_DECL
tests-clar/clone/clone.c
View file @
bb1f6087
...
...
@@ -28,7 +28,7 @@ static void build_local_file_url(git_buf *out, const char *fixture)
cl_git_pass
(
git_path_prettify_dir
(
&
path_buf
,
fixture
,
NULL
));
cl_git_pass
(
git_buf_puts
(
out
,
"file://"
));
#ifdef
_MSC_VER
#ifdef
GIT_WIN32
/*
* A FILE uri matches the following format: file://[host]/path
* where "host" can be empty and "path" is an absolute path to the resource.
...
...
@@ -62,9 +62,9 @@ static void build_local_file_url(git_buf *out, const char *fixture)
void
test_clone_clone__bad_url
(
void
)
{
/* Clone should clean up the mess if the URL isn't a git repository */
cl_git_fail
(
git_clone
(
&
g_repo
,
"not_a_repo"
,
"./foo"
));
cl_git_fail
(
git_clone
(
&
g_repo
,
"not_a_repo"
,
"./foo"
,
NULL
));
cl_assert
(
!
git_path_exists
(
"./foo"
));
cl_git_fail
(
git_clone_bare
(
&
g_repo
,
"not_a_repo"
,
"./foo.git"
));
cl_git_fail
(
git_clone_bare
(
&
g_repo
,
"not_a_repo"
,
"./foo.git"
,
NULL
));
cl_assert
(
!
git_path_exists
(
"./foo"
));
}
...
...
@@ -74,11 +74,13 @@ void test_clone_clone__local(void)
git_buf
src
=
GIT_BUF_INIT
;
build_local_file_url
(
&
src
,
cl_fixture
(
"testrepo.git"
));
cl_git_pass
(
git_clone
(
&
g_repo
,
git_buf_cstr
(
&
src
),
"./local"
));
cl_git_pass
(
git_clone
(
&
g_repo
,
git_buf_cstr
(
&
src
),
"./local"
,
NULL
));
git_repository_free
(
g_repo
);
git_futils_rmdir_r
(
"./local"
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
cl_git_pass
(
git_clone_bare
(
&
g_repo
,
git_buf_cstr
(
&
src
),
"./local.git"
));
cl_git_pass
(
git_clone_bare
(
&
g_repo
,
git_buf_cstr
(
&
src
),
"./local.git"
,
NULL
));
git_futils_rmdir_r
(
"./local.git"
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
git_buf_free
(
&
src
);
}
...
...
@@ -86,8 +88,8 @@ void test_clone_clone__network(void)
{
cl_git_pass
(
git_clone
(
&
g_repo
,
"https://github.com/libgit2/libgit2.git"
,
"./libgit2
.git"
));
git_futils_rmdir_r
(
"./libgit2
.git
"
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
"./libgit2
"
,
NULL
));
git_futils_rmdir_r
(
"./libgit2"
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
}
...
...
@@ -96,6 +98,6 @@ void test_clone_clone__already_exists(void)
mkdir
(
"./foo"
,
GIT_DIR_MODE
);
cl_git_fail
(
git_clone
(
&
g_repo
,
"https://github.com/libgit2/libgit2.git"
,
"./foo"
));
"./foo"
,
NULL
));
git_futils_rmdir_r
(
"./foo"
,
GIT_DIRREMOVAL_FILES_AND_DIRS
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment