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
bf4ef0c5
Commit
bf4ef0c5
authored
Apr 16, 2012
by
Carlos Martín Nieto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: run fetch in a background thread
This allows us to give updates on how it's doing
parent
dee5515a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
13 deletions
+56
-13
examples/network/fetch.c
+56
-13
No files found.
examples/network/fetch.c
View file @
bf4ef0c5
...
...
@@ -3,33 +3,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
struct
dl_data
{
git_remote
*
remote
;
git_off_t
*
bytes
;
git_indexer_stats
*
stats
;
int
ret
;
int
finished
;
};
static
void
*
download
(
void
*
ptr
)
{
struct
dl_data
*
data
=
(
struct
dl_data
*
)
ptr
;
// Connect to the remote end specifying that we want to fetch
// information from it.
if
(
git_remote_connect
(
data
->
remote
,
GIT_DIR_FETCH
)
<
0
)
{
data
->
ret
=
-
1
;
goto
exit
;
}
// Download the packfile and index it. This function updates the
// amount of received data and the indexer stats which lets you
// inform the user about progress.
if
(
git_remote_download
(
data
->
remote
,
data
->
bytes
,
data
->
stats
)
<
0
)
{
data
->
ret
=
-
1
;
goto
exit
;
}
data
->
ret
=
0
;
exit:
data
->
finished
=
1
;
pthread_exit
(
&
data
->
ret
);
}
int
fetch
(
git_repository
*
repo
,
int
argc
,
char
**
argv
)
{
git_remote
*
remote
=
NULL
;
git_off_t
bytes
=
0
;
git_indexer_stats
stats
;
char
*
packname
=
NULL
;
pthread_t
worker
;
struct
dl_data
data
;
//
Get the remote and connect to it
//
Figure out whether it's a named remote or a URL
printf
(
"Fetching %s
\n
"
,
argv
[
1
]);
if
(
git_remote_load
(
&
remote
,
repo
,
argv
[
1
])
==
GIT_ENOTFOUND
)
{
if
(
git_remote_load
(
&
remote
,
repo
,
argv
[
1
])
<
0
)
{
if
(
git_remote_new
(
&
remote
,
repo
,
argv
[
1
],
NULL
)
<
0
)
return
-
1
;
}
if
(
git_remote_connect
(
remote
,
GIT_DIR_FETCH
)
<
0
)
return
-
1
;
// Set up the information for the background worker thread
data
.
remote
=
remote
;
data
.
bytes
=
&
bytes
;
data
.
stats
=
&
stats
;
data
.
ret
=
0
;
data
.
finished
=
0
;
memset
(
&
stats
,
0
,
sizeof
(
stats
));
// Download the packfile and index it
// Doing this in a background thread and printing out what bytes
// and stats.{processed,total} say would make the UI friendlier
if
(
git_remote_download
(
remote
,
&
bytes
,
&
stats
)
<
0
)
{
git_remote_free
(
remote
);
return
-
1
;
}
pthread_create
(
&
worker
,
NULL
,
download
,
&
data
);
printf
(
"Received %d objects in %d bytes
\n
"
,
stats
.
total
,
bytes
);
// Loop while the worker thread is still running. Here we show processed
// and total objects in the pack and the amount of received
// data. Most frontends will probably want to show a percentage and
// the download rate.
do
{
usleep
(
10000
);
printf
(
"
\r
Received %d/%d objects in %d bytes"
,
stats
.
processed
,
stats
.
total
,
bytes
);
}
while
(
!
data
.
finished
);
printf
(
"
\r
Received %d/%d objects in %d bytes
\n
"
,
stats
.
processed
,
stats
.
total
,
bytes
);
// Update the references in the remote's namespace to point to the
// right commits. This may be needed even if there was no packfile
...
...
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