0

I'm trying to get a list of only new directories using git diff (or something else).

I can get a list of new files, or show only directories, but a file can be added to a directory that already exists.

Any ideas?

3
  • 1
    Git doesn't store directories by themselves. It only stores files and the paths to those files. Thus, git doesn't know what directories are new. It only knows it has files in directories that didn't exist before. Commented Dec 14, 2021 at 0:33
  • What happened when you tried to combine the two options you have linked? Commented Dec 14, 2021 at 0:35
  • @mkrieger1 it will output a list of directories containing new files (but the directories themselves may already exist) Commented Dec 14, 2021 at 0:58

1 Answer 1

2

TL;DR

git ls-tree provides part the information you need, but you'll need to use a regular diff to extract the actual diff.

The general idea

This is not a fully satisfying answer, but you can use git ls-tree to show the contents of a commit, and with the right options reduce that to just the list of directories that commit includes.

Here's the logic behind my solution:

  • git ls-tree -r -t <commit> outputs lines with blob for each file, and tree for each directory.
  • filter that list with grep " tree " to see just the trees, i.e., the directories.
  • Use sed to remove all but the directory name
  • Run this twice and use diff to compare the two directory lists.

Concrete solution

So, to see the list of directories in a commit:

git ls-tree -r -t <commit> | grep ' tree ' | sed 's/.*\t//'

And now diff the output of that command for the two commits. This bash command line will work interactively, but it's ugly:

diff <(git ls-tree -r -t  <commit1> | grep " tree " | sed 's/.*\t//') <(git ls-tree -r -t <commit2> | grep " tree " | sed 's/.*\t//')

To make this solution usable, I would turn it into a script. Let's call it git-new-dirs:

#!/bin/bash

commit1=$1
commit2=$2

diff <(git ls-tree -r -t "$commit1" | grep " tree " | sed 's/.*\t//') <(git ls-tree -r -t "$commit2" | grep " tree " | sed 's/.*\t//')

If you put that script somewhere on your PATH, you will then be able to use it by typing

git new-dirs <commit1> <commit2>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.