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
5f60fd00
Commit
5f60fd00
authored
May 24, 2012
by
Russell Belfer
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #726 from arrbee/utf16-home-dir
Get user's home dir in UTF-16 clean manner
parents
d3e9c4a5
9e35d7fd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
188 additions
and
16 deletions
+188
-16
src/fileops.c
+42
-3
src/win32/utf-conv.c
+10
-13
tests-clar/core/env.c
+136
-0
No files found.
src/fileops.c
View file @
5f60fd00
...
@@ -353,13 +353,48 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type
...
@@ -353,13 +353,48 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type
return
error
;
return
error
;
}
}
#ifdef GIT_WIN32
static
char
*
win32_getenv
(
const
wchar_t
*
name
)
{
char
*
val_utf8
;
wchar_t
*
val_utf16
;
DWORD
len
=
GetEnvironmentVariableW
(
name
,
NULL
,
0
);
if
(
len
<=
0
)
return
NULL
;
val_utf16
=
git__calloc
(
len
,
sizeof
(
wchar_t
));
if
(
!
val_utf16
)
return
NULL
;
if
(
GetEnvironmentVariableW
(
name
,
val_utf16
,
len
)
!=
len
-
1
)
{
giterr_set
(
GITERR_OS
,
"Could not read environment variable"
);
git__free
(
val_utf16
);
return
NULL
;
}
val_utf8
=
gitwin_from_utf16
(
val_utf16
);
git__free
(
val_utf16
);
return
val_utf8
;
}
#endif
int
git_futils_find_global_file
(
git_buf
*
path
,
const
char
*
filename
)
int
git_futils_find_global_file
(
git_buf
*
path
,
const
char
*
filename
)
{
{
c
onst
char
*
home
=
getenv
(
"HOME"
)
;
c
har
*
home
;
#ifdef GIT_WIN32
#ifdef GIT_WIN32
if
(
home
==
NULL
)
home
=
win32_getenv
(
L"HOME"
);
home
=
getenv
(
"USERPROFILE"
);
if
(
!
home
)
home
=
win32_getenv
(
L"USERPROFILE"
);
if
(
home
)
git_path_mkposix
(
home
);
#else
home
=
getenv
(
"HOME"
);
#endif
#endif
if
(
home
==
NULL
)
{
if
(
home
==
NULL
)
{
...
@@ -371,6 +406,10 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
...
@@ -371,6 +406,10 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
if
(
git_buf_joinpath
(
path
,
home
,
filename
)
<
0
)
if
(
git_buf_joinpath
(
path
,
home
,
filename
)
<
0
)
return
-
1
;
return
-
1
;
#ifdef GIT_WIN32
git__free
(
home
);
#endif
if
(
git_path_exists
(
path
->
ptr
)
==
false
)
{
if
(
git_path_exists
(
path
->
ptr
)
==
false
)
{
git_buf_clear
(
path
);
git_buf_clear
(
path
);
return
GIT_ENOTFOUND
;
return
GIT_ENOTFOUND
;
...
...
src/win32/utf-conv.c
View file @
5f60fd00
...
@@ -32,19 +32,16 @@ void gitwin_set_utf8(void)
...
@@ -32,19 +32,16 @@ void gitwin_set_utf8(void)
wchar_t
*
gitwin_to_utf16
(
const
char
*
str
)
wchar_t
*
gitwin_to_utf16
(
const
char
*
str
)
{
{
wchar_t
*
ret
;
wchar_t
*
ret
;
size_
t
cb
;
in
t
cb
;
if
(
!
str
)
if
(
!
str
)
return
NULL
;
return
NULL
;
cb
=
strlen
(
str
)
*
sizeof
(
wchar_t
);
cb
=
MultiByteToWideChar
(
_active_codepage
,
0
,
str
,
-
1
,
NULL
,
0
);
if
(
cb
==
0
)
if
(
cb
==
0
)
return
(
wchar_t
*
)
git__calloc
(
1
,
sizeof
(
wchar_t
));
return
(
wchar_t
*
)
git__calloc
(
1
,
sizeof
(
wchar_t
));
/* Add space for null terminator */
ret
=
(
wchar_t
*
)
git__malloc
(
cb
*
sizeof
(
wchar_t
));
cb
+=
sizeof
(
wchar_t
);
ret
=
(
wchar_t
*
)
git__malloc
(
cb
);
if
(
!
ret
)
if
(
!
ret
)
return
NULL
;
return
NULL
;
...
@@ -59,7 +56,8 @@ wchar_t* gitwin_to_utf16(const char* str)
...
@@ -59,7 +56,8 @@ wchar_t* gitwin_to_utf16(const char* str)
int
gitwin_append_utf16
(
wchar_t
*
buffer
,
const
char
*
str
,
size_t
len
)
int
gitwin_append_utf16
(
wchar_t
*
buffer
,
const
char
*
str
,
size_t
len
)
{
{
int
result
=
MultiByteToWideChar
(
_active_codepage
,
0
,
str
,
-
1
,
buffer
,
(
int
)
len
);
int
result
=
MultiByteToWideChar
(
_active_codepage
,
0
,
str
,
-
1
,
buffer
,
(
int
)
len
);
if
(
result
==
0
)
if
(
result
==
0
)
giterr_set
(
GITERR_OS
,
"Could not convert string to UTF-16"
);
giterr_set
(
GITERR_OS
,
"Could not convert string to UTF-16"
);
return
result
;
return
result
;
...
@@ -68,23 +66,22 @@ int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
...
@@ -68,23 +66,22 @@ int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
char
*
gitwin_from_utf16
(
const
wchar_t
*
str
)
char
*
gitwin_from_utf16
(
const
wchar_t
*
str
)
{
{
char
*
ret
;
char
*
ret
;
size_
t
cb
;
in
t
cb
;
if
(
!
str
)
if
(
!
str
)
return
NULL
;
return
NULL
;
cb
=
wcslen
(
str
)
*
sizeof
(
char
);
cb
=
WideCharToMultiByte
(
_active_codepage
,
0
,
str
,
-
1
,
NULL
,
0
,
NULL
,
NULL
);
if
(
cb
==
0
)
if
(
cb
==
0
)
return
(
char
*
)
git__calloc
(
1
,
sizeof
(
char
));
return
(
char
*
)
git__calloc
(
1
,
sizeof
(
char
));
/* Add space for null terminator */
cb
+=
sizeof
(
char
);
ret
=
(
char
*
)
git__malloc
(
cb
);
ret
=
(
char
*
)
git__malloc
(
cb
);
if
(
!
ret
)
if
(
!
ret
)
return
NULL
;
return
NULL
;
if
(
WideCharToMultiByte
(
_active_codepage
,
0
,
str
,
-
1
,
ret
,
(
int
)
cb
,
NULL
,
NULL
)
==
0
)
{
if
(
WideCharToMultiByte
(
_active_codepage
,
0
,
str
,
-
1
,
ret
,
(
int
)
cb
,
NULL
,
NULL
)
==
0
)
{
giterr_set
(
GITERR_OS
,
"Could not convert string to UTF-8"
);
giterr_set
(
GITERR_OS
,
"Could not convert string to UTF-8"
);
git__free
(
ret
);
git__free
(
ret
);
ret
=
NULL
;
ret
=
NULL
;
...
...
tests-clar/core/env.c
0 → 100644
View file @
5f60fd00
#include "clar_libgit2.h"
#include "fileops.h"
#include "path.h"
#ifdef GIT_WIN32
#include "win32/utf-conv.h"
static
char
*
cl_getenv
(
const
char
*
name
)
{
wchar_t
*
name_utf16
=
gitwin_to_utf16
(
name
);
DWORD
value_len
,
alloc_len
;
wchar_t
*
value_utf16
;
char
*
value_utf8
;
cl_assert
(
name_utf16
);
alloc_len
=
GetEnvironmentVariableW
(
name_utf16
,
NULL
,
0
);
if
(
alloc_len
<=
0
)
return
NULL
;
cl_assert
(
value_utf16
=
git__calloc
(
alloc_len
,
sizeof
(
wchar_t
)));
value_len
=
GetEnvironmentVariableW
(
name_utf16
,
value_utf16
,
alloc_len
);
cl_assert_equal_i
(
value_len
,
alloc_len
-
1
);
cl_assert
(
value_utf8
=
gitwin_from_utf16
(
value_utf16
));
git__free
(
value_utf16
);
return
value_utf8
;
}
static
int
cl_setenv
(
const
char
*
name
,
const
char
*
value
)
{
wchar_t
*
name_utf16
=
gitwin_to_utf16
(
name
);
wchar_t
*
value_utf16
=
value
?
gitwin_to_utf16
(
value
)
:
NULL
;
cl_assert
(
name_utf16
);
cl_assert
(
SetEnvironmentVariableW
(
name_utf16
,
value_utf16
));
git__free
(
name_utf16
);
git__free
(
value_utf16
);
return
0
;
}
#else
#include <stdlib.h>
#define cl_getenv(n) getenv(n)
#define cl_setenv(n,v) (v) ? setenv((n),(v),1) : unsetenv(n)
#endif
static
char
*
env_home
=
NULL
;
#ifdef GIT_WIN32
static
char
*
env_userprofile
=
NULL
;
#endif
void
test_core_env__initialize
(
void
)
{
env_home
=
cl_getenv
(
"HOME"
);
#ifdef GIT_WIN32
env_userprofile
=
cl_getenv
(
"USERPROFILE"
);
#endif
}
void
test_core_env__cleanup
(
void
)
{
cl_setenv
(
"HOME"
,
env_home
);
#ifdef GIT_WIN32
cl_setenv
(
"USERPROFILE"
,
env_userprofile
);
git__free
(
env_home
);
git__free
(
env_userprofile
);
#endif
}
void
test_core_env__0
(
void
)
{
static
char
*
home_values
[]
=
{
"fake_home"
,
"fáke_hõme"
,
/* all in latin-1 supplement */
"fĀke_Ĥome"
,
/* latin extended */
"fακε_hοmέ"
,
/* having fun with greek */
"faงe_นome"
,
/* now I have no idea, but thai characters */
"f
\xe1\x9c
x80ke_
\xe1\x9c\x91
ome"
,
/* tagalog characters */
"
\xe1\xb8\x9f
Ảke_hoṁe"
,
/* latin extended additional */
"
\xf0\x9f\x98\x98\xf0\x9f\x98\x82
"
,
/* emoticons */
NULL
};
git_buf
path
=
GIT_BUF_INIT
,
found
=
GIT_BUF_INIT
;
char
**
val
;
char
*
check
;
for
(
val
=
home_values
;
*
val
!=
NULL
;
val
++
)
{
if
(
p_mkdir
(
*
val
,
0777
)
==
0
)
{
/* if we can't make the directory, let's just assume
* we are on a filesystem that doesn't support the
* characters in question and skip this test...
*/
cl_git_pass
(
git_path_prettify
(
&
path
,
*
val
,
NULL
));
cl_git_pass
(
cl_setenv
(
"HOME"
,
path
.
ptr
));
/* do a quick check that it was set correctly */
check
=
cl_getenv
(
"HOME"
);
cl_assert_equal_s
(
path
.
ptr
,
check
);
#ifdef GIT_WIN32
git__free
(
check
);
cl_git_pass
(
cl_setenv
(
"USERPROFILE"
,
path
.
ptr
));
/* do a quick check that it was set correctly */
check
=
cl_getenv
(
"USERPROFILE"
);
cl_assert_equal_s
(
path
.
ptr
,
check
);
git__free
(
check
);
#endif
cl_git_pass
(
git_buf_puts
(
&
path
,
"/testfile"
));
cl_git_mkfile
(
path
.
ptr
,
"find me"
);
cl_git_pass
(
git_futils_find_global_file
(
&
found
,
"testfile"
));
#ifdef GIT_WIN32
/* do another check with HOME unset */
cl_git_pass
(
cl_setenv
(
"HOME"
,
NULL
));
cl_git_pass
(
git_futils_find_global_file
(
&
found
,
"testfile"
));
#endif
}
}
git_buf_free
(
&
path
);
git_buf_free
(
&
found
);
}
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