Skip to content

Commit a969009

Browse files
committed
Merge 'hide-dotgit' into HEAD
2 parents 592db0b + 6c7bcdc commit a969009

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ See linkgit:git-update-index[1].
269269
+
270270
The default is true (when core.filemode is not specified in the config file).
271271

272+
core.hideDotFiles::
273+
(Windows-only) If true (which is the default), mark newly-created
274+
directories and files whose name starts with a dot as hidden.
275+
If 'dotGitOnly', only the .git/ directory is hidden, but no other
276+
files starting with a dot.
277+
272278
core.ignoreCase::
273279
If true, this option enables various workarounds to enable
274280
Git to work better on filesystems that are not case sensitive,

builtin/init-db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ int init_db(const char *template_dir, unsigned int flags)
366366
check_repository_format();
367367

368368
reinit = create_default_files(template_dir);
369+
mark_as_git_dir(get_git_dir());
369370

370371
create_object_directory();
371372

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,13 @@ extern int ref_paranoia;
676676
extern char comment_line_char;
677677
extern int auto_comment_line_char;
678678

679+
enum hide_dotfiles_type {
680+
HIDE_DOTFILES_FALSE = 0,
681+
HIDE_DOTFILES_TRUE,
682+
HIDE_DOTFILES_DOTGITONLY,
683+
};
684+
extern enum hide_dotfiles_type hide_dotfiles;
685+
679686
enum branch_track {
680687
BRANCH_TRACK_UNSPECIFIED = -1,
681688
BRANCH_TRACK_NEVER = 0,

compat/mingw.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../cache.h"
88

99
static const int delay[] = { 0, 1, 10, 20, 40 };
10+
unsigned int _CRT_fmode = _O_BINARY;
1011

1112
int err_win_to_posix(DWORD winerr)
1213
{
@@ -284,13 +285,44 @@ int mingw_rmdir(const char *pathname)
284285
return ret;
285286
}
286287

288+
static int make_hidden(const wchar_t *path)
289+
{
290+
DWORD attribs = GetFileAttributesW(path);
291+
if (SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN | attribs))
292+
return 0;
293+
errno = err_win_to_posix(GetLastError());
294+
return -1;
295+
}
296+
297+
void mingw_mark_as_git_dir(const char *dir)
298+
{
299+
wchar_t wdir[MAX_PATH];
300+
if (hide_dotfiles != HIDE_DOTFILES_FALSE && !is_bare_repository())
301+
if (xutftowcs_path(wdir, dir) < 0 || make_hidden(wdir))
302+
warning("Failed to make '%s' hidden", dir);
303+
git_config_set("core.hideDotFiles",
304+
hide_dotfiles == HIDE_DOTFILES_FALSE ? "false" :
305+
(hide_dotfiles == HIDE_DOTFILES_DOTGITONLY ?
306+
"dotGitOnly" : "true"));
307+
}
308+
287309
int mingw_mkdir(const char *path, int mode)
288310
{
289311
int ret;
290312
wchar_t wpath[MAX_PATH];
291313
if (xutftowcs_path(wpath, path) < 0)
292314
return -1;
293315
ret = _wmkdir(wpath);
316+
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
317+
/*
318+
* In Windows a file or dir starting with a dot is not
319+
* automatically hidden. So lets mark it as hidden when
320+
* such a directory is created.
321+
*/
322+
const char *start = basename((char*)path);
323+
if (*start == '.')
324+
return make_hidden(wpath);
325+
}
294326
return ret;
295327
}
296328

@@ -317,6 +349,17 @@ int mingw_open (const char *filename, int oflags, ...)
317349
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
318350
errno = EISDIR;
319351
}
352+
if ((oflags & O_CREAT) && fd >= 0 &&
353+
hide_dotfiles == HIDE_DOTFILES_TRUE) {
354+
/*
355+
* In Windows a file or dir starting with a dot is not
356+
* automatically hidden. So lets mark it as hidden when
357+
* such a file is created.
358+
*/
359+
const char *start = basename((char*)filename);
360+
if (*start == '.' && make_hidden(wfilename))
361+
warning("Could not mark '%s' as hidden.", filename);
362+
}
320363
return fd;
321364
}
322365

@@ -348,27 +391,39 @@ int mingw_fgetc(FILE *stream)
348391
#undef fopen
349392
FILE *mingw_fopen (const char *filename, const char *otype)
350393
{
394+
int hide = 0;
351395
FILE *file;
352396
wchar_t wfilename[MAX_PATH], wotype[4];
397+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
398+
basename((char*)filename)[0] == '.')
399+
hide = access(filename, F_OK);
353400
if (filename && !strcmp(filename, "/dev/null"))
354401
filename = "nul";
355402
if (xutftowcs_path(wfilename, filename) < 0 ||
356403
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
357404
return NULL;
358405
file = _wfopen(wfilename, wotype);
406+
if (file && hide && make_hidden(wfilename))
407+
warning("Could not mark '%s' as hidden.", filename);
359408
return file;
360409
}
361410

362411
FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
363412
{
413+
int hide = 0;
364414
FILE *file;
365415
wchar_t wfilename[MAX_PATH], wotype[4];
416+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
417+
basename((char*)filename)[0] == '.')
418+
hide = access(filename, F_OK);
366419
if (filename && !strcmp(filename, "/dev/null"))
367420
filename = "nul";
368421
if (xutftowcs_path(wfilename, filename) < 0 ||
369422
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
370423
return NULL;
371424
file = _wfreopen(wfilename, wotype, stream);
425+
if (file && hide && make_hidden(wfilename))
426+
warning("Could not mark '%s' as hidden.", filename);
372427
return file;
373428
}
374429

config.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,15 @@ static int git_default_core_config(const char *var, const char *value)
911911
return 0;
912912
}
913913

914+
if (!strcmp(var, "core.hidedotfiles")) {
915+
if (value && !strcasecmp(value, "dotgitonly")) {
916+
hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
917+
return 0;
918+
}
919+
hide_dotfiles = git_config_bool(var, value);
920+
return 0;
921+
}
922+
914923
/* Add other config variables here and to Documentation/config.txt. */
915924
return 0;
916925
}

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int merge_log_config = -1;
6666
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
6767
struct startup_info *startup_info;
6868
unsigned long pack_size_limit_cfg;
69+
enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
6970

7071
#ifndef PROTECT_HFS_DEFAULT
7172
#define PROTECT_HFS_DEFAULT 0

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,4 +950,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
950950
#define getc_unlocked(fh) getc(fh)
951951
#endif
952952

953+
#ifndef mark_as_git_dir
954+
#define mark_as_git_dir(x) /* noop */
955+
#endif
956+
953957
#endif

t/t0001-init.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,32 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
339339
test_path_is_dir realgitdir/refs
340340
'
341341

342+
# Tests for the hidden file attribute on windows
343+
is_hidden () {
344+
test "1" -eq "$(echo puts [file attributes $1 -hidden]|tclsh)"
345+
}
346+
347+
test_expect_success MINGW 'plain hidden' '
348+
rm -rf newdir &&
349+
(
350+
unset GIT_DIR GIT_WORK_TREE
351+
mkdir newdir &&
352+
cd newdir &&
353+
git init &&
354+
is_hidden .git
355+
) &&
356+
check_config newdir/.git false unset
357+
'
358+
359+
test_expect_success MINGW 'plain bare not hidden' '
360+
rm -rf newdir &&
361+
(
362+
unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
363+
mkdir newdir &&
364+
cd newdir &&
365+
git --bare init
366+
) &&
367+
! is_hidden newdir
368+
'
369+
342370
test_done

0 commit comments

Comments
 (0)