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
481f5e27
Commit
481f5e27
authored
Feb 20, 2012
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #568 from carlosmn/remotes
Remotes improvements
parents
555c81f3
f0f3a18a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
172 additions
and
22 deletions
+172
-22
include/git2/remote.h
+26
-0
src/remote.c
+104
-22
tests-clar/network/remotes.c
+42
-0
No files found.
include/git2/remote.h
View file @
481f5e27
...
...
@@ -54,6 +54,14 @@ GIT_EXTERN(int) git_remote_new(git_remote **out, git_repository *repo, const cha
GIT_EXTERN
(
int
)
git_remote_load
(
git_remote
**
out
,
git_repository
*
repo
,
const
char
*
name
);
/**
* Save a remote to its repository's configuration
*
* @param remote the remote to save to config
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN
(
int
)
git_remote_save
(
const
git_remote
*
remote
);
/**
* Get the remote's name
*
* @param remote the remote
...
...
@@ -70,6 +78,15 @@ GIT_EXTERN(const char *) git_remote_name(git_remote *remote);
GIT_EXTERN
(
const
char
*
)
git_remote_url
(
git_remote
*
remote
);
/**
* Set the remote's fetch refspec
*
* @param remote the remote
* @apram spec the new fetch refspec
* @return GIT_SUCCESS or an error value
*/
GIT_EXTERN
(
int
)
git_remote_set_fetchspec
(
git_remote
*
remote
,
const
char
*
spec
);
/**
* Get the fetch refspec
*
* @param remote the remote
...
...
@@ -78,6 +95,15 @@ GIT_EXTERN(const char *) git_remote_url(git_remote *remote);
GIT_EXTERN
(
const
git_refspec
*
)
git_remote_fetchspec
(
git_remote
*
remote
);
/**
* Set the remote's push refspec
*
* @param remote the remote
* @apram spec the new push refspec
* @return GIT_SUCCESS or an error value
*/
GIT_EXTERN
(
int
)
git_remote_set_pushspec
(
git_remote
*
remote
,
const
char
*
spec
);
/**
* Get the push refspec
*
* @param remote the remote
...
...
src/remote.c
View file @
481f5e27
...
...
@@ -96,9 +96,9 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *url, cons
int
git_remote_load
(
git_remote
**
out
,
git_repository
*
repo
,
const
char
*
name
)
{
git_remote
*
remote
;
char
*
buf
=
NULL
;
git_buf
buf
=
GIT_BUF_INIT
;
const
char
*
val
;
int
ret
,
error
,
buf_len
;
int
error
;
git_config
*
config
;
assert
(
out
&&
repo
&&
name
);
...
...
@@ -123,21 +123,13 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
goto
cleanup
;
}
/* "fetch" is the longest var name we're interested in */
buf_len
=
strlen
(
"remote."
)
+
strlen
(
".fetch"
)
+
strlen
(
name
)
+
1
;
buf
=
git__malloc
(
buf_len
);
if
(
buf
==
NULL
)
{
git_buf_printf
(
&
buf
,
"remote.%s.url"
,
name
);
if
(
git_buf_oom
(
&
buf
))
{
error
=
GIT_ENOMEM
;
goto
cleanup
;
}
ret
=
p_snprintf
(
buf
,
buf_len
,
"%s.%s.%s"
,
"remote"
,
name
,
"url"
);
if
(
ret
<
0
)
{
error
=
git__throw
(
GIT_EOSERR
,
"Failed to build config var name"
);
goto
cleanup
;
}
error
=
git_config_get_string
(
config
,
buf
,
&
val
);
error
=
git_config_get_string
(
config
,
git_buf_cstr
(
&
buf
),
&
val
);
if
(
error
<
GIT_SUCCESS
)
{
error
=
git__rethrow
(
error
,
"Remote's url doesn't exist"
);
goto
cleanup
;
...
...
@@ -150,25 +142,27 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
goto
cleanup
;
}
ret
=
p_snprintf
(
buf
,
buf_len
,
"%s.%s.%s"
,
"remote"
,
name
,
"fetch"
);
if
(
ret
<
0
)
{
error
=
git__throw
(
GIT_EOSERR
,
"Failed to build config var name"
);
git_buf_clear
(
&
buf
);
git_buf_printf
(
&
buf
,
"remote.%s.fetch"
,
name
);
if
(
git_buf_oom
(
&
buf
))
{
error
=
GIT_ENOMEM
;
goto
cleanup
;
}
error
=
parse_remote_refspec
(
config
,
&
remote
->
fetch
,
buf
);
error
=
parse_remote_refspec
(
config
,
&
remote
->
fetch
,
git_buf_cstr
(
&
buf
)
);
if
(
error
<
GIT_SUCCESS
)
{
error
=
git__rethrow
(
error
,
"Failed to get fetch refspec"
);
goto
cleanup
;
}
ret
=
p_snprintf
(
buf
,
buf_len
,
"%s.%s.%s"
,
"remote"
,
name
,
"push"
);
if
(
ret
<
0
)
{
error
=
git__throw
(
GIT_EOSERR
,
"Failed to build config var name"
);
git_buf_clear
(
&
buf
);
git_buf_printf
(
&
buf
,
"remote.%s.push"
,
name
);
if
(
git_buf_oom
(
&
buf
))
{
error
=
GIT_ENOMEM
;
goto
cleanup
;
}
error
=
parse_remote_refspec
(
config
,
&
remote
->
push
,
buf
);
error
=
parse_remote_refspec
(
config
,
&
remote
->
push
,
git_buf_cstr
(
&
buf
)
);
/* Not finding push is fine */
if
(
error
==
GIT_ENOTFOUND
)
error
=
GIT_SUCCESS
;
...
...
@@ -179,7 +173,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
*
out
=
remote
;
cleanup:
git_
_free
(
buf
);
git_
buf_free
(
&
buf
);
if
(
error
<
GIT_SUCCESS
)
git_remote_free
(
remote
);
...
...
@@ -187,6 +181,56 @@ cleanup:
return
error
;
}
int
git_remote_save
(
const
git_remote
*
remote
)
{
int
error
;
git_config
*
config
;
git_buf
buf
=
GIT_BUF_INIT
,
value
=
GIT_BUF_INIT
;
error
=
git_repository_config__weakptr
(
&
config
,
remote
->
repo
);
if
(
error
<
GIT_SUCCESS
)
return
error
;
git_buf_printf
(
&
buf
,
"remote.%s.%s"
,
remote
->
name
,
"url"
);
if
(
git_buf_oom
(
&
buf
))
return
GIT_ENOMEM
;
error
=
git_config_set_string
(
config
,
git_buf_cstr
(
&
buf
),
remote
->
url
);
if
(
error
<
GIT_SUCCESS
)
goto
cleanup
;
if
(
remote
->
fetch
.
src
!=
NULL
&&
remote
->
fetch
.
src
!=
NULL
)
{
git_buf_clear
(
&
buf
);
git_buf_clear
(
&
value
);
git_buf_printf
(
&
buf
,
"remote.%s.%s"
,
remote
->
name
,
"fetch"
);
git_buf_printf
(
&
value
,
"%s:%s"
,
remote
->
fetch
.
src
,
remote
->
fetch
.
dst
);
if
(
git_buf_oom
(
&
buf
)
||
git_buf_oom
(
&
value
))
return
GIT_ENOMEM
;
error
=
git_config_set_string
(
config
,
git_buf_cstr
(
&
buf
),
git_buf_cstr
(
&
value
));
if
(
error
<
GIT_SUCCESS
)
goto
cleanup
;
}
if
(
remote
->
push
.
src
!=
NULL
&&
remote
->
push
.
src
!=
NULL
)
{
git_buf_clear
(
&
buf
);
git_buf_clear
(
&
value
);
git_buf_printf
(
&
buf
,
"remote.%s.%s"
,
remote
->
name
,
"push"
);
git_buf_printf
(
&
value
,
"%s:%s"
,
remote
->
push
.
src
,
remote
->
push
.
dst
);
if
(
git_buf_oom
(
&
buf
)
||
git_buf_oom
(
&
value
))
return
GIT_ENOMEM
;
error
=
git_config_set_string
(
config
,
git_buf_cstr
(
&
buf
),
git_buf_cstr
(
&
value
));
if
(
error
<
GIT_SUCCESS
)
goto
cleanup
;
}
cleanup:
git_buf_free
(
&
buf
);
git_buf_free
(
&
value
);
return
error
;
}
const
char
*
git_remote_name
(
git_remote
*
remote
)
{
assert
(
remote
);
...
...
@@ -199,12 +243,50 @@ const char *git_remote_url(git_remote *remote)
return
remote
->
url
;
}
int
git_remote_set_fetchspec
(
git_remote
*
remote
,
const
char
*
spec
)
{
int
error
;
git_refspec
refspec
;
assert
(
remote
&&
spec
);
error
=
refspec_parse
(
&
refspec
,
spec
);
if
(
error
!=
GIT_SUCCESS
)
return
error
;
git__free
(
remote
->
fetch
.
src
);
git__free
(
remote
->
fetch
.
dst
);
remote
->
fetch
.
src
=
refspec
.
src
;
remote
->
fetch
.
dst
=
refspec
.
dst
;
return
GIT_SUCCESS
;
}
const
git_refspec
*
git_remote_fetchspec
(
git_remote
*
remote
)
{
assert
(
remote
);
return
&
remote
->
fetch
;
}
int
git_remote_set_pushspec
(
git_remote
*
remote
,
const
char
*
spec
)
{
int
error
;
git_refspec
refspec
;
assert
(
remote
&&
spec
);
error
=
refspec_parse
(
&
refspec
,
spec
);
if
(
error
!=
GIT_SUCCESS
)
return
error
;
git__free
(
remote
->
push
.
src
);
git__free
(
remote
->
push
.
dst
);
remote
->
push
.
src
=
refspec
.
src
;
remote
->
push
.
dst
=
refspec
.
dst
;
return
GIT_SUCCESS
;
}
const
git_refspec
*
git_remote_pushspec
(
git_remote
*
remote
)
{
assert
(
remote
);
...
...
tests-clar/network/remotes.c
View file @
481f5e27
...
...
@@ -36,6 +36,48 @@ void test_network_remotes__refspec_parsing(void)
cl_assert
(
!
strcmp
(
git_refspec_dst
(
_refspec
),
"refs/remotes/test/*"
));
}
void
test_network_remotes__set_fetchspec
(
void
)
{
cl_git_pass
(
git_remote_set_fetchspec
(
_remote
,
"refs/*:refs/*"
));
_refspec
=
git_remote_fetchspec
(
_remote
);
cl_assert
(
!
strcmp
(
git_refspec_src
(
_refspec
),
"refs/*"
));
cl_assert
(
!
strcmp
(
git_refspec_dst
(
_refspec
),
"refs/*"
));
}
void
test_network_remotes__set_pushspec
(
void
)
{
cl_git_pass
(
git_remote_set_pushspec
(
_remote
,
"refs/*:refs/*"
));
_refspec
=
git_remote_pushspec
(
_remote
);
cl_assert
(
!
strcmp
(
git_refspec_src
(
_refspec
),
"refs/*"
));
cl_assert
(
!
strcmp
(
git_refspec_dst
(
_refspec
),
"refs/*"
));
}
void
test_network_remotes__save
(
void
)
{
git_remote_free
(
_remote
);
/* Set up the remote and save it to config */
cl_git_pass
(
git_remote_new
(
&
_remote
,
_repo
,
"git://github.com/libgit2/libgit2"
,
"upstream"
));
cl_git_pass
(
git_remote_set_fetchspec
(
_remote
,
"refs/heads/*:refs/remotes/upstream/*"
));
cl_git_pass
(
git_remote_set_pushspec
(
_remote
,
"refs/heads/*:refs/heads/*"
));
cl_git_pass
(
git_remote_save
(
_remote
));
git_remote_free
(
_remote
);
_remote
=
NULL
;
/* Load it from config and make sure everything matches */
cl_git_pass
(
git_remote_load
(
&
_remote
,
_repo
,
"upstream"
));
_refspec
=
git_remote_fetchspec
(
_remote
);
cl_assert
(
_refspec
!=
NULL
);
cl_assert
(
!
strcmp
(
git_refspec_src
(
_refspec
),
"refs/heads/*"
));
cl_assert
(
!
strcmp
(
git_refspec_dst
(
_refspec
),
"refs/remotes/upstream/*"
));
_refspec
=
git_remote_pushspec
(
_remote
);
cl_assert
(
_refspec
!=
NULL
);
cl_assert
(
!
strcmp
(
git_refspec_src
(
_refspec
),
"refs/heads/*"
));
cl_assert
(
!
strcmp
(
git_refspec_dst
(
_refspec
),
"refs/heads/*"
));
}
void
test_network_remotes__fnmatch
(
void
)
{
cl_git_pass
(
git_refspec_src_match
(
_refspec
,
"refs/heads/master"
));
...
...
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