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

前端 未结 22 1232
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  耶瑟儿~
    2020-11-22 10:54

    Had to solve this myself today. Solved it in C# as I needed it for a program, but I guess it can be esily rewritten. Consider this Public Domain.

    public static string GetGitRoot (string file_path) {
    
        file_path = System.IO.Path.GetDirectoryName (file_path);
    
        while (file_path != null) {
    
            if (Directory.Exists (System.IO.Path.Combine (file_path, ".git")))
                return file_path;
    
            file_path = Directory.GetParent (file_path).FullName;
    
        }
    
        return null;
    
    }
    

提交回复
热议问题