35

I'd like to automatically checkout a file when I start to edit it in Delphi 7 IDE.

ClearCase is my version control system and I really hate the need to checkout a file before starting to edit. It always breaks my thought flow: I'm trying to solve a problem, find where I need to change, try to edit it, fail because the file is read only, open clearcase, search the file, finally checkout, try to edit the file again, fail because the IDE still thinks it is readonly, tell the IDE that isn't readonly. When I finally go back to code, I forgot what I was trying do do.

I've found this nice and simple ClearCase IDE integration code. It works, but uses the deprecated ToolIntf unit. I've added a couple of shortcuts. Here is a simplified version of it (didn't try to compile):

unit clearcase;

interface
uses ToolsApi, ToolIntf;

implementation
uses
  Windows, Dialogs, Classes, ExptIntf, Menus, ShellApi, SysUtils;

type
  TDelphiClearcase = class
  private
    FClearcaseMenu,
    FDoCheckOutPasDfm,
    FDoCheckInPasDfm : TIMenuItemIntf;

    procedure ExecCommand(const command: string; path: PChar = nil);
  public
    destructor Destroy;override;
    procedure DoClick(Sender: TIMenuItemIntf);
    property ClearcaseMenu: TIMenuItemIntf read FClearcaseMenu write FClearcaseMenu;
    property DoCheckOutPasDfm:TIMenuItemIntf write FDoCheckOutPasDfm;
    property DoCheckInPasDfm: TIMenuItemIntf write FDoCheckInPasDfm;
  end;

var
  dcc: TDelphiClearcase = nil;

{ TDelphiClearcase }

destructor TDelphiClearcase.Destroy;
  procedure Remove(item: TIMenuItemIntf);
  begin
    if( item = nil )then
      Exit;
    item.DestroyMenuItem;
    FreeAndNil(item);
  end;
begin
  Remove(FDoCheckOutPasDfm);
  Remove(FDoCheckInPasDfm);
  inherited;
end;

procedure TDelphiClearcase.DoClick(Sender: TIMenuItemIntf);
  function GetPasDfm(const f: string): string;
  var
    aux: string;
  begin
    aux := Copy(f, 1, Length(f) - 4);
    Result := aux + '.pas' + ' ' + aux + '.dfm'
  end;

var
  command, fileName  : string;
begin
  fileName := ToolServices.GetCurrentFile;

  if( Sender = FDoCheckOutPasDfm )then
    command := 'cleartool co ' + GetPasDfm(fileName)
  else if( Sender = FDoCheckInPasDfm )then
    command := 'cleartool ci ' + GetPasDfm(fileName);

  ExecCommand(command);

  ToolServices.ReloadFile(fileName);

end;

procedure TDelphiClearcase.ExecCommand(const command: string; path: PChar);
var
  pi  : TProcessInformation;
  stinfo : TStartupInfo;
begin
  FillChar(stinfo, SizeOf(stinfo), 0);
  stinfo.cb := SizeOf(stinfo);

  if( CreateProcess(nil, PChar(command), nil, nil, True, CREATE_NEW_CONSOLE,
      nil, path, stinfo, pi) )then begin
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess)
  end
end;

procedure CreateMenus;
var
  services: TIToolServices;
begin
  if( BorlandIDEServices = nil )then
    Exit;
  services := ToolServices;

  if( services = nil )then
    Exit;

  dcc := TDelphiClearcase.Create;

  dcc.ClearcaseMenu := services.GetMainMenu.GetMenuItems.InsertItem(6,
    'C&learcase', 'ClearcaseMenu', 'ClearcaseTools', 0, 0, 0,
    [mfEnabled, mfVisible], nil);


  dcc.DoCheckOutPasDfm := dcc.ClearcaseMenu.InsertItem(2,
    'Check Out pas and dfm', 'DoCheckOutPasDfm', 'Undo the check outs', ShortCut(Ord('>'),
    [ssCtrl]), 0, 2,
    [mfEnabled, mfVisible], dcc.DoClick);
  dcc.DoCheckInPasDfm:= dcc.ClearcaseMenu.InsertItem(4,
    'Check In pas and dfm', 'DoCheckInPasDfm', 'Check in current files', ShortCut(Ord('<'),
    [ssCtrl]), 0, 2,
    [mfEnabled, mfVisible], dcc.DoClick);

end;

procedure DestroyMenus;
begin
  FreeAndNil(dcc);
end;

initialization
  CreateMenus;

finalization
  DestroyMenus
end.

I'd like to checkout the file when I first start editing it and it is read only. I have no idea how to hook a function to the IDE edit event of a file. Any pointers are welcome.

Ken White
  • 117,855
  • 13
  • 197
  • 405
neves
  • 20,547
  • 15
  • 108
  • 137
  • 2
    One possible *workaround* might be to have your projects managed in git, make your changes locally and push your changes at the end of the day into your Clearcase repository. No need to checkout files during the day and no need to change readonly flags. You could take a look at following question for more relevant information http://stackoverflow.com/questions/2342131/how-to-bridge-git-to-clearcase – Lieven Keersmaekers Jul 12 '11 at 21:10
  • 6
    Holy sh*t, just found this question: http://stackoverflow.com/questions/908942/how-to-detect-if-file-in-ide-is-being-edited-using-open-tools-api – neves Jul 12 '11 at 21:28
  • 4
    First of all you shouldn't be using a version control system that forces you to check files out, simply because there are alternatives. Try Suversion, Git, Mercurial. If you really are stuck with Clearcase, you really don't want to accidentally check files out, so you shouldn't try to check them out automatically. – Cosmin Prund Jul 13 '11 at 04:40
  • @Cosmin: I agree with you that you shouldn't try to check stuff out automatically (I actually prefer vcs's that force you to check things out). That's why we have created a "simple" menu expert. Checking out a file is now a matter of a keyboard short-cut to the menu item. The expert recognizes when it is in a dpr or a pas file with a dfm and it will check out both dfm and pas, or both dpr and dproj as applicable. @neves: The shortcut key is engrained in my muscle memory so checking out a file no longer interrupts my flow of thought. – Marjan Venema Jul 13 '11 at 06:22
  • @Cosmin: Other vc systems are out of question :-( – neves Jul 13 '11 at 19:08
  • @Marjan: We are using UCM and each developer will work in his own stream. Checkout is just a nuisance. – neves Jul 14 '11 at 14:32
  • This probably doesn't apply to Delphi 7, but may still be interesting: http://sourceforge.net/projects/radstudioverins/ It's the Version Insight project for Delphi XE. – Kaos Aug 05 '11 at 08:31
  • 2
    I have to use ClearCase. We use it only for integration; for the day-to-day work we use Mercurial. – Leonardo Herrera Aug 17 '11 at 19:23

2 Answers2

1

Aternative to writing API or the like is to simply use snapshot views and automatically write files using "Highjack" functionality ...then just check'em in later.

JimZ
  • 147
  • 1
  • 8
  • This isn't the answer to what I asked, but you are right. Hijacked files are the only reasonable way to work with ClearCase. – neves Jul 08 '13 at 19:00
0

Alternatively you can use the open ToolsAPI to listen for changes in the editor and checkout the file when the user has changed any of the content in the file.

Rahul W
  • 833
  • 11
  • 24