Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 5ad0401

Browse files
kusmakasal
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 9d86323 commit 5ad0401

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ The default is true, except linkgit:git-clone[1] or linkgit:git-init[1]
213213
will probe and set core.fileMode false if appropriate when the
214214
repository is created.
215215

216+
core.hideDotFiles::
217+
(Windows-only) If true (which is the default), mark newly-created
218+
directories and files whose name starts with a dot as hidden.
219+
If 'dotGitOnly', only the .git/ directory is hidden, but no other
220+
files starting with a dot.
221+
216222
core.ignorecase::
217223
If true, this option enables various workarounds to enable
218224
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
@@ -385,6 +385,7 @@ int init_db(const char *template_dir, unsigned int flags)
385385
check_repository_format();
386386

387387
reinit = create_default_files(template_dir);
388+
mark_as_git_dir(get_git_dir());
388389

389390
create_object_directory();
390391

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,13 @@ extern int precomposed_unicode;
594594
*/
595595
extern char comment_line_char;
596596

597+
enum hide_dotfiles_type {
598+
HIDE_DOTFILES_FALSE = 0,
599+
HIDE_DOTFILES_TRUE,
600+
HIDE_DOTFILES_DOTGITONLY,
601+
};
602+
extern enum hide_dotfiles_type hide_dotfiles;
603+
597604
enum branch_track {
598605
BRANCH_TRACK_UNSPECIFIED = -1,
599606
BRANCH_TRACK_NEVER = 0,

compat/mingw.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <wchar.h>
55
#include "../strbuf.h"
66
#include "../run-command.h"
7+
#include "../cache.h"
78

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

1012
int err_win_to_posix(DWORD winerr)
1113
{
@@ -283,13 +285,40 @@ int mingw_rmdir(const char *pathname)
283285
return ret;
284286
}
285287

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+
}
304+
286305
int mingw_mkdir(const char *path, int mode)
287306
{
288307
int ret;
289308
wchar_t wpath[MAX_PATH];
290309
if (xutftowcs_path(wpath, path) < 0)
291310
return -1;
292311
ret = _wmkdir(wpath);
312+
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
313+
/*
314+
* In Windows a file or dir starting with a dot is not
315+
* automatically hidden. So lets mark it as hidden when
316+
* such a directory is created.
317+
*/
318+
const char *start = basename((char*)path);
319+
if (*start == '.')
320+
return make_hidden(wpath);
321+
}
293322
return ret;
294323
}
295324

@@ -316,6 +345,17 @@ int mingw_open (const char *filename, int oflags, ...)
316345
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
317346
errno = EISDIR;
318347
}
348+
if ((oflags & O_CREAT) && fd >= 0 &&
349+
hide_dotfiles == HIDE_DOTFILES_TRUE) {
350+
/*
351+
* In Windows a file or dir starting with a dot is not
352+
* automatically hidden. So lets mark it as hidden when
353+
* such a file is created.
354+
*/
355+
const char *start = basename((char*)filename);
356+
if (*start == '.' && make_hidden(wfilename))
357+
warning("Could not mark '%s' as hidden.", filename);
358+
}
319359
return fd;
320360
}
321361

@@ -347,27 +387,39 @@ int mingw_fgetc(FILE *stream)
347387
#undef fopen
348388
FILE *mingw_fopen (const char *filename, const char *otype)
349389
{
390+
int hide = 0;
350391
FILE *file;
351392
wchar_t wfilename[MAX_PATH], wotype[4];
393+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
394+
basename((char*)filename)[0] == '.')
395+
hide = access(filename, F_OK);
352396
if (filename && !strcmp(filename, "/dev/null"))
353397
filename = "nul";
354398
if (xutftowcs_path(wfilename, filename) < 0 ||
355399
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
356400
return NULL;
357401
file = _wfopen(wfilename, wotype);
402+
if (file && hide && make_hidden(wfilename))
403+
warning("Could not mark '%s' as hidden.", filename);
358404
return file;
359405
}
360406

361407
FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
362408
{
409+
int hide = 0;
363410
FILE *file;
364411
wchar_t wfilename[MAX_PATH], wotype[4];
412+
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
413+
basename((char*)filename)[0] == '.')
414+
hide = access(filename, F_OK);
365415
if (filename && !strcmp(filename, "/dev/null"))
366416
filename = "nul";
367417
if (xutftowcs_path(wfilename, filename) < 0 ||
368418
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
369419
return NULL;
370420
file = _wfreopen(wfilename, wotype, stream);
421+
if (file && hide && make_hidden(wfilename))
422+
warning("Could not mark '%s' as hidden.", filename);
371423
return file;
372424
}
373425

config.c

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

888+
if (!strcmp(var, "core.hidedotfiles")) {
889+
if (value && !strcasecmp(value, "dotgitonly")) {
890+
hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
891+
return 0;
892+
}
893+
hide_dotfiles = git_config_bool(var, value);
894+
return 0;
895+
}
896+
888897
/* Add other config variables here and to Documentation/config.txt. */
889898
return 0;
890899
}

environment.c

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

6768
/*
6869
* The character that begins a commented line in user-editable file

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,4 +728,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
728728
#define gmtime_r git_gmtime_r
729729
#endif
730730

731+
#ifndef mark_as_git_dir
732+
#define mark_as_git_dir(x) /* noop */
733+
#endif
734+
731735
#endif

0 commit comments

Comments
 (0)