Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout using wildcard to a specific revision

I have been seeking it on Google for 2 hours and still can't find the solution.

I want to checkout all files with wildcard to a specific revision. I used following command:

git checkout 663090b1c 'src/app/**/*.spec.ts'

But it says:

error: pathspec 'src/app/**/*.spec.ts' did not match any file(s) known to git.

Can anyone help me?

like image 841
Thanh Nguyen Avatar asked Jul 25 '19 02:07

Thanh Nguyen


1 Answers

I believe that this should work, but clearly it doesn't. Here's the effect of using the most recent Git (which you can get at https://github.com/git/git) on Documentation/**/*.txt.

I built Git, then used the system Git (at 2.21, slightly behind this version which is 2.22.0.545.g9c9b961d7e) to do this:

$ rm Documentation/*/*.txt
$ git status --short | head
 D Documentation/RelNotes/1.5.0.1.txt
 D Documentation/RelNotes/1.5.0.2.txt
 D Documentation/RelNotes/1.5.0.3.txt
 D Documentation/RelNotes/1.5.0.4.txt
 D Documentation/RelNotes/1.5.0.5.txt
 D Documentation/RelNotes/1.5.0.6.txt
 D Documentation/RelNotes/1.5.0.7.txt
 D Documentation/RelNotes/1.5.0.txt
 D Documentation/RelNotes/1.5.1.1.txt
 D Documentation/RelNotes/1.5.1.2.txt
$ git checkout -- 'Documentation/**/*.txt'
$ git status --short | head
$ 

Now, however:

$ ./git checkout HEAD -- 'Documentation/**/*.txt'
error: pathspec 'Documentation/**/*.txt' did not match any file(s) known to git

It seems that pathspecs work when used with the index, but not when used with a commit hash.

As a workaround—note that this workaround is somewhat defective, though you can patch around it further—you can first read the desired commit into a temporary index, then do the git checkout from the temporary index. The following script is completely untested.

#! /bin/sh
# git-co-c-ps: git checkout <commit> <pathspec>
# work around a bug in which combining <commit> and <pathspec> does not work
. git-sh-setup
case $# in
2) ;;
*) die "usage: $0 <commit> <pathspec>";;
esac

hash=$(git rev-parse "$1^{commit}") || exit 1
export GIT_INDEX_FILE=$(mktemp) || exit 1
rm -f $GIT_INDEX_FILE
trap "rm -f $GIT_INDEX_FILE" 0 1 2 3 15
git read-tree "$hash" || exit 1
git checkout -- "$2"

The (untested) idea here is to read the commit specified by "$1" into a temporary index. (We could relax the ^{commit} to be just ^{tree} as any tree-ish should suffice.) Then, having read that into a temporary index, we use the mode of "git checkout" with a pathspec ($2) that works, using the temporary index. This writes to the work-tree.

The defect is that the now-updated work-tree files are not also updated in the index. A real git checkout would have copied these same files to the index. It should be possible to unset $GIT_INDEX_FILE and git add the added files to the real index. This is left as an exercise. Note that it is not as trivial as just git add -- "$2" since some work-tree files may be in the work-tree not because they were read into the temporary index by the git read-tree operation, but rather because they were already in the work-tree. (Some may be tracked and modified-but-not-yet-staged/added, and others may be untracked.)

Another, perhaps better, workaround is to use git ls-files on the temporary index, to find the files of interest. Use xargs or similar to pass these file names to a git checkout that is run without the temporary index:

files_file=$(mktemp)
trap "rm -f $files_file" 0 1 2 3 15
(
    export GIT_INDEX_FILE=$(mktemp) || exit 125
    rm -f $GIT_INDEX_FILE
    trap "rm -f $GIT_INDEX_FILE" 0 1 2 3 15
    git read-tree $hash
    git ls-files -z -- "$2"
) > $files_file
# if the inner shell exited with an error, exit now
case $? in 125) exit 1;; esac
xargs -0 ...

(Filling in the rest of this to make a usable Git shell command is also left as an exercise.)

like image 50
torek Avatar answered Oct 27 '22 10:10

torek