wisetime-io / vscode-branch-in-window-title

Visual Studio Code extension that shows the current Git branch in the window title
Apache License 2.0
13 stars 6 forks source link

support worktree and sub dir #22

Closed qiyue51 closed 5 months ago

qiyue51 commented 7 months ago

add support for worktree dir and sub dir

StK88 commented 5 months ago

Hi, on macOS npm run test throws:

Error: ENOENT: no such file or directory, lstat '/var/folders/t4/3hplmlsj4z16bzyd4txtwplm0000gn/T/vscode-git-branch-detector.branchDetector.test'

Related to: https://github.com/nodejs/node/issues/11422 Fixed it by:

Screenshot 2024-04-11 at 16 18 05

Wrapped in after hook fs.rmdirSync(tmpProjectRoot, { recursive: true }), otherwise, vscode-git-branch-detector.branchDetector.test remained in the file system.

source:

// Copyright (c) 2020 WiseTime. All rights reserved.

import { after } from "mocha";
import * as assert from 'assert';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import detectBranch from '../../branchDetector';
import { TextEncoder } from 'text-encoding';

suite('Branch Detector Integration Test Suite', () => {
  const tmpProjectRoot = path.join(fs.realpathSync(os.tmpdir()), "vscode-git-branch-detector.branchDetector.test");
  if (!fs.existsSync(tmpProjectRoot)) {
    fs.mkdirSync(tmpProjectRoot);
  }
  const gitRoot = path.join(tmpProjectRoot, ".git");

  after(() => {
    if (fs.existsSync(tmpProjectRoot)) {
      fs.rmdirSync(tmpProjectRoot, { recursive: true });
    }
  });

  test("branchDidChange should be called with undefined branch if the .git/HEAD file could not be read", done => {
    detectBranch(tmpProjectRoot, 100, branchName => {
      assert(branchName === undefined, 'No branch detected');
      done();
    });
  });

  test("branchDidChange should be called with undefined branch if the .git/HEAD file does contain a branch", done => {
    writeGitHeadFile(gitRoot, '3d4986953423141f971dc69a691dbd89756cccbc');  // Some commit hash.
    detectBranch(tmpProjectRoot, 100, branchName => {
      assert(branchName === undefined, 'No branch detected');
      done();
    });
  });

  test("branchDidChange should be called with the branch name if the .git/HEAD file contains a branch", done => {
    writeGitHeadFile(gitRoot, 'ref: refs/heads/master');
    detectBranch(tmpProjectRoot, 100, branchName => {
      assert(branchName === 'master', 'A branch is detected');
      done();
    });
  });
});

function writeGitHeadFile(gitDir: string, content: string): void {
  if (!fs.existsSync(gitDir)) {
    fs.mkdirSync(gitDir, { recursive: true });
  }
  const gitHeadFilePath = path.join(gitDir, 'HEAD');
  const data = new TextEncoder().encode(content);
  fs.writeFileSync(gitHeadFilePath, data);
};

Can you please update it in your fork and check by npm run test, to make sure it didn't break tests on your side?

vyshane commented 5 months ago

Closing in favour of #23