threads / discuss / 36369

Handling empty directories in Git

Subject: Handling empty directories in Git

## tl;dr

9 messages between Apr 8, 2014 and Apr 11, 2014.

replies: 8people: 5as markdown or json

Olivier LE ROY· Apr 8, 2014, 14:47 UTC · lore
Hello, 
I have a project under SVN with contains empty directories.
I would like to move this project on a Git server, still handling empty directories.
The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.
Is there any expert able to tell me: this cannot be done in Git, or this can be done by the following trick, or why there is no valuable reason to maintain empty directories under version control?
Regards,

Olivier LE ROY France

Andrew Keller· Apr 8, 2014, 17:02 UTC · re: Olivier LE ROY · lore

Re: Handling empty directories in Git

On Apr 8, 2014, at 10:47 AM, Olivier LE ROY <olivier_le_roy@yahoo.com> wrote:
Show 9 quoted lines
> Hello, 
> 
> I have a project under SVN with contains empty directories.
> 
> I would like to move this project on a Git server, still handling empty directories.
> 
> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.
> 
> Is there any expert able to tell me: this cannot be done in Git, or this can be done by the following trick, or why there is no valuable reason to maintain empty directories under version control?
Git is designed to track files.  The existence of folders is secondary to the notion that files have a relative path inside the repository, which is perceived by the user as folders.
Why can't your scripts create the folders on demand?  Or, could your scripts interpret a missing folder as an empty folder?

Thanks, Andrew Keller

Andrew Keller· Apr 8, 2014, 17:20 UTC · re: Andrew Keller · lore

Re: Handling empty directories in Git

On Apr 8, 2014, at 1:02 PM, Andrew Keller <andrew@kellerfarm.com> wrote:
Show 13 quoted lines
> On Apr 8, 2014, at 10:47 AM, Olivier LE ROY <olivier_le_roy@yahoo.com> wrote:
> 
>> Hello, 
>> 
>> I have a project under SVN with contains empty directories.
>> 
>> I would like to move this project on a Git server, still handling empty directories.
>> 
>> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.
>> 
>> Is there any expert able to tell me: this cannot be done in Git, or this can be done by the following trick, or why there is no valuable reason to maintain empty directories under version control?
> 
> Git is designed to track files.  The existence of folders is secondary to the notion that files have a relative path inside the repository, which is perceived by the user as folders.
To clarify: That's Git's "personality" from the point of view of the front end, and is not the same as how data is actually stored.

Thanks, Andrew Keller

Olivier LE ROY· Apr 8, 2014, 17:47 UTC · re: Andrew Keller · lore

Re: Handling empty directories in Git

Hello Andrew,
yes that is possible to have scripts create the missing directories.
The reason I asked this is people at my work want to avoid the hassle of having to create a script for that. They want to checkout seamlessly as they used to do with subversion.
I guess it is similar as programming in Java and programming in plain old C.
Olivier LE ROY 

________________________________ De : Andrew Keller <andrew@kellerfarm.com> À : Olivier LE ROY <olivier_le_roy@yahoo.com> Cc : "git@vger.kernel.org" <git@vger.kernel.org> Envoyé le : Mardi 8 avril 2014 17h02 Objet : Re: Handling empty directories in Git

On Apr 8, 2014, at 10:47 AM, Olivier LE ROY <olivier_le_roy@yahoo.com> wrote:
Show 9 quoted lines
> Hello, 
> 
> I have a project under SVN with contains empty directories.
> 
> I would like to move this project on a Git server, still handling empty directories.
> 
> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.
> 
> Is there any expert able to tell me: this cannot be done in Git, or this can be done by the following trick, or why there is no valuable reason to maintain empty directories under version control?
Git is designed to track files.  The existence of folders is secondary to the notion that files have a relative path inside the repository, which is perceived by the user as folders.
Why can't your scripts create the folders on demand?  Or, could your scripts interpret a missing folder as an empty folder?

Thanks, Andrew Keller

Matthieu Moy· Apr 8, 2014, 17:03 UTC · re: Olivier LE ROY · lore

Re: Handling empty directories in Git

Olivier LE ROY <olivier_le_roy@yahoo.com> writes:
> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.

Another option is to have a post-checkout hook that does the mkdir for you. Or do that in the build system if your use case is to store untracked generated files in the directory.

> Is there any expert able to tell me: this cannot be done in Git, or
> this can be done by the following trick,
https://git.wiki.kernel.org/index.php/Git_FAQ#Can_I_add_empty_directories.3F
Short answer: it cannot be done.
> or why there is no valuable reason to maintain empty directories under
> version control?

The reason would be closer to "there is a valuable reason, but not valuable enough to change Git to do it". It's actually not so easy to track directories properly. Storing them in the Git repository is actually possible (actually, an empty tree is a special case of this, and is obviously supported), but defining and implementing a decent behavior for each Git command wrt this is not trivial.

David Kastrup gave it a try a few years ago. I don't remember exactly what made him give up, but it was never completed and merged.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
David Kastrup· Apr 8, 2014, 17:36 UTC · re: Matthieu Moy · lore

Re: Handling empty directories in Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 9 quoted lines
> The reason would be closer to "there is a valuable reason, but not
> valuable enough to change Git to do it". It's actually not so easy to
> track directories properly. Storing them in the Git repository is
> actually possible (actually, an empty tree is a special case of this,
> and is obviously supported), but defining and implementing a decent
> behavior for each Git command wrt this is not trivial.
>
> David Kastrup gave it a try a few years ago. I don't remember exactly
> what made him give up, but it was never completed and merged.

