Showing git branch in dopus

I'm using TortoiseSVN. For a folder that's managed by git, is there some way--say, an extension--that would show the current branch in dopus on the window title bar or somewhere else?

TortoiseSVN is for SVN servers. TortoiseGit is for Git servers. (Unless you're using a Git/SVN bridge to make the Git server pretend to be an SVN server.)

TortoiseSVN adds columns which work in Opus but the way branches work in SVN there would not be a column for it because you'd just be in a different folder path.

I don't know how it works with Git, or if TortoiseGit's columns work in Opus. (There was a thread recently where the TortoiseGit developers suggested that their shell extension's columns are no longer supported on Vista and above, so perhaps not.)

Sorry, slip of the brain ... I meant TortoiseGit. I use it along with TortoiseSVN for different projects, but in Git have a hard time knowing where I am in the branch structure. The Git Status column doesn't show anything.

This is an old thread but I don't see an answer. Is there a way to see the git branch easily in DOpus?

What exactly do you mean by "show the git branch"?

Are you still using TortoiseSVN rather than a Git-based tool? It will probably limit what you can do if you are using a Git/SVN bridge instead of Git directly. (Although it may not. I am not an expert on any of this. But if you can get the information via a command or column then Opus can show it somewhere.)

Let me try again. I'm using Git (specifically, TortoiseGit). When I show a Git folder in DOPus, it would be really nice if it would show the git branch somewhere. I can right-click on the folder and the Tortoise add-in will show me the branch name. So I imagine DOPus could too.

Opus can show it if TortoiseGit adds a column with the information. Opus has no built-in knowledge of Git, though, so it would need to come from TortoiseGit's shell extension (the same one that adds the context menus), or via something else. Opus can be made to display any (almost) column that is visible in Explorer, as well as columns added the old way which Explorer no longer supports (which is how TortoiseSVN does things, but TortoiseGit may be different and sounded like it was dropping support for columns, unless people complained and got it back).

You may also be able to get the information via a command or script which could populate a column using Opus's scripting support. I don't know if TortoiseGit or the Git command-line tools provide a convenient & efficient way to get that information via javascript or vbscript, but if they do then it would be easy to hook it up to an Opus column with a few lines of scripting code on top.

Hi Leo,

Is it possible to use a script to change the Window title or the status information at the bottom of each pane for a given directory?

Thanks,
Hai

Yes, for the window title. Scripts can run Set LISTERTITLE="..." to set a custom title.

Set command docs.

Simple script to display the git branch in the lister and the tab. It reads .git/HEAD directly since WScript seems to not like git being called...perhaps for security reasons.

Please offer any feedback if you think it can be improved.


function FindGitHead(path)
{
  var git_head_path = null;
  var n = path.components;
  for (var i = 0; i < n; ++i) {
    var file_path = DOpus.FSUtil.NewPath(path);
    file_path.Add(".git");
    file_path.Add("HEAD");
    if (DOpus.FSUtil.Exists(file_path)) {
      git_head_path = DOpus.FSUtil.NewPath(file_path);
      break;
    }
    path.Parent();
  }
  return git_head_path;
}

function ReatGitBranch(path)
{
  var git_branch = null;
  var fso = new ActiveXObject("Scripting.FilesystemObject");
  var file = fso.OpenTextFile(path, 1, -2);
  while (!file.AtEndOfStream) {
    var line  = file.ReadLine();
    var re    = /ref: refs\/heads\/(.+)/;
    var match = re.exec(line);
    if (match != null) {
      git_branch = match[1];
    }
  }
  file.Close();
  return git_branch;
}

function UpdateTitleAndLabel(tab)
{
  var label = tab.path.filepart;
  var git_head_path = FindGitHead(tab.path);
  if (git_head_path != null) {
    var git_branch = ReatGitBranch(git_head_path);
    if (git_branch != null) {
      label = label + " (" + git_branch + ")";
    }
  }
  // New DOpus command
  var dopus_cmd = DOpus.NewCommand();
  // Change lister title
  var cmd = "SET LISTERTITLE=\"" + label + "\""; 
  dopus_cmd.RunCommand(cmd);
  // Change tab label
  var cmd = "GO TABNAME=\"" + label + "\""; 
  dopus_cmd.RunCommand(cmd);
}

function OnActivateTab(data)
{
  UpdateTitleAndLabel(data.newtab);
}

function OnAfterFolderChange(data)
{
  if (data.result) {
    UpdateTitleAndLabel(data.tab);
  }
}
1 Like

Excellent, thanks. It shows up just fine in the tab but I haven't found how to show it in the lister. I checked all the column options but nothing that mentioned git. Am I missing something?

As I understand it, the script above puts the git branch into the main window title, and into the folder tab's label, showing the git branch for the current folder.

It doesn't add a column showing the branch of files and subfolders.

@blearyeye It's exactly as @Leo said. It wouldn't be too difficult to extend to add it to a column tho.

Hi -

Thanks a lot for the script. It works well, but I just found that it does not work when I remotely access a server via FTP (SFTP via SSH) - I kept getting connection retrying. I was wondering if you have a solution to this? I wonder if I can just disable this for ssh connections. Please let me know and thank you very much!

Here's a version which only checks paths which use a drive letter (i.e. local drives, or mapped network drives):

Git Branch Title.js.txt (2.0 KB)

// Git Branch Title

function OnInit(initData)
{
	initData.name = "Git Branch Title";
	initData.version = "1.0";
//	initData.copyright = "...";
	initData.url = "https://resource.dopus.com/t/showing-git-branch-in-dopus/18347";
	initData.desc = "Show Git branch in window title";
	initData.default_enable = true;
	initData.min_version = "12.0";
}

function FindGitHead(path)
{
  var git_head_path = null;
  if (path.drive != 0) // Leo 26/Jun/2018: Only check on local drives.
  {
    var n = path.components;
    for (var i = 0; i < n; ++i) {
      var file_path = DOpus.FSUtil.NewPath(path);
      file_path.Add(".git");
      file_path.Add("HEAD");
      if (DOpus.FSUtil.Exists(file_path)) {
        git_head_path = DOpus.FSUtil.NewPath(file_path);
        break;
      }
      path.Parent();
    }
  }
  return git_head_path;
}

function ReatGitBranch(path)
{
  var git_branch = null;
  var fso = new ActiveXObject("Scripting.FilesystemObject");
  var file = fso.OpenTextFile(path, 1, -2);
  while (!file.AtEndOfStream) {
    var line  = file.ReadLine();
    var re    = /ref: refs\/heads\/(.+)/;
    var match = re.exec(line);
    if (match != null) {
      git_branch = match[1];
    }
  }
  file.Close();
  return git_branch;
}

function UpdateTitleAndLabel(tab)
{
  var label = tab.path.filepart;
  var git_head_path = FindGitHead(tab.path);
  if (git_head_path != null) {
    var git_branch = ReatGitBranch(git_head_path);
    if (git_branch != null) {
      label = label + " (" + git_branch + ")";
    }
  }
  // New DOpus command
  var dopus_cmd = DOpus.NewCommand();
  // Change lister title
  var cmd = "SET LISTERTITLE=\"" + label + "\"";
  dopus_cmd.RunCommand(cmd);
  // Change tab label
  var cmd = "GO TABNAME=\"" + label + "\"";
  dopus_cmd.RunCommand(cmd);
}

function OnActivateTab(data)
{
  UpdateTitleAndLabel(data.newtab);
}

function OnAfterFolderChange(data)
{
  if (data.result) {
    UpdateTitleAndLabel(data.tab);
  }
}
1 Like

Thank you so much for your quick reply. The new version seems to be working well!

I saw this thread and thought I create a thread for the Git script column set I did.
Right now, just the status column is kind of useful. But maybe it is more useful than others, since it tries to be a smarter one (detecting empty folders, untracked and ignored items, move/rename-detection with source/dest paths).

Maybe you script folks like to play around with it and have some more ideas on how to enhance/debug it.
Glueing the branch thing in there, might be worth a try e.g.

This is great! Thanks for sharing these!

Hi Tbone (or anyone else) how easy would it be to incorporate the git Branch into a column? The point of this would be just to make it more obvious and harder to miss (I tend to keep my tabs on the small side).