Is there a way to get the git root directory in one command?

前端 未结 22 1240
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 09:57

Mercurial has a way of printing the root directory (that contains .hg) via

hg root

Is there something equivalent in git to get the director

22条回答
  •  Happy的楠姐
    2020-11-22 10:45

    As others have noted, the core of the solution is to use git rev-parse --show-cdup. However, there are a few of edge cases to address:

    1. When the cwd already is the root of the working tree, the command yields an empty string.
      Actually it produces an empty line, but command substitution strip off the trailing line break. The final result is an empty string.

      Most answers suggest prepending the output with ./ so that an empty output becomes "./" before it is fed to cd.

    2. When GIT_WORK_TREE is set to a location that is not the parent of the cwd, the output may be an absolute pathname.

      Prepending ./ is wrong in this situation. If a ./ is prepended to an absolute path, it becomes a relative path (and they only refer to the same location if the cwd is the root directory of the system).

    3. The output may contain whitespace.

      This really only applies in the second case, but it has an easy fix: use double quotes around the command substitution (and any subsequent uses of the value).

    As other answers have noted, we can do cd "./$(git rev-parse --show-cdup)", but this breaks in the second edge case (and the third edge case if we leave off the double quotes).

    Many shells treat cd "" as a no-op, so for those shells we could do cd "$(git rev-parse --show-cdup)" (the double quotes protect the empty string as an argument in the first edge case, and preserve whitespace in the third edge case). POSIX says the result of cd "" is unspecified, so it may be best to avoid making this assumption.

    A solution that works in all of the above cases requires a test of some sort. Done explicitly, it might look like this:

    cdup="$(git rev-parse --show-cdup)" && test -n "$cdup" && cd "$cdup"
    

    No cd is done for the first edge case.

    If it is acceptable to run cd . for the first edge case, then the conditional can be done in the expansion of the parameter:

    cdup="$(git rev-parse --show-cdup)" && cd "${cdup:-.}"
    

提交回复
热议问题