Oh, most likely what afflicts most of my unfinished projects. I lost focus at some point of time. I don't remember any fundamentally unsolvable problems, but then I don't remember much at all. There were some annoyances with sorting order (either regarding the sorting of xxx/ or . or ./ or whatever) and some other stuff.

If anybody wants to take a look at the direction of unfinished stuff, I can see whether there are some old backups with git repos in my possession. But I really have no idea how much of the design might have ended up in actual comments or code, and how much on some scraps of paper or half-committed memory, and how much of that might have been invalidated by other scraps of paper and half-committed memory.

So there is not likely to be more than food for thought recoverable.

I'm amused that you remember me being involved with that. I think I myself had forgotten all about it until recently. I don't even recollect what made me remember again: looking at some old repo/commit or searching in some old mailing list archive.

-- 
David Kastrup
Olivier LE ROY· Apr 8, 2014, 18:39 UTC · re: David Kastrup · lore

Re: Handling empty directories in Git

Hi David,
thanks for the whole story. We understand why the empty directories handling was never completed, and how it could be, if one developer submited patches for that.
Regards,
Olivier LE ROY

----- Mail original ----- De : David Kastrup <dak@gnu.org> À : Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> Cc : Olivier LE ROY <olivier_le_roy@yahoo.com>; "git@vger.kernel.org" <git@vger.kernel.org> Envoyé le : Mardi 8 avril 2014 17h36 Objet : Re: Handling empty directories in Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 9 quoted lines
> The reason would be closer to "there is a valuable reason, but not
> valuable enough to change Git to do it". It's actually not so easy to
> track directories properly. Storing them in the Git repository is
> actually possible (actually, an empty tree is a special case of this,
> and is obviously supported), but defining and implementing a decent
> behavior for each Git command wrt this is not trivial.
>
> David Kastrup gave it a try a few years ago. I don't remember exactly
> what made him give up, but it was never completed and merged.

Oh, most likely what afflicts most of my unfinished projects.  I lost focus at some point of time.  I don't remember any fundamentally unsolvable problems, but then I don't remember much at all.  There were some annoyances with sorting order (either regarding the sorting of xxx/ or . or ./ or whatever) and some other stuff.

If anybody wants to take a look at the direction of unfinished stuff, I can see whether there are some old backups with git repos in my possession.  But I really have no idea how much of the design might have ended up in actual comments or code, and how much on some scraps of paper or half-committed memory, and how much of that might have been invalidated by other scraps of paper and half-committed memory.

So there is not likely to be more than food for thought recoverable.

I'm amused that you remember me being involved with that.  I think I myself had forgotten all about it until recently.  I don't even recollect what made me remember again: looking at some old repo/commit or searching in some old mailing list archive.

-- 
David Kastrup
Olivier LE ROY· Apr 8, 2014, 18:06 UTC · re: Matthieu Moy · lore

Re: Handling empty directories in Git

Hi Mattieu,
thanks for this answer. It is clear enough.
Olivier LE ROY

----- Mail original ----- De : Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> À : Olivier LE ROY <olivier_le_roy@yahoo.com> Cc : "git@vger.kernel.org" <git@vger.kernel.org> Envoyé le : Mardi 8 avril 2014 17h03 Objet : Re: Handling empty directories in Git

Olivier LE ROY <olivier_le_roy@yahoo.com> writes:
> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.

Another option is to have a post-checkout hook that does the mkdir for you. Or do that in the build system if your use case is to store untracked generated files in the directory.

> Is there any expert able to tell me: this cannot be done in Git, or
> this can be done by the following trick,
https://git.wiki.kernel.org/index.php/Git_FAQ#Can_I_add_empty_directories.3F
Short answer: it cannot be done.
> or why there is no valuable reason to maintain empty directories under
> version control?

The reason would be closer to "there is a valuable reason, but not valuable enough to change Git to do it". It's actually not so easy to track directories properly. Storing them in the Git repository is actually possible (actually, an empty tree is a special case of this, and is obviously supported), but defining and implementing a decent behavior for each Git command wrt this is not trivial.

David Kastrup gave it a try a few years ago. I don't remember exactly what made him give up, but it was never completed and merged.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Øyvind A. Holm· Apr 11, 2014, 12:34 UTC · re: Olivier LE ROY · lore

Re: Handling empty directories in Git

On 8 April 2014 16:47, Olivier LE ROY <olivier_le_roy@yahoo.com> wrote:
Show 9 quoted lines
> Hello,
>
> I have a project under SVN with contains empty directories.
>
> I would like to move this project on a Git server, still handling empty directories.
>
> The solution: put a .gitignore file in each empty directory to have them recognized by the Git database cannot work, because some scripts in my projects test the actual emptiness of the directories.
>
> Is there any expert able to tell me: this cannot be done in Git, or this can be done by the following trick, or why there is no valuable reason to maintain empty directories under version control?

Git doesn't support storage of empty directories, but there are ways around it. As you say, you could place empty files in every directory, but I've never liked this concept. Instead, I use a couple of scripts to store/restore empty directories:

https://gist.github.com/sunny256/419015

This creates a file called ".emptydirs" at the top of the repo, and it can easily be implemented into scripts and build processes.

Greetings, Øyvind

← back to recent threads