4

I am working on a git-related software and I have included unit tests in my code.

I have created a test git project (under a subdirectory) to be used by the tests: resources/test-git-project
This test git project is not associated to any remote, but it contains commits/tags/branches.

What is the safest way to commit the contents of resources/test-git-project in git?

Marinos An
  • 9,481
  • 6
  • 63
  • 96
  • Just to be clear: you had a git repo `resources/test-git-project` and you created a nested git repo inside under `.git` or what? Or you have created empty git repo and wander what is that `.git` directory inside your empty repository? – Andrejs Cainikovs Jun 27 '17 at 09:22
  • Still, I don't understand your question. Are you asking how to handle your repo, like committing changes? What do you mean by safest way? – Andrejs Cainikovs Jun 27 '17 at 09:26
  • @Andrejs Cainikovs I try to commit the contents of a test git repository (which is a subdir of my real repository), as plain files. I'm trying to find out how to commit `.git` directory, so that anyone that checks out the project can get it exactly as I committed it in order to perform the unit tests? – Marinos An Jun 27 '17 at 09:51
  • `.git` is the internal directory of a git repository and you should not touch it. Everything that is stored there is handled automatically. Either me understood the question incorrectly because of not clear question, or you're doing something totally wrong. – Andrejs Cainikovs Jun 27 '17 at 12:09
  • 2
    @AndrejsCainikovs this comes up if you need a git repo as a test asset, i.e. building a plugin that uses git, you need a know state git repo to operate on, and you need that asset committed to another repo. kind of a funky case, but it exists – Ralph Callaway Oct 23 '19 at 16:34
  • 1
    @RalphCallaway That was exactly my use case :) – Marinos An Oct 24 '19 at 07:36

3 Answers3

3

You could either zip it or add it as a git submodule, depending on how often you want to change the contents and what parts of the .git folder are important for your tests.

Adding it as a submodule will make it easier to access the .git directory but will not really commit the content of the directory and manage revisions of it. You will even have different contents on different clones because of when you perform git pull (if you add/remove branches, if you rebase branches, if you locally create dangling objects, etc...)

You could also probably do something with the --git-dir option .

Thibault D.
  • 10,041
  • 3
  • 25
  • 56
3

Thanks to Thibault D. answer I found the solution I was looking for:

I have renamed .git to gitdir and it is now commitable:

When I want to perform updates or see the status of the test repo I do the following:

git --git-dir=resources/test-git-project/gitdir --work-tree=resources/test-git-project anycommand

Before performing the unit tests I copy the directory resources/test-git-project under /tmp and rename /tmp/test-git-project/gitdir to /tmp/test-git-project/.git, so that I don't have to mess with --git-dir option on my test code.

Marinos An
  • 9,481
  • 6
  • 63
  • 96
-2

You should not have nested git repositories.

Have one unique repo for the code and the test (kind of common), or have two separate repos, but at the same hierachical level in your filesystem, not nested on into another.

Jidey
  • 359
  • 2
  • 6
  • 20
  • the "nested" git repo is just an asset used in testing in this case so it makes sense to check it into another git repo. – NSjonas Oct 23 '19 at 07:53