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
274f2c21
Commit
274f2c21
authored
May 19, 2011
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #193 from carlosmn/config
A couple of config improvements
parents
44d16d6f
6421c49a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
22 deletions
+83
-22
include/git2/config.h
+14
-0
src/config.c
+34
-0
src/config_file.c
+35
-22
No files found.
include/git2/config.h
View file @
274f2c21
...
...
@@ -51,7 +51,21 @@ GIT_EXTERN(int) git_config_new(git_config **out);
GIT_EXTERN
(
int
)
git_config_open_bare
(
git_config
**
cfg_out
,
const
char
*
path
);
/**
* Open the global configuration file at $HOME/.gitconfig
*
* @param cfg pointer to the configuration
*/
GIT_EXTERN
(
int
)
git_config_open_global
(
git_config
**
cfg
);
/**
* Add a config backend to an existing instance
*
* Note that the configuration will call the backend's ->free()
* function.
*
* @param cfg the configuration to add the backend to
* @param backend the backend to add
* @param priority the priority the backend should have
*/
GIT_EXTERN
(
int
)
git_config_add_backend
(
git_config
*
cfg
,
git_config_backend
*
backend
,
int
priority
);
...
...
src/config.c
View file @
274f2c21
...
...
@@ -70,6 +70,40 @@ int git_config_open_bare(git_config **out, const char *path)
return
error
;
}
int
git_config_open_global
(
git_config
**
out
)
{
char
*
home
=
NULL
,
*
filename
=
NULL
;
const
char
*
gitconfig
=
".gitconfig"
;
int
filename_len
,
ret
,
error
;
home
=
git__strdup
(
getenv
(
"HOME"
));
if
(
home
==
NULL
)
return
GIT_ENOMEM
;
filename_len
=
strlen
(
home
)
+
strlen
(
gitconfig
)
+
1
;
filename
=
git__malloc
(
filename_len
+
1
);
if
(
filename
==
NULL
)
{
error
=
GIT_ENOMEM
;
goto
out
;
}
ret
=
snprintf
(
filename
,
filename_len
,
"%s/%s"
,
home
,
gitconfig
);
if
(
ret
<
0
)
{
error
=
git__throw
(
GIT_EOSERR
,
"Failed to build global filename. OS err: %s"
,
strerror
(
errno
));
goto
out
;
}
else
if
(
ret
>=
filename_len
)
{
error
=
git__throw
(
GIT_ERROR
,
"Failed to build global filename. Length calulation wrong"
);
goto
out
;
}
error
=
git_config_open_bare
(
out
,
filename
);
out:
free
(
home
);
free
(
filename
);
return
error
;
}
void
git_config_free
(
git_config
*
cfg
)
{
unsigned
int
i
;
...
...
src/config_file.c
View file @
274f2c21
...
...
@@ -197,36 +197,49 @@ static cvar_t *cvar_list_find(cvar_t_list *list, const char *name)
return
NULL
;
}
static
int
cvar_n
ame_normalize
(
const
char
*
input
,
char
**
output
)
static
int
cvar_n
ormalize_name
(
cvar_t
*
var
,
char
**
output
)
{
char
*
input_sp
=
strchr
(
input
,
' '
);
char
*
quote
,
*
str
;
int
i
;
char
*
section_sp
=
strchr
(
var
->
section
,
' '
);
char
*
quote
,
*
name
;
int
len
,
ret
;
/* We need to make a copy anyway */
str
=
git__strdup
(
input
);
if
(
str
==
NULL
)
/*
* The final string is going to be at most one char longer than
* the input
*/
len
=
strlen
(
var
->
section
)
+
strlen
(
var
->
name
)
+
1
;
name
=
git__malloc
(
len
+
1
);
if
(
name
==
NULL
)
return
GIT_ENOMEM
;
*
output
=
str
;
/* If there aren't any spaces in the section, it's easy */
if
(
section_sp
==
NULL
)
{
ret
=
snprintf
(
name
,
len
+
1
,
"%s.%s"
,
var
->
section
,
var
->
name
);
if
(
ret
<
0
)
return
git__throw
(
GIT_EOSERR
,
"Failed to normalize name. OS err: %s"
,
strerror
(
errno
));
/* If there aren't any spaces, we don't need to do anything */
if
(
input_sp
==
NULL
)
*
output
=
name
;
return
GIT_SUCCESS
;
}
/*
* If there are spaces, we replace the space by a dot, move
the
*
variable name so that the dot before it replaces the last
*
quotation mark and repeat so that the first quotation mark
*
disappears
.
* If there are spaces, we replace the space by a dot, move
*
section name so it overwrites the first quotation mark and
*
replace the last quotation mark by a dot. We then append the
*
variable name
.
*/
str
[
input_sp
-
input
]
=
'.'
;
for
(
i
=
0
;
i
<
2
;
++
i
)
{
quote
=
strrchr
(
str
,
'"'
);
memmove
(
quote
,
quote
+
1
,
strlen
(
quote
));
}
strcpy
(
name
,
var
->
section
);
section_sp
=
strchr
(
name
,
' '
);
*
section_sp
=
'.'
;
/* Remove first quote */
quote
=
strchr
(
name
,
'"'
);
memmove
(
quote
,
quote
+
1
,
strlen
(
quote
+
1
));
/* Remove second quote */
quote
=
strchr
(
name
,
'"'
);
*
quote
=
'.'
;
strcpy
(
quote
+
1
,
var
->
name
);
*
output
=
name
;
return
GIT_SUCCESS
;
}
...
...
@@ -276,7 +289,7 @@ static int file_foreach(git_config_backend *backend, int (*fn)(const char *, voi
file_backend
*
b
=
(
file_backend
*
)
backend
;
CVAR_LIST_FOREACH
(
&
b
->
var_list
,
var
)
{
ret
=
cvar_n
ame_normalize
(
var
->
name
,
&
normalized
);
ret
=
cvar_n
ormalize_name
(
var
,
&
normalized
);
if
(
ret
<
GIT_SUCCESS
)
return
ret
;
...
...
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