Skip to content

Commit 37e202e

Browse files
kusmadscho
authored andcommitted
core.hideDotFiles: hide '.git' dir by default
At least for cross-platform projects, it makes sense to hide the files starting with a dot, as this is the behavior on Unix/MacOSX. However, at least Eclipse has problems interpreting the hidden flag correctly, so the default is to hide only the .git/ directory. The config setting core.hideDotFiles therefore supports not only 'true' and 'false', but also 'dotGitOnly'. [jes: clarified the commit message, made git init respect the setting by marking the .git/ directory only after reading the config, and added documentation, and rebased on top of current junio/next] Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2a3188b commit 37e202e

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define HCAST(type, handle) ((type)(intptr_t)handle)
1010

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

1314
int err_win_to_posix(DWORD winerr)
1415
{
@@ -286,13 +287,40 @@ int mingw_rmdir(const char *pathname)
286287
return ret;
287288
}
288289

290+
static int make_hidden(const wchar_t *path)
291+
{
292+
DWORD attribs = GetFileAttributesW(path);
293+
if (SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN | attribs))
294+
return 0;
295+
errno = err_win_to_posix(GetLastError());
296+
return -1;
297+
}
298+
299+
void mingw_mark_as_git_dir(const char *dir)
300+
{
301+
wchar_t wdir[MAX_PATH];
302+
if (hide_dotfiles != HIDE_DOTFILES_FALSE && !is_bare_repository())
303+
if (xutftowcs_path(wdir, dir) < 0 || make_hidden(wdir))
304+
warning("Failed to make '%s' hidden", dir);
305+
}
306+
289307
int mingw_mkdir(const char *path, int mode)
290308
{
291309
int ret;
292310
wchar_t wpath[MAX_PATH];
293311
if (xutftowcs_path(wpath, path) < 0)
294312
return -1;
295313
ret = _wmkdir(wpath);
314+
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
315+
/*
316+
* In Windows a file or dir starting with a dot is not
317+
* automatically hidden. So lets mark it as hidden when
318+
* such a directory is created.
319+
*/
320+
const char *start = basename((char*)path);
321+
if (*start == '.')
322+
return make_hidden(wpath);
323+
}
296324
return ret;
297325
}
298326

@@ -319,6 +347,17 @@ int mingw_open (const char *filename, int oflags, ...)
319347
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
320348
errno = EISDIR;
321349
}
350+
if ((oflags & O_CREAT) && fd >= 0 &&
351+
hide_dotfiles == HIDE_DOTFILES_TRUE) {
352+
/*
353+
* In Windows a file or dir starting with a dot is not
354+
* automatically hidden. So lets mark it as hidden when
355+
* such a file is created.
356+
*/
357+
const char *start = basename((char*)filename);
358+
if (*start == '.' && make_hidden(wfilename))
359+
warning("Could not mark '%s' as hidden.", filename);
360+
}
322361
return fd;
323362
}
324363

@@ -350,27 +389,39 @@ int mingw_fgetc(FILE *stream)
350389
#undef fopen
351390
FILE *mingw_fopen (const char *filename, const char *otype)
352391
{
392+
int hide = 0;
353393
FILE *file;
354394
wchar_t wfilename[MAX_PATH], wotype[4];
395+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
396+
basename((char*)filename)[0] == '.')
397+
hide = access(filename, F_OK);
355398
if (filename && !strcmp(filename, "/dev/null"))
356399
filename = "nul";
357400
if (xutftowcs_path(wfilename, filename) < 0 ||
358401
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
359402
return NULL;
360403
file = _wfopen(wfilename, wotype);
404+
if (file && hide && make_hidden(wfilename))
405+
warning("Could not mark '%s' as hidden.", filename);
361406
return file;
362407
}
363408

364409
FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
365410
{
411+
int hide = 0;
366412
FILE *file;
367413
wchar_t wfilename[MAX_PATH], wotype[4];
414+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
415+
basename((char*)filename)[0] == '.')
416+
hide = access(filename, F_OK);
368417
if (filename && !strcmp(filename, "/dev/null"))
369418
filename = "nul";
370419
if (xutftowcs_path(wfilename, filename) < 0 ||
371420
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
372421
return NULL;
373422
file = _wfreopen(wfilename, wotype, stream);
423+
if (file && hide && make_hidden(wfilename))
424+
warning("Could not mark '%s' as hidden.", filename);
374425
return file;
375426
}
376427

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

0 commit comments

Comments
 (0)