repo sync error under windows: cannot initialize work tree

created at 07-06-2021 views: 3

1. Problem description

This error occurred when downloading the Android source code

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. Problem analysis

2.1 Check what went wrong

From the log point of view, .repo/repo/project.py is the place to pay attention to, and the script can be located:
raise GitError("cannot initialize work tree")
Its function is _InitWorkTree

The complete code of _InitWorkTree is as follows:

  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

But here does not give us any effective information, so we have to continue to analyze

2.2 repo --trace sync

Use repo --trace sync -cdf to output all the actions of the repo in detail (this process may be very long and we need to wait patiently), you will find that errors here lead to:

: 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

At this point, we have located the problem.

2.3 Add printing information

Of course, when we are not familiar with the repo command, we can also add log to understand where we are currently stuck. In order to find which directory git is processing when the current error occurs, I am in _InitWorkTree
Two log messages are added to the function:

    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)
        ...
      ...
    ...

The printed log is as follows:

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

It can be seen that there is a problem in processing E:\LocalProject\Oreo\external\kmod. However, due to the limited log, we can't really understand what is currently happening.

3. Solution

The previous log says that the file could not be created. The reason is that the name of the file to be created is usb:btusb, which obviously violates the Windows naming rules. So there is actually no solution.
So I plan to bypass this problem first, download the other parts of the source code first, and solve this problem later. Since there is a problem with this project, I won't download it for now.
Open .repo/manifest/default.xml

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

After searching on the Internet, it seems that many netizens have encountered the problem of "cannot initialize work tree" when downloading code under windows. Even the repo of the Linux subsystem under win10 provided by Microsoft has problems (the last reference link has a solution to this problem). It is also a matter of path names. Interested netizens can have a look:
Microsoft: https://github.com/Microsoft/...

I also encountered the problem of cannot initialize work tree twice during this download

Error downloading "platform/libcore"

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

"Test?.properties" this is also a violation of the Windows file naming problem

Error downloading "external/libunwind"

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

This is an error in the folder named "aux", and "aux" is the Windows device name folder. AUX is used on DOS, which is the abbreviation of auxiliary device, auxiliary device. It is stated in the DOS system manual that this is a system identifier. AUX is the stream name of the asynchronous port by default. It is usually connected to the Console and belongs to the standard naming device. Therefore, you are not allowed to create files with the reserved names of the system, because the files also belong to the stream devices.

Remarks

the code of the android I downloaded  if android_8.1.0_r17.

The role of the trace option of the repo
You can trace what really happens with repo

Please log in to leave a comment.