windows下repo sync error: cannot initialize work tree

杨锘

1. 问题描述

下载Android源代码时,出现这个错误

Traceback (most recent call last):
  File "E:\LocalProject\Oreo\.repo\repo/main.py", line 538, in <module>
    _Main(sys.argv[1:])
  File "E:\LocalProject\Oreo\.repo\repo/main.py", line 512, in _Main
    result = repo._Run(argv) or 0
  File "E:\LocalProject\Oreo\.repo\repo/main.py", line 185, in _Run
    result = cmd.Execute(copts, cargs)
  File "E:\LocalProject\Oreo\.repo\repo\subcmds\sync.py", line 823, in Execute
    project.Sync_LocalHalf(syncbuf, force_sync=opt.force_sync)
  File "E:\LocalProject\Oreo\.repo\repo\project.py", line 1335, in Sync_LocalHalf
    self._InitWorkTree(force_sync=force_sync)
  File "E:\LocalProject\Oreo\.repo\repo\project.py", line 2502, in _InitWorkTree
    raise GitError("cannot initialize work tree")
error.GitError: cannot initialize work tree

2. 问题分析

2.1 查看哪里出了问题

从log来看,.repo/repo/project.py是要关注的地方,可以定位到脚本:
raise GitError("cannot initialize work tree")
其所在的函数为_InitWorkTree

_InitWorkTree完整的代码如下:

  def _InitWorkTree(self, force_sync=False):
    dotgit = os.path.join(self.worktree, '.git')
    print("dotgit is {0}".format(dotgit))
    print(self.gitdir)
    init_dotgit = not os.path.exists(dotgit)
    try:
      if init_dotgit:
        os.makedirs(dotgit)
        self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True,
                              copy_all=False)

      try:
        self._CheckDirReference(self.gitdir, dotgit, share_refs=True)
      except GitError as e:
        if force_sync:
          try:
            shutil.rmtree(dotgit)
            return self._InitWorkTree(force_sync=False)
          except:
            raise e
        raise e

      if init_dotgit:
        _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())

        cmd = ['read-tree', '--reset', '-u']
        cmd.append('-v')
        cmd.append(HEAD)
        if GitCommand(self, cmd).Wait() != 0:
          raise GitError("cannot initialize work tree")

        self._CopyAndLinkFiles()
    except Exception:
      if init_dotgit:
        shutil.rmtree(dotgit)
      raise

但是此处并不能给我们什么有效信息,所以我们要进一步继续分析

2.2 repo --trace sync

用 repo --trace sync -cdf 将repo的所有动作详细输出(这个过程可能会很长,需要我们耐心等待),会发现在这里出错导致:

: cd E:\LocalProject\Oreo\external\kmod
: git read-tree --reset -u -v HEAD 1>| 2>|
error: unable to create symlink testsuite/rootfs-pristine/test-loaded/sys/module/btusb/drivers/usb:btusb: File exists

至此,我们就定位到了问题。

2.3添加打印信息

当然,在不熟悉repo命令的时候,我们也可以添加log来了解当前卡在了哪里。为了找到当前出错时,git正在处理哪个目录,我在_InitWorkTree
函数中加了2句log信息:

    print("dotgit is {0}".format(dotgit))
    print(self.gitdir)
  def _InitWorkTree(self, force_sync=False):
    dotgit = os.path.join(self.worktree, '.git')
    print("dotgit is {0}".format(dotgit))
    print(self.gitdir)
    init_dotgit = not os.path.exists(dotgit)
    try:
      if init_dotgit:
        os.makedirs(dotgit)
        ...
      ...
    ...

打印出来的log如下:

dotgit is E:\LocalProject\Oreo\external\kmod\.git
E:/LocalProject/Oreo/.repo/projects/external/kmod.git

可以看出当前是在处理E:\LocalProject\Oreo\external\kmod时出了问题。但是由于log有限,我们其实并不能真正的了解到当前发生了什么。


3.解决方案

前面的log是说file无法创建。原因是是要创建的文件名字叫usb:btusb,这种命名是明显违反Windows命名规则的。所以其实是无解的。
所以我打算先绕过这个问题,先把源码的其他部分下载下来,对于这个问题,以后再解决。既然这个项目有问题,那我就先不下载了,不能因小失大嘛。
打开.repo/manifest/default.xml

 <!--<project path="external/kmod" name="platform/external/kmod" groups="pdk" />-->

在网上搜了一圈,好像很多网友在windows下下载代码都遇到了"cannot initialize work tree"的问题。连Microsoft提供的win10下linux子系统的repo都有问题(最后参考链接里边有该问题的解决方案)。也是路径名称的问题。有兴趣的网友可以自己爬楼研究下。
Microsoft:https://github.com/Microsoft/...

我这次下载中还遇到了两次"cannot initialize work tree"这样的问题

下载“platform/libcore”出错

error: unable to create file luni/src/test/resources/org/apache/harmony/tests/java/lang/test?.properties: Invalid argument

“test?.properties”这个也是违反了Windows文件命名问题

下载“external/libunwind”出错

fatal: cannot create directory at 'aux': Invalid argument

这个是文件夹命名为“aux”出错,“aux”是Windows设备名文件夹。DOS上使用AUX,是auxiliary device的缩写,辅助设备,在DOS系统手册有声明这是系统标识符的。AUX默认是异步端口的流名字,通常与Console连在一起,属于标准命名设备,所以不允许你创建使用了系统保留名字的文件,因为文件也属于流设备。

如果大家想体验下这个问题,可以单独下载这三个项目试试 git clone url
下载单个项目的方法 https://segmentfault.com/a/11...

$ git clone   https://aosp.tuna.tsinghua.edu.cn/platform/external/libunwind
Cloning into 'libunwind'...
remote: Counting objects: 16490, done.
remote: Compressing objects: 100% (5188/5188), done.
remote: Total 16490 (delta 11521), reused 15746 (delta 11246)
Receiving objects: 100% (16490/16490), 4.18 MiB | 4.01 MiB/s, done.
Resolving deltas: 100% (11521/11521), done.
fatal: cannot create directory at 'aux': Invalid argument
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'
$ git clone   https://aosp.tuna.tsinghua.edu.cn/platform/external/kmod
Cloning into 'kmod'...
remote: Counting objects: 6864, done.
remote: Compressing objects: 100% (1851/1851), done.
remote: Total 6864 (delta 4941), reused 6514 (delta 4746)
Receiving objects: 100% (6864/6864), 5.51 MiB | 420.00 KiB/s, done.
Resolving deltas: 100% (4941/4941), done.
error: unable to stat just-written file testsuite/rootfs-pristine/test-loaded/sys/module/btusb/drivers/usb:btusb: No such file or directory
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'
$ git clone   https://aosp.tuna.tsinghua.edu.cn/platform/libcore
Cloning into 'libcore'...
remote: Counting objects: 245949, done.
remote: Compressing objects: 100% (474/474), done.
remote: Total 245949 (delta 263), reused 83 (delta 32)
Receiving objects: 100% (245949/245949), 86.37 MiB | 3.97 MiB/s, done.
Resolving deltas: 100% (157977/157977), done.
error: unable to create file luni/src/test/resources/org/apache/harmony/tests/java/lang/test?.properties: Invalid argument
Checking out files: 100% (6121/6121), done.
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'

5. 备注

我下载的是android_8.1.0_r17版本的代码:
repo init -u https://aosp.tuna.tsinghua.ed... -b android_8.1.0_r1

repo的trace选项的作用
You can trace what really happens with repo

系统
Windows7

参考链接

repo和Git的关系 https://blog.csdn.net/qugenam...
WSL https://github.com/Microsoft/...
FixDownloadAnrdoid https://github.com/NyaSik/Fix...
repo sync error: cannot initialize work tree https://blog.csdn.net/ly89070...
出现error: unable to create file tests/P_str_escape/str\escape.rs的解决办法 https://blog.csdn.net/u013553...
不能在 win7 下建立命为 aux 的文件夹? http://bbs.bccn.net/m.thread....

阅读 8.9k

博客和专栏有什么区别啊
记录自己想不开的岁月

讨厌版权!模仿,人类进步的阶梯!

18 声望
2 粉丝
0 条评论
你知道吗?

讨厌版权!模仿,人类进步的阶梯!

18 声望
2 粉丝
文章目录
宣传栏