1

I'm working through a git rebase , and it's been a number of times where I've had to make decisions about files deleted by us, added by us, deleted by them, etc.

Is there a way to get git to show me only one subset of these files? This way, I can pipe the output into xargs and deal with a set of files all in one go.

In other words, if I have this:

    added by us:     core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithVarType.php
    added by us:     core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithConstants.php
    added by us:     core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtClass.php
    added by us:     core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtMethod.php
    added by us:     core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithValidAnnotationTarget.php
    deleted by us:   vendor/behat/mink/.gitattributes
    deleted by us:   vendor/behat/mink/.gitignore
    deleted by them: vendor/doctrine/annotations/CHANGELOG.md
    deleted by them: vendor/doctrine/annotations/phpstan.neon
    added by them:   vendor/justinrainbow/json-schema/phpunit.xml.dist

I would want to be able to specify, for example, added by us, and see only

    core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithVarType.php
    core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithConstants.php
    core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtClass.php
    core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtMethod.php
    core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithValidAnnotationTarget.php

And then the same with the other combinations.


The closest I've gotten with with ls-files:

$ git ls-files -u

100644 31f22a4cec3c5e170077bcfebc2208d2b37fe075 2       core/tests/Drupal/Tests/ComponentAnnotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtClass.php
100644 9ee0a27f86c900240e781ab103e1bdc6fab957ba 2       core/tests/Drupal/Tests/ComponentAnnotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtMethod.php
100644 505aa27a131e0090af5f2416f240e0cbcee3ff84 2       core/tests/Drupal/Tests/ComponentAnnotation/Doctrine/Fixtures/ClassWithValidAnnotationTarget.php
100644 010e9b933b548c96cb7618844ff160f5e800f6e1 1       vendor/behat/mink/.gitattributes
100644 9db3888aebea01ede0d8a42e87ddb7b430310f90 3       vendor/behat/mink/.gitattributes
100644 66de342a111ac61c9bcba7d19b2372399c8502c3 1       vendor/behat/mink/.gitignore
100644 e3305a8383c4cd841083be61e1b5dcfa31cc4ca0 3       vendor/behat/mink/.gitignore
100644 0b0ba1a71d8c725fa394861e87542b0e612cf738 1       vendor/doctrine/annotations/CHANGELOG.md
100644 c09ebe60107ef2d316321cef1c1bccf5c0545df7 2       vendor/doctrine/annotations/CHANGELOG.md
100644 be267e611cc7a8c7e105f17801e8f2512f56b8fc 1       vendor/doctrine/annotations/phpstan.neon
100644 d2b79a7fd65549181ea808290b13b32dbb78bce0 2       vendor/doctrine/annotations/phpstan.neon
100644 0136d8edcc23976ae263b15acf5512ef57bd9c9e 3       vendor/justinrainbow/json-schema/phpunit.xml.dist

The second column of the output has a number, which seems to indicate the action, and whom it was taken by. But, I don't know the name of that column or file property to reference it. Some files are represented twice, I suppose from different commits, but I'm not sure how git ultimately decides which status to go with for the file.

I also looked at --diff-filter, but it only seems to be able to show me files that are changed at all, not by whom.

$ git diff --name-only --diff-filter=U
core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithVarType.php
core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithConstants.php
core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtClass.php
core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithInvalidAnnotationTargetAtMethod.php
core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/ClassWithValidAnnotationTarget.php
vendor/behat/mink/.gitattributes
vendor/behat/mink/.gitignore
vendor/doctrine/annotations/CHANGELOG.md
vendor/doctrine/annotations/phpstan.neon
vendor/justinrainbow/json-schema/phpunit.xml.dist

Of course, I could use grep and sed or something to filter the lines and extract the text, but I wonder if there is a git-only way to solve this problem.

user151841
  • 17,377
  • 29
  • 109
  • 171
  • How did you get added by us, which command? – Tarek Dakhran Mar 17 '20 at 21:03
  • It's from a `git rebase` – user151841 Mar 17 '20 at 21:07
  • 1
    The only thing I have in mind is `git status -s | grep "^AU" | cut -d ' ' -f 2` – Tarek Dakhran Mar 17 '20 at 21:16
  • The number in the third column (the second is a hash ID) is the *stage number* in the index. The index holds up to three copies of each file: staging slot 1 holds the copy from the merge base commit, staging slot 2 holds the copy from the `--ours` commit, and slot 3 holds the copy from the `--theirs` commit. Files that are "added by us" have a slot-2 entry but no slot-1 or slot-3 entries. – torek Mar 17 '20 at 22:57
  • To work these out, remember that every commit is a full and complete snapshot of all files. So if base, you, and they have a file named *F* it exists in all three slots. Git attempts to merge the three copies. If Git succeeds, Git writes the merge result to slot zero and erases the remaining three slots. If Git fails, Git leaves all three slots in place. An *unoccupied* slot means that file *F* wasn't *in* the corresponding commit at all. – torek Mar 17 '20 at 23:00
  • The big problem here is with renamed files. If the file's name was B in the base, but is O/B in ours and T/B in theirs, you'll have one file in slot 1 named B, one in slot 2 named O/B (ours) and one in slot 3 named T/B (theirs). If there are no renames to worry about, the rest you can figure out automatically. You will need to write code for this. – torek Mar 17 '20 at 23:01

1 Answers1

0

List "deleted by us":

git status --porcelain -uno | sed -n 's/^DU //p'

A linked answer: https://stackoverflow.com/a/69558302/1300170

ruvim
  • 7,151
  • 2
  • 27
  • 36