2

I am trying to write some end-to-end tests for my lab's codebase. My initial idea was to iterate through every commit on master (i.e. every version of the program)

for commit in $(git rev-list master | head)
do
  rm -rf build
  git checkout -b "${commit}" ${commit}
  #do tests, etc.
done

but this method gets pretty messy when I start moving files between different branches. My new idea is to copy the contents of each separate version (commit) into a separate directory, such that the directory structure would look like

all_versions/                                                                                         │
├── commit_0                                                                                          │
├── commit_1                                                                                          │
└── commit_2

Is there a git command that I can use to cleanly copy the contents of an entire commit into a directory?

Luciano
  • 493
  • 5
  • 14
  • 1
    Git doesn't provide an export command that produces a directory but it provides [`git archive`](https://git-scm.com/docs/git-archive) that outputs a `tar` archive. Its output can be piped to `tar -x` that restores the files and the directory structure. – axiac May 09 '18 at 14:13

1 Answers1

3

Git doesn't provide an export command that produces a directory but it provides git archive that outputs a tar archive. Its output can be piped to tar -x that restores the files and the directory structure.

Assuming you have already computed the output directory (commit_0, commit_1 etc. in your example) into the variable dir, the command is:

git archive $commit --prefix="$dir/" | tar x -C all_versions
axiac
  • 68,258
  • 9
  • 99
  • 134
  • This almost works, but the output directory is empty. – Luciano May 09 '18 at 14:27
  • 2
    It works fine for me, I tested it before posting the answer. The `all_versions` directory must exist (and you better put an absolute path there), the subdirectory whose name is stored in `$dir` does not need to exist, the `/` after `$dir` is required. If `$dir` contains spaces then use `--prefix="$dir/"` (I updated it in the answer too). Of course, you have to be sure that `$commit` is a valid commit reference (branch name or hash). – axiac May 09 '18 at 14:30
  • 1
    Ah, I found the error I was making. Thank you so much! – Luciano May 09 '18 at 14:31