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

Commit

Permalink
Merge 'hide-dotgit' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
kblees committed Aug 16, 2014
2 parents ffa9869 + f8b7f55 commit 9af43a3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ The default is true, except linkgit:git-clone[1] or linkgit:git-init[1]
will probe and set core.fileMode false if appropriate when the
repository is created.

core.hideDotFiles::
(Windows-only) If true (which is the default), mark newly-created
directories and files whose name starts with a dot as hidden.
If 'dotGitOnly', only the .git/ directory is hidden, but no other
files starting with a dot.

core.ignorecase::
If true, this option enables various workarounds to enable
Git to work better on filesystems that are not case sensitive,
Expand Down
1 change: 1 addition & 0 deletions builtin/init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ int init_db(const char *template_dir, unsigned int flags)
check_repository_format();

reinit = create_default_files(template_dir);
mark_as_git_dir(get_git_dir());

create_object_directory();

Expand Down
7 changes: 7 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ extern int precomposed_unicode;
extern char comment_line_char;
extern int auto_comment_line_char;

enum hide_dotfiles_type {
HIDE_DOTFILES_FALSE = 0,
HIDE_DOTFILES_TRUE,
HIDE_DOTFILES_DOTGITONLY,
};
extern enum hide_dotfiles_type hide_dotfiles;

enum branch_track {
BRANCH_TRACK_UNSPECIFIED = -1,
BRANCH_TRACK_NEVER = 0,
Expand Down
55 changes: 55 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../cache.h"

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

int err_win_to_posix(DWORD winerr)
{
Expand Down Expand Up @@ -284,13 +285,44 @@ int mingw_rmdir(const char *pathname)
return ret;
}

static int make_hidden(const wchar_t *path)
{
DWORD attribs = GetFileAttributesW(path);
if (SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN | attribs))
return 0;
errno = err_win_to_posix(GetLastError());
return -1;
}

void mingw_mark_as_git_dir(const char *dir)
{
wchar_t wdir[MAX_PATH];
if (hide_dotfiles != HIDE_DOTFILES_FALSE && !is_bare_repository())
if (xutftowcs_path(wdir, dir) < 0 || make_hidden(wdir))
warning("Failed to make '%s' hidden", dir);
git_config_set("core.hideDotFiles",
hide_dotfiles == HIDE_DOTFILES_FALSE ? "false" :
(hide_dotfiles == HIDE_DOTFILES_DOTGITONLY ?
"dotGitOnly" : "true"));
}

int mingw_mkdir(const char *path, int mode)
{
int ret;
wchar_t wpath[MAX_PATH];
if (xutftowcs_path(wpath, path) < 0)
return -1;
ret = _wmkdir(wpath);
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
/*
* In Windows a file or dir starting with a dot is not
* automatically hidden. So lets mark it as hidden when
* such a directory is created.
*/
const char *start = basename((char*)path);
if (*start == '.')
return make_hidden(wpath);
}
return ret;
}

Expand All @@ -317,6 +349,17 @@ int mingw_open (const char *filename, int oflags, ...)
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
errno = EISDIR;
}
if ((oflags & O_CREAT) && fd >= 0 &&
hide_dotfiles == HIDE_DOTFILES_TRUE) {
/*
* In Windows a file or dir starting with a dot is not
* automatically hidden. So lets mark it as hidden when
* such a file is created.
*/
const char *start = basename((char*)filename);
if (*start == '.' && make_hidden(wfilename))
warning("Could not mark '%s' as hidden.", filename);
}
return fd;
}

Expand Down Expand Up @@ -348,27 +391,39 @@ int mingw_fgetc(FILE *stream)
#undef fopen
FILE *mingw_fopen (const char *filename, const char *otype)
{
int hide = 0;
FILE *file;
wchar_t wfilename[MAX_PATH], wotype[4];
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
basename((char*)filename)[0] == '.')
hide = access(filename, F_OK);
if (filename && !strcmp(filename, "/dev/null"))
filename = "nul";
if (xutftowcs_path(wfilename, filename) < 0 ||
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
return NULL;
file = _wfopen(wfilename, wotype);
if (file && hide && make_hidden(wfilename))
warning("Could not mark '%s' as hidden.", filename);
return file;
}

FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
{
int hide = 0;
FILE *file;
wchar_t wfilename[MAX_PATH], wotype[4];
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
basename((char*)filename)[0] == '.')
hide = access(filename, F_OK);
if (filename && !strcmp(filename, "/dev/null"))
filename = "nul";
if (xutftowcs_path(wfilename, filename) < 0 ||
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
return NULL;
file = _wfreopen(wfilename, wotype, stream);
if (file && hide && make_hidden(wfilename))
warning("Could not mark '%s' as hidden.", filename);
return file;
}

Expand Down
9 changes: 9 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@ static int git_default_core_config(const char *var, const char *value)
return 0;
}

if (!strcmp(var, "core.hidedotfiles")) {
if (value && !strcasecmp(value, "dotgitonly")) {
hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
return 0;
}
hide_dotfiles = git_config_bool(var, value);
return 0;
}

/* Add other config variables here and to Documentation/config.txt. */
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int merge_log_config = -1;
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
struct startup_info *startup_info;
unsigned long pack_size_limit_cfg;
enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;

/*
* The character that begins a commented line in user-editable file
Expand Down
4 changes: 4 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
#define gmtime_r git_gmtime_r
#endif

#ifndef mark_as_git_dir
#define mark_as_git_dir(x) /* noop */
#endif

#endif
28 changes: 28 additions & 0 deletions t/t0001-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,32 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
test_path_is_dir realgitdir/refs
'

# Tests for the hidden file attribute on windows
is_hidden () {
test "1" -eq "$(echo puts [file attributes $1 -hidden]|tclsh)"
}

test_expect_success MINGW 'plain hidden' '
rm -rf newdir &&
(
unset GIT_DIR GIT_WORK_TREE
mkdir newdir &&
cd newdir &&
git init &&
is_hidden .git
) &&
check_config newdir/.git false unset
'

test_expect_success MINGW 'plain bare not hidden' '
rm -rf newdir
(
unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
mkdir newdir &&
cd newdir &&
git --bare init
) &&
! is_hidden newdir
'

test_done

0 comments on commit 9af43a3

Please sign in to comment.