首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在wsl2网络驱动下处理git

问如何在wsl2网络驱动下处理git
EN

Stack Overflow用户
提问于 2020-06-17 12:54:53
回答 2查看 2.2K关注 0票数 1

我刚刚将docker配置为在wsl2下运行。这是有效的,没有任何相关的问题,我们的项目运行良好。

在本文之后,我介绍了如何设置wsl2对接环境:https://www.docker.com/blog/docker-desktop-wsl-2-best-practices/

出于性能考虑,本文建议将项目文件放入linux系统中,而不是使用/mnt下的挂载来访问它。

为了访问这些文件,本文提出了以下选项:

  • 使用Visual代码(它可以直接访问wsl )
  • 使用\\wsl$\下的网络共享
  • 直接在linux中运行IDE

有些同事使用的IDE既不兼容linux,也不能访问wsl。所以我试着让他们通过网络共享来访问这个项目。这些同事在IDE中使用git集成,不习惯使用git cli。

Git运行良好,只是有一个小问题。存储库中有文件,它们具有可执行权限(+x)。这些文件在linux上有perms 755,在windows网络驱动器上有644。

因为实际的文件perms与linux环境不同,所以git会检测到这些文件的更改。

是否有办法将正确的文件权限公开到windows环境中?

对于如何解决这个问题,你还有什么其他的想法吗?

EN

回答 2

Stack Overflow用户

发布于 2020-06-17 19:14:56

要同步Linux文件权限,需要为Linux发行版启用元数据。创建具有以下内容的/etc/wsl.conf文件:

代码语言:javascript
复制
[automount]
root = /
options = "metadata"

root是可选的,但很有用,它将您的Windows驱动器安装到/c,而不是/mnt/c,这样可以节省一些按键。

若要确保此操作生效,请关闭Windows并重新登录。

查看文档获得更多详细信息,以及设置umask之类的高级设置。

同时,请记住启用Git的自动CRLF处理

代码语言:javascript
复制
$ git config --global core.autocrlf true

这将为您在WSL上使用Git时节省很多麻烦。

票数 1
EN

Stack Overflow用户

发布于 2020-06-20 04:27:53

我在git中也遇到了类似的问题,您可能无法使用各种神秘错误运行commit。其中的一部分就是git打开文件的方式,所以希望这篇文章能以某种方式解开它。

背景

在最新的Windows版本中,关于WSL如何与您的文件系统交互,WSL中有一些更改。一个问题是,以前Windows和Linux文件系统之间的不兼容性意味着大多数文件实际上在外部存储上拥有‘777’权限,因此任何用户都可以读/写/执行任何东西。这对于Linux来说并不理想,因为您不能存储私钥等等。微软现在已经改变了这种行为,以便文件可以通过扩展属性同时拥有Windows (NTFS)和Linux权限。在普通驱动器上,可以使用以下命令安装带有元数据的驱动器:

代码语言:javascript
复制
umount /mnt/c;
mount -t drvfs C:\\ /mnt/c/ -o metadata

不幸的是,它们似乎也将这一更改扩展到网络和外部文件系统(如exFAT),它没有可扩展的属性,并且变得有点混乱,使外部驱动器无法使用。例如,如果该文件在Windows中是只读的,那么您就不能在WSL中写入它;您也不能用sudo重写它,因为Windows权限会覆盖WSL。这些变化破坏了很多程序,我今天在尝试将更改上传到git时发现了这些程序。这是那些在互联网上还没有解决方案的新问题之一,所以我附上了一个文件来解决这个问题。

要运行,首先编译共享对象:

代码语言:javascript
复制
cc -Wall -O3 -D_GNU_SOURCE -fPIC -c -o githack.o githack.c; gcc -o githack.so -nostartfiles -shared githack.o -ldl;

然后运行以LD_PRELOAD为前缀的命令

代码语言:javascript
复制
LD_PRELOAD=./githack.so git commit -a -m "Another interesting commit"

如何为其他程序进行研究

对于git来说,具体的问题是:

代码语言:javascript
复制
error: insufficient permission for adding an object to repository database .git/objects

要找出失败的原因,可以使用strace

代码语言:javascript
复制
strace git commit -a -m "Another interesting commit"

>

...

gettimeofday({tv_sec=1592618056, tv_usec=52991}, NULL) = 0

getpid()                               = 651

openat(AT_FDCWD, ".git/objects/78/tmp_obj_flbKNc", O_RDWR|O_CREAT|O_EXCL, 0444) = -1 EACCES (Permission denied)

write(2, "error: insufficient permission f"..., 88error: insufficient permission for adding an object to repository database .git/objects

) = 88

close(4)                               = 0

...

在打印的错误行之前,我们会看到它为什么失败(-1),因此要修复它,需要拦截该调用。您可以从ltrace中确定

代码语言:javascript
复制
ltrace git commit -a -m "Latest local copy from George"

>

...

open64(".git/objects/78/tmp_obj_zDayCc", 194, 0444)                                                                                                         = -1

__errno_location()                                                                                                                                          = 0x7f2777001000

__errno_location()                                                                                                                                          = 0x7f2777001000

__vsnprintf_chk(0x7fffd4786d00, 4096, 1, 4096)                                                                                                              = 80

__fprintf_chk(0x7f277631c680, 1, 0x7f27773eacfc, 0x7f27773c8083error: insufficient permission for adding an object to repository database .git/objects

)                                                                                            = 88

close(4)

...

因此,本文底部附加的代码截取了open64代码,其标志与194相等。

解决方案代码(名称githack.c)

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
//#define openat ignorethisopen
#define open ignorethisopen
#define open64 ignorethisopen64
#include <fcntl.h>
//#undef openat
#undef open
#undef open64
#include <dlfcn.h>


/*
    'strace git ...' will show git fail on an openat() command
    this is probably implemented as open64() on your system
    you can confirm this by use of 'ltrace git ...'
    you may also need to adjust the oflag comparison of 194
    George O'Neill 2020/06/20 (feel free to reach out if issues remain)
*/


/*static int (*___openat)(int, char *, int, mode_t);*/
static int (*___open)(const char *, int, mode_t);
static int (*___open64)(const char *, int, mode_t);


static void* dlwrap(const char *fn)
{
    const char *e;
    void *p = dlsym(RTLD_NEXT, fn);
    if ((e=dlerror())!=0)
        fprintf(stderr, "dlsym(RTLD_NEXT,'%s'): %s\r\n", fn, e);
    return p;
}


void _init(void)
{
    ___open = dlwrap("open");
    ___open64 = dlwrap("open64");
}


/*int openat(int dirfd, const char *pathname, int oflag, mode_t mode)*/
int open(const char *pathname, int oflag, mode_t mode)
{
    if (oflag && oflag == 194)
        return ___open(pathname, oflag, S_IRWXU);
    return ___open(pathname, oflag, mode);
}


int open64(const char *pathname, int oflag, mode_t mode)
{
    if (oflag && oflag == 194)
        return ___open64(pathname, oflag, S_IRWXU);
    return ___open64(pathname, oflag, mode);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62429516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档