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
7fcec834
Commit
7fcec834
authored
Dec 11, 2012
by
Edward Thomson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fetchhead reading/iterating
parent
5c3c86b0
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
469 additions
and
44 deletions
+469
-44
include/git2/errors.h
+1
-0
include/git2/repository.h
+18
-0
src/fetchhead.c
+175
-5
src/fetchhead.h
+11
-4
src/util.c
+18
-0
src/util.h
+1
-0
tests-clar/fetchhead/nonetwork.c
+245
-35
No files found.
include/git2/errors.h
View file @
7fcec834
...
...
@@ -63,6 +63,7 @@ typedef enum {
GITERR_THREAD
,
GITERR_STASH
,
GITERR_CHECKOUT
,
GITERR_FETCHHEAD
,
}
git_error_t
;
/**
...
...
include/git2/repository.h
View file @
7fcec834
...
...
@@ -490,6 +490,24 @@ GIT_EXTERN(int) git_repository_message(char *out, size_t len, git_repository *re
*/
GIT_EXTERN
(
int
)
git_repository_message_remove
(
git_repository
*
repo
);
typedef
int
(
*
git_repository_fetchhead_foreach_cb
)(
const
char
*
ref_name
,
const
char
*
remote_url
,
const
git_oid
*
oid
,
unsigned
int
is_merge
,
void
*
payload
);
/**
* Call callback 'callback' for each entry in the given FETCH_HEAD file.
*
* @param repo A repository object
* @param callback Callback function
* @param payload Pointer to callback data (optional)
* @return 0 on success, GIT_ENOTFOUND, GIT_EUSER or error
*/
GIT_EXTERN
(
int
)
git_repository_fetchhead_foreach
(
git_repository
*
repo
,
git_repository_fetchhead_foreach_cb
callback
,
void
*
payload
);
/**
* Calculate hash of file using repository filtering rules.
*
...
...
src/fetchhead.c
View file @
7fcec834
...
...
@@ -10,6 +10,7 @@
#include "fetchhead.h"
#include "common.h"
#include "buffer.h"
#include "fileops.h"
#include "filebuf.h"
#include "refs.h"
...
...
@@ -26,19 +27,28 @@ int git_fetchhead_ref_cmp(const void *a, const void *b)
if
(
two
->
is_merge
&&
!
one
->
is_merge
)
return
1
;
if
(
one
->
ref_name
&&
two
->
ref_name
)
return
strcmp
(
one
->
ref_name
,
two
->
ref_name
);
else
if
(
one
->
ref_name
)
return
-
1
;
else
if
(
two
->
ref_name
)
return
1
;
return
0
;
}
int
git_fetchhead_ref_create
(
git_fetchhead_ref
**
fetchhead_ref_
out
,
git_fetchhead_ref
**
out
,
git_oid
*
oid
,
int
is_merge
,
unsigned
int
is_merge
,
const
char
*
ref_name
,
const
char
*
remote_url
)
{
git_fetchhead_ref
*
fetchhead_ref
=
NULL
;
git_fetchhead_ref
*
fetchhead_ref
;
assert
(
fetchhead_ref_out
&&
oid
&&
ref_name
&&
remote_url
);
assert
(
out
&&
oid
);
*
out
=
NULL
;
fetchhead_ref
=
git__malloc
(
sizeof
(
git_fetchhead_ref
));
GITERR_CHECK_ALLOC
(
fetchhead_ref
);
...
...
@@ -47,10 +57,14 @@ int git_fetchhead_ref_create(
git_oid_cpy
(
&
fetchhead_ref
->
oid
,
oid
);
fetchhead_ref
->
is_merge
=
is_merge
;
if
(
ref_name
)
fetchhead_ref
->
ref_name
=
git__strdup
(
ref_name
);
if
(
remote_url
)
fetchhead_ref
->
remote_url
=
git__strdup
(
remote_url
);
*
fetchhead_ref_
out
=
fetchhead_ref
;
*
out
=
fetchhead_ref
;
return
0
;
}
...
...
@@ -114,6 +128,162 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
return
git_filebuf_commit
(
&
file
,
GIT_REFS_FILE_MODE
);
}
static
int
fetchhead_ref_parse
(
git_oid
*
oid
,
unsigned
int
*
is_merge
,
git_buf
*
ref_name
,
const
char
**
remote_url
,
char
*
line
,
size_t
line_num
)
{
char
*
oid_str
,
*
is_merge_str
,
*
desc
,
*
name
=
NULL
;
const
char
*
type
=
NULL
;
int
error
=
0
;
*
remote_url
=
NULL
;
if
(
!*
line
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"Empty line in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
/* Compat with old git clients that wrote FETCH_HEAD like a loose ref. */
if
((
oid_str
=
git__strsep
(
&
line
,
"
\t
"
))
==
NULL
)
{
oid_str
=
line
;
line
+=
strlen
(
line
);
*
is_merge
=
1
;
}
if
(
strlen
(
oid_str
)
!=
GIT_OID_HEXSZ
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"Invalid object ID in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
if
(
git_oid_fromstr
(
oid
,
oid_str
)
<
0
)
{
const
git_error
*
oid_err
=
giterr_last
();
const
char
*
err_msg
=
oid_err
?
oid_err
->
message
:
"Invalid object ID"
;
giterr_set
(
GITERR_FETCHHEAD
,
"%s in FETCH_HEAD line %d"
,
err_msg
,
line_num
);
return
-
1
;
}
/* Parse new data from newer git clients */
if
(
*
line
)
{
if
((
is_merge_str
=
git__strsep
(
&
line
,
"
\t
"
))
==
NULL
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"Invalid description data in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
if
(
*
is_merge_str
==
'\0'
)
*
is_merge
=
1
;
else
if
(
strcmp
(
is_merge_str
,
"not-for-merge"
)
==
0
)
*
is_merge
=
0
;
else
{
giterr_set
(
GITERR_FETCHHEAD
,
"Invalid for-merge entry in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
if
((
desc
=
line
)
==
NULL
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"Invalid description in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
if
(
git__prefixcmp
(
desc
,
"branch '"
)
==
0
)
{
type
=
GIT_REFS_HEADS_DIR
;
name
=
desc
+
8
;
}
else
if
(
git__prefixcmp
(
desc
,
"tag '"
)
==
0
)
{
type
=
GIT_REFS_TAGS_DIR
;
name
=
desc
+
5
;
}
else
if
(
git__prefixcmp
(
desc
,
"'"
)
==
0
)
name
=
desc
+
1
;
if
(
name
)
{
if
((
desc
=
strchr
(
name
,
'\''
))
==
NULL
||
git__prefixcmp
(
desc
,
"' of "
)
!=
0
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"Invalid description in FETCH_HEAD line %d"
,
line_num
);
return
-
1
;
}
*
desc
=
'\0'
;
desc
+=
5
;
}
*
remote_url
=
desc
;
}
git_buf_clear
(
ref_name
);
if
(
type
)
git_buf_join
(
ref_name
,
'/'
,
type
,
name
);
else
if
(
name
)
git_buf_puts
(
ref_name
,
name
);
return
error
;
}
int
git_repository_fetchhead_foreach
(
git_repository
*
repo
,
git_repository_fetchhead_foreach_cb
cb
,
void
*
payload
)
{
git_buf
path
=
GIT_BUF_INIT
,
file
=
GIT_BUF_INIT
,
name
=
GIT_BUF_INIT
;
const
char
*
ref_name
;
git_oid
oid
;
const
char
*
remote_url
;
unsigned
int
is_merge
;
char
*
buffer
,
*
line
;
size_t
line_num
=
0
;
int
error
=
0
;
assert
(
repo
&&
cb
);
if
(
git_buf_joinpath
(
&
path
,
repo
->
path_repository
,
GIT_FETCH_HEAD_FILE
)
<
0
)
return
-
1
;
if
((
error
=
git_futils_readbuffer
(
&
file
,
git_buf_cstr
(
&
path
)))
<
0
)
goto
done
;
buffer
=
file
.
ptr
;
while
((
line
=
git__strsep
(
&
buffer
,
"
\n
"
))
!=
NULL
)
{
++
line_num
;
if
((
error
=
fetchhead_ref_parse
(
&
oid
,
&
is_merge
,
&
name
,
&
remote_url
,
line
,
line_num
))
<
0
)
goto
done
;
if
(
git_buf_len
(
&
name
)
>
0
)
ref_name
=
git_buf_cstr
(
&
name
);
else
ref_name
=
NULL
;
if
((
cb
(
ref_name
,
remote_url
,
&
oid
,
is_merge
,
payload
))
!=
0
)
{
error
=
GIT_EUSER
;
goto
done
;
}
}
if
(
*
buffer
)
{
giterr_set
(
GITERR_FETCHHEAD
,
"No EOL at line %d"
,
line_num
+
1
);
error
=
-
1
;
goto
done
;
}
done:
git_buf_free
(
&
file
);
git_buf_free
(
&
path
);
git_buf_free
(
&
name
);
return
error
;
}
void
git_fetchhead_ref_free
(
git_fetchhead_ref
*
fetchhead_ref
)
{
if
(
fetchhead_ref
==
NULL
)
...
...
src/fetchhead.h
View file @
7fcec834
...
...
@@ -9,18 +9,25 @@
#include "vector.h"
typedef
struct
git_fetchhead_ref
{
struct
git_fetchhead_ref
{
git_oid
oid
;
unsigned
int
is_merge
;
char
*
ref_name
;
char
*
remote_url
;
}
git_fetchhead_ref
;
};
int
git_fetchhead_ref_create
(
git_fetchhead_ref
**
fetchhead_ref_out
,
git_oid
*
oid
,
int
is_merge
,
const
char
*
ref_name
,
const
char
*
remote_url
);
typedef
struct
git_fetchhead_ref
git_fetchhead_ref
;
int
git_fetchhead_ref_create
(
git_fetchhead_ref
**
fetchhead_ref_out
,
git_oid
*
oid
,
unsigned
int
is_merge
,
const
char
*
ref_name
,
const
char
*
remote_url
);
int
git_fetchhead_ref_cmp
(
const
void
*
a
,
const
void
*
b
);
int
git_fetchhead_write
(
git_repository
*
repo
sitory
,
git_vector
*
fetchhead_refs
);
int
git_fetchhead_write
(
git_repository
*
repo
,
git_vector
*
fetchhead_refs
);
void
git_fetchhead_ref_free
(
git_fetchhead_ref
*
fetchhead_ref
);
...
...
src/util.c
View file @
7fcec834
...
...
@@ -276,6 +276,24 @@ char *git__strtok(char **end, const char *sep)
return
NULL
;
}
/* Similar to strtok, but does not collapse repeated tokens. */
char
*
git__strsep
(
char
**
end
,
const
char
*
sep
)
{
char
*
start
=
*
end
,
*
ptr
=
*
end
;
while
(
*
ptr
&&
!
strchr
(
sep
,
*
ptr
))
++
ptr
;
if
(
*
ptr
)
{
*
end
=
ptr
+
1
;
*
ptr
=
'\0'
;
return
start
;
}
return
NULL
;
}
void
git__hexdump
(
const
char
*
buffer
,
size_t
len
)
{
static
const
size_t
LINE_WIDTH
=
16
;
...
...
src/util.h
View file @
7fcec834
...
...
@@ -107,6 +107,7 @@ GIT_INLINE(int) git__is_sizet(git_off_t p)
#endif
extern
char
*
git__strtok
(
char
**
end
,
const
char
*
sep
);
extern
char
*
git__strsep
(
char
**
end
,
const
char
*
sep
);
extern
void
git__strntolower
(
char
*
str
,
size_t
len
);
extern
void
git__strtolower
(
char
*
str
);
...
...
tests-clar/fetchhead/nonetwork.c
View file @
7fcec834
This diff is collapsed.
Click to expand it.
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