Page 1 of 1

[IDE-Tool] GitHelper

Posted: Thu Jun 01, 2017 3:29 pm
by GPI
I tried to add a simple GIT-Support for the IDE. Theoretically it is possible to add the GIT (-gui) as a tool in the IDE, but i want some more stuff like automatically initalize a repo and so on. GitHelper also commit alls changes at once. GitHelper is designed to run in the background with as less messages as possible.

First of all GIT should be installed:
https://git-scm.com/downloads

I have tested GitHelper only with Windows, but it should be useable with minor changes under Linux/Mac.

When you start the tool the first time, a githelper.ini is created and you should check and edit the settings. In [Global] you find the name and path to the git, git-gui and gitk. "ProjectRoot" is the root of all projects monitored by my tool. GitHelper will handle every Subfolder of ProjectRoot as single project and create a git-repo in every directory, if necessary. Additional you can activate a log with DoLog. When you set DoBuildNo to 1, a file ".gitbuildno.pbi" for every project, which contain two constants (a commit-counter and the active branch).
In [Default .gitignore] is a prototype for the ".gitignore"-file. Githelper create this file every time a new repo is created.
And finally under [Environment] you can set some os-environment-variables. For Example the user name and email.

You can add GitHelper to the tool-menĂ¼ with this arguments:

Code: Select all

"%FILE" <command> <optionalparameter>
command can be one of this:
COMMIT <headline>
Adds all changed, new, deleted files in a new commit. If you don't set a headline, GitHelper will ask for it.
I use this command with the trigger "After compiler&run" with the headline "--" and "After Create Executeable" without a headline. with this settings the compile&run will automatically add a new commit with all changes in the source code.

GUI
Open the Git-Gui with the current repo.

VIEWHISTORY
Open the Git repository browser of the current branch

VIEWALLHISTORY
open the Git repository browser of all branches.

OPENLOG
open the log file

BRANCH
open the branch-window of GitHelper. You can here create, checkout, merge, rebase and delete branches. You can also commit and create tags here.

TAG <tagname>
Add a tag to the last commit of the current branch.

menu
Open a pop-up-menĂ¼ with all GitHelper command. Usefull, when you don't want add every command in the IDE-Tool-menu.

GitHelper.pb

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_OS=#PB_OS_Windows
  #DS="\"
  #editorPRG="notepad.exe"
  #gitPRG="git.exe"
  #gitguiPRG="git-gui.exe"
  #gitkPRG="gitk.exe"
CompilerElse
  #DS="/"
CompilerEndIf

#Title="GitHelper"
#GithelperINI="GitHelper.ini"
#GitLog=".gitlog.log" 
#GitBuildNoFile=".gitbuildno.pbi"
#DateFormat="[%dd.%mm.%yyyy %hh:%ii:%ss]"

Global GitPRG.s,GitGuiPRG.s,GitkPRG.s,ProjectRoot.s,DateFormat.s,DoLog,DoBuild
Global LastPRGOutput.s,active
Global Repo.s

Procedure.s SearchPath(file.s)
  Protected ENVpath.s=GetPathPart(ProgramFilename())+";"+GetEnvironmentVariable("PATH")
  Protected i,path.s
  Protected ret.s=""
  path=GetCurrentDirectory()
  Repeat
    If Right(path,1)<>#ds
      path+#ds
    EndIf
    If FileSize(path+file)<>-1
      ret=path+file
      Break
    EndIf
    i+1
    path=StringField(ENVpath,i,";")
  Until path=""
  
  ProcedureReturn ret
EndProcedure
Procedure CreateDefaultIni()    
  Protected pbConfig.s,pbSource.s
  Protected out
  pbConfig=GetEnvironmentVariable("PB_TOOL_Preferences")
  CompilerIf #PB_Compiler_OS=#PB_OS_Windows
    If pbconfig=""
      pbConfig=GetEnvironmentVariable("APPDATA")+"\PureBasic\PureBasic.prefs"
    EndIf
  CompilerEndIf
  
  pbSource=GetCurrentDirectory()
    
  If OpenPreferences(pbConfig)
    PreferenceGroup("Global")
    pbSource=ReadPreferenceString("SourceDirectory",pbSource)
    If ReadPreferenceInteger("MemorizeCursor",0)
      MessageRequester(#title,"I recommend to disable in PB IDE"+#LF$+"Preference > Editor > Memorize Cursor Position")
    EndIf
    ClosePreferences()
  EndIf
  
  out=CreateFile(#PB_Any,#GithelperINI)
  If out
    WriteStringN(out,"[Global]")
    WriteStringN(out,"git = "+SearchPath(#gitPRG))
    WriteStringN(out,"git-gui = "+SearchPath(#gitguiPRG))
    WriteStringN(out,"gitk = "+SearchPath(#gitkPRG))
    WriteStringN(out,"ProjectRoot = "+pbSource)
    WriteStringN(out,"DateFormat = "+#DateFormat)
    WriteStringN(out,"DoLog = 0")
    WriteStringN(out,"DoBuildNo = 0")
    WriteStringN(out,"")
    
    WriteStringN(out,"; https://git-scm.com/docs/gitignore")
    WriteStringN(out,"[Default .gitignore]")
    WriteStringN(out,"# Object files")
    WriteStringN(out,"*.o")
    WriteStringN(out,"*.ko")
    WriteStringN(out,"*.obj")
    WriteStringN(out,"*.elf")
    WriteStringN(out,"#")
    WriteStringN(out,"# Linker output")
    WriteStringN(out,"*.ilk")
    WriteStringN(out,"*.Map")
    WriteStringN(out,"*.exp")
    WriteStringN(out,"#")
    WriteStringN(out,"# Libraries")
    WriteStringN(out,"#*.lib")
    WriteStringN(out,"#*.a")
    WriteStringN(out,"#*.la")
    WriteStringN(out,"#*.lo")
    WriteStringN(out,"#")
    WriteStringN(out,"# Shared objects (inc. Windows DLLs)")
    WriteStringN(out,"#*.dll")
    WriteStringN(out,"#*.so")
    WriteStringN(out,"#*.so.*")
    WriteStringN(out,"#*.dylib")
    WriteStringN(out,"#")
    WriteStringN(out,"# Executables")
    WriteStringN(out,"*.exe")
    WriteStringN(out,"*.out")
    WriteStringN(out,"*.app")
    WriteStringN(out,"*.i*86")
    WriteStringN(out,"*.x86_64")
    WriteStringN(out,"*.hex")
    WriteStringN(out,"#")
    WriteStringN(out,"# Others")
    WriteStringN(out,"*.pre.pb")
    WriteStringN(out,"*.asm")
    WriteStringN(out,#GitLog)
    WriteStringN(out,".gitignore")
    WriteStringN(out,#GitBuildNoFile)
    
    WriteStringN(out,"")
    WriteStringN(out,"; https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables")
    WriteStringN(out,"[Environment]")
    WriteStringN(out,"GIT_EDITOR = "+#DQUOTE$+SearchPath(#editorPRG)+#DQUOTE$)
    WriteStringN(out,"GIT_AUTHOR_NAME = "+UserName())
    WriteStringN(out,"GIT_AUTHOR_EMAIL = pb@pb.pb")
    WriteStringN(out,"GIT_AUTHOR_DATE = "+Str(Date()))
    CloseFile(out)
  EndIf
EndProcedure

Global LogOut
Procedure CloseLog()
  If LogOut
    CloseFile(LogOut)
    LogOut=0
  EndIf
EndProcedure
Procedure OpenLog(wdir.s)
  CloseLog()
  If DoLog
    LogOut=OpenFile(#PB_Any,wdir+#GitLog,#PB_File_Append|#PB_File_SharedRead|#PB_File_SharedWrite|#PB_File_NoBuffering)
  EndIf  
EndProcedure
Procedure AddLog(txt.s)
  Debug txt
  If LogOut
    WriteStringN(LogOut,FormatDate(DateFormat,Date())+txt)    
  EndIf
EndProcedure

Procedure CreateGitIgnore(path.s)
  Protected out
  out=CreateFile(#PB_Any,path+".gitignore")
  If out
    
    If OpenPreferences(#GithelperINI)
      PreferenceGroup("Default .gitignore")
      
      If ExaminePreferenceKeys()
        While  NextPreferenceKey()
          WriteStringN(out,PreferenceKeyName())
        Wend
      EndIf
      ClosePreferences()
      
    EndIf
    CloseFile(out)
  EndIf
EndProcedure

Procedure.i RunPRG(prg.s,cmd.s,wdir.s)
  Protected prgid
  Protected output.s,exitcode
  
  OpenLog(wdir)
  
  LastPRGOutput=""
  
  prgid = RunProgram(prg, cmd, wdir, #PB_Program_Open | #PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  AddLog("[RunProgram]"+prg+" "+cmd+" "+wdir)
  
  If prgid
    While ProgramRunning(prgid)
      If AvailableProgramOutput(prgid)
        Output=ReadProgramString(prgid) 
        AddLog("[Output]"+output)
        LastPRGOutput+output+#LF$
      EndIf
    Wend
    Repeat
      output=ReadProgramError(prgid)
      If output=""
        Break
      EndIf
      LastPRGOutput+output+#LF$
      AddLog("[Error]"+output)
    ForEver  
    exitcode=ProgramExitCode(prgid)
    AddLog("[ExitCode]"+exitcode)
    
    CloseProgram(prgid) ; Close the connection to the program
  EndIf
  
  CloseLog()
  ProcedureReturn exitcode
EndProcedure
Procedure.s GetDefaultBranch()
  Protected i,branch.s,ret.s
  
  If RunPRG(GitPRG,"branch -a --no-color",ProjectRoot+repo+#ds)=0
    i=0
    Repeat
      i+1
      branch=StringField(LastPRGOutput,i,#LF$)
      If branch="":Break:EndIf
      If Left(branch,1)="*"
        ret=Mid(branch,3)
        Break
      EndIf
    ForEver
  EndIf
  ProcedureReturn ret
EndProcedure
Procedure Commit(message.s)
  Protected check
  Protected no.q,add.s
  Protected ret=1
  
  If message=""
    check=#True
    message=InputRequester(#title,"Enter Commit Message for "+repo,"")
  EndIf
  
  If message<>""
    If RunPRG(GitPRG,"add --all",ProjectRoot+repo+#ds)=0
      
      If DoBuild
        OpenPreferences(ProjectRoot+repo+#ds+#GitBuildNoFile)
        no=ReadPreferenceQuad("#GitBuildNo",1)
        ;WritePreferenceQuad("#GitBuildNo",no)
        ClosePreferences()
        add="["+Str(no)+"] "
      EndIf  
      
      If RunPRG(GitPRG,"commit -m "+#DQUOTE$+add+message+#DQUOTE$,ProjectRoot+repo+#ds)=0
        ret=0
      ElseIf FindString(LastPRGOutput,"nothing to commit, working tree clean")
        If check=#False
          ret=0
        ElseIf MessageRequester(#title,"Commit fail:"+#LF$+#LF$+LastPRGOutput+#LF$+"Allow Empty?",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes
          ret= RunPRG(GitPRG,"commit --allow-empty -m "+#DQUOTE$+message+#DQUOTE$,ProjectRoot+repo+#ds)
        EndIf
      EndIf      
      
    EndIf
    
    If ret
      MessageRequester(#title,"Can't commit."+#LF$+LastPRGOutput)
    ElseIf DoBuild
      OpenPreferences(ProjectRoot+repo+#ds+#GitBuildNoFile)
      WritePreferenceQuad("#GitBuildNo",no+1)
      ClosePreferences()      
    EndIf
      
  EndIf
EndProcedure

Procedure Tag(tag.s)
  If tag=""
    tag=InputRequester(#title,"Enter Tag","")
  EndIf 
  If tag<>""
    If RunPRG(GitPRG,"tag "+#DQUOTE$+tag+#DQUOTE$,ProjectRoot+repo+#ds)
      
      If FindString(LastPRGOutput,"already exists")
        If MessageRequester(#title,"Can't add a Tag:"+#LF$+#LF$+LastPRGOutput+#LF$+"Replace Tag?",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes
          If RunPRG(GitPRG,"tag -f "+#DQUOTE$+tag+#DQUOTE$,ProjectRoot+repo+#ds)
            MessageRequester(#title,"Can't add a Tag."+#LF$+LastPRGOutput)
          EndIf
        EndIf
      Else
        
        MessageRequester(#title,"Can't add a Tag."+#LF$+LastPRGOutput)
      EndIf
      
    EndIf
  EndIf
EndProcedure


Enumeration menu
  #mNew
  #mCheckout
  #mDelete
  #mForceDelete
  #mExit
  #mMerge
  #mRename
  #mCommit
  #mContext
  #mRebase
  #mRebaseContinue
  #mRebaseAbort
  
  #mRefresh
    
  #mBranch
  #mGui
  #mViewHistory
  #mViewAllHistroy
  #mOpenLog
  #mTag
EndEnumeration

Procedure ShowFile(file.s)
  Protected IDE.s
  Protected ext.s
  Protected in,lineNb,line.s,res
  ide=GetEnvironmentVariable("PB_TOOL_IDE")
  
  CompilerIf #PB_Compiler_Debugger
    If ide=""
      If FileSize("C:\Program Files\PureBasic\PureBasic.exe")>0
        ide="C:\Program Files\PureBasic\PureBasic.exe"
        Debug "FALLBACK FIND IDE"
      EndIf
    EndIf
  CompilerEndIf
  
  If ide<>""
    ext=UCase(GetExtensionPart(file))
    If ext="PB" Or ext="PBI"
      in= ReadFile(#PB_Any,file,#PB_File_SharedRead|#PB_File_SharedWrite)
      
      If in
        While Not Eof(in)
          line=ReadString(in)
          lineNb+1
          If line = "======="
            res=lineNb
            Break
          EndIf          
        Wend
        CloseFile(in)
        
        
        If res
          RunProgram(ide,#DQUOTE$+file+#DQUOTE$+" /L "+Str(res),GetFilePart(ide))
        Else
          RunProgram(ide,#DQUOTE$+file+#DQUOTE$,GetFilePart(ide))
        EndIf
      EndIf
      
    Else
      RunProgram(file)
    EndIf
    
  Else
    RunProgram(file)
  EndIf
EndProcedure

Procedure SetBuildBranch(branch.s)
  Protected set.s
  If DoBuild
    OpenPreferences(ProjectRoot+repo+#ds+#GitBuildNoFile)
    set=ReadPreferenceString("#GitBranch","")
    If set<>#DQUOTE$+branch+#DQUOTE$
      WritePreferenceString("#GitBranch",#DQUOTE$+branch+#DQUOTE$)
    EndIf
    ClosePreferences()
  EndIf
EndProcedure


Procedure Branch_Switch(gBranch,gOutput,NewActive)
  Protected branch.s,state.i
  branch=GetGadgetItemText(gBranch,NewActive)
  If runprg(GitPRG,"checkout "+#DQUOTE$+branch+#DQUOTE$,ProjectRoot+repo+#ds)=0   
    
    state=GetGadgetItemState(gBranch,active)
    SetGadgetItemState(gBranch,active,state & ~#PB_ListIcon_Checked)
    active=NewActive
    state=GetGadgetItemState(gBranch,active)
    SetGadgetItemState(gBranch,active,state | #PB_ListIcon_Checked)
    SetBuildBranch(branch)
  Else
    MessageRequester(#Title,"Cant't switch to "+branch+#LF$+LastPRGOutput)
    state=GetGadgetItemState(gBranch,active)
    If state&#PB_ListIcon_Checked=0
      SetGadgetItemState(gBranch,active,state|#PB_ListIcon_Checked)
    EndIf
  EndIf
  
  SetGadgetText(gOutput,LastPRGOutput)
  
  ProcedureReturn active
EndProcedure
Procedure Branch_FillStatus(gStatus)
  Protected i,status.s
  ClearGadgetItems(gStatus)
  If RunPRG(GitPRG,"status -s",ProjectRoot+repo+#ds)=0
    i=0
    Repeat
      i+1
      status=StringField(LastPRGOutput,i,#LF$)
      If status=""
        If i=1
          AddGadgetItem(gStatus,-1,"-"+#LF$+"-"+#LF$)
        EndIf
        Break
      EndIf
      AddGadgetItem(gStatus,-1,
                    Left(status,1)+#LF$+
                    Mid(status,2,1)+#LF$+
                    Mid(status,4))
      SetGadgetItemColor(gStatus,i-1,#PB_Gadget_FrontColor,RGB(0,$70,0),0)
      SetGadgetItemColor(gStatus,i-1,#PB_Gadget_FrontColor,RGB($70,0,0),1)
    ForEver
   
  EndIf
EndProcedure

Procedure Branch_FillList(gBranch)
  Protected i,branch.s
  Protected info.s
  ClearGadgetItems(gBranch)
  If RunPRG(GitPRG,"branch -a --no-color",ProjectRoot+repo+#ds)=0
    i=0
    Repeat
      i+1
      info=StringField(LastPRGOutput,i,#LF$)
      branch=Mid(info,3)
      If branch="":Break:EndIf
      If Left(info,1)="*"
        active.i=i-1
        
        SetBuildBranch(branch)        
        
      EndIf
      AddGadgetItem(gBranch,-1,branch)
    ForEver
    
    SetGadgetState(gBranch,active)
    SetGadgetItemState(gBranch,active,#PB_ListIcon_Checked|#PB_ListIcon_Selected)
    SetActiveGadget(gBranch)
  EndIf
  ProcedureReturn active
EndProcedure
Procedure Branch_ShowMenu(win,gBranch,PM_Entry,PM_Empty,PM_Rebase)
  Protected state=GetGadgetState(gBranch)
  If Left(GetGadgetItemText(gBranch,0),20)="(no branch, rebasing"
    DisplayPopupMenu(PM_Rebase,WindowID(win))
  ElseIf state>=0
    DisableMenuItem(PM_Entry,#mDelete,Bool(state=active))
    DisableMenuItem(PM_Entry,#mForceDelete,Bool(state=active))
    DisableMenuItem(PM_Entry,#mMerge,Bool(state=active))
    DisableMenuItem(PM_Entry,#mCheckout,Bool(state=active))
    DisableMenuItem(PM_Entry,#mRebase,Bool(state=active))
    DisableMenuItem(PM_Entry,#mCommit,Bool(Not (state=active)))
    DisableMenuItem(PM_Entry,#mTag,Bool(Not (state=active)))
    SetMenuItemText(PM_Entry,#mNew,"New Branch from "+GetGadgetText(gBranch))
    SetMenuItemText(PM_Entry,#mViewHistory,GetGadgetText(gBranch))
    DisplayPopupMenu(PM_Entry,WindowID(win))
  Else
    DisplayPopupMenu(PM_Empty,WindowID(win))
  EndIf
EndProcedure
Procedure Branch_DoGit(gbranch,gOutput,Cmd.s,err.s)
  If RunPRG(GitPRG,cmd,ProjectRoot+repo+#ds)=0
    SetGadgetText(gOutput,LastPRGOutput)
    Branch_FillList(gBranch)
    ProcedureReturn #False
  Else
    SetGadgetText(gOutput,LastPRGOutput)
    MessageRequester(#Title,err+#LF$+LastPRGOutput)
    ProcedureReturn #True
  EndIf
EndProcedure

Procedure DoBranch()
  Protected Win
  Protected gBranch,gOutput,gHSplitter,gVSplitter,gStatus
  Protected i,branch.s,NewName.s,NewActive.i,file.s
  Protected event,state
  Protected PM_Empty
  Protected PM_Entry
  Protected PM_rebase
  Protected PM_status
  Protected w,h,sv,sh
  
  ;{ Create Popup menu
  PM_status=CreatePopupMenu(#PB_Any)
  MenuItem(#mRefresh,"Refresh")
  
  pm_rebase=CreatePopupMenu(#PB_Any)
  MenuItem(#mRebaseContinue,"Continue")
  MenuItem(#mRebaseAbort,"Abort")
    
  pm_empty=CreatePopupMenu(#PB_Any)
  MenuItem(#mNew,"New Branch")
  MenuItem(#mViewAllHistroy,"View History (All)")
  MenuItem(#mGui,"Open GUI")
  
  PM_Entry=CreatePopupMenu(#PB_Any)
  MenuItem(#mCheckout,"Checkout")
  MenuItem(#mNew,"New Branch from this")
  MenuItem(#mRename,"Rename")
  OpenSubMenu("Delete")
  MenuItem(#mDelete,"Confirm")
  OpenSubMenu("Force")
  MenuItem(#mForceDelete,"Confirm")
  CloseSubMenu()
  CloseSubMenu()
  OpenSubMenu("Merge")
  MenuItem(#mMerge,"Confirm")
  CloseSubMenu()
  OpenSubMenu("Rebase")
  MenuItem(#mRebase,"Confirm")
  CloseSubMenu()
  OpenSubMenu("View History")
  MenuItem(#mViewHistory,"")
  MenuItem(#mViewAllHistroy,"All")
  CloseSubMenu()
  MenuBar()
  MenuItem(#mCommit,"Commit")
  MenuItem(#mTag,"Tag")
  MenuItem(#mGui,"Open GUI")
  ;}
  
  OpenPreferences(#GithelperINI)
    PreferenceGroup("Window")
    w=ReadPreferenceInteger("width",400)
    h=ReadPreferenceInteger("height",300)
    sh=ReadPreferenceInteger("SplitterH",300-10-70)
    sv=ReadPreferenceInteger("SplitterV",(400-10)/2)
  ClosePreferences()  
  
  
  ;If RunPRG(GitPRG,"branch -a --no-color",ProjectRoot+repo+#ds)=0
    win=OpenWindow(#PB_Any,0,0,w,h,#Title+" - "+repo,#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_SizeGadget)
    If win
      
      AddKeyboardShortcut(win,#PB_Shortcut_Return,#mCheckout)
      AddKeyboardShortcut(win,#PB_Shortcut_Escape,#mExit)
      AddKeyboardShortcut(win,#PB_Shortcut_Apps,#mContext)
      AddKeyboardShortcut(win,#PB_Shortcut_F5,#mRefresh)
      
      gBranch=ListIconGadget(#PB_Any,0,0,W,h,"Branch",w-40,#PB_ListIcon_CheckBoxes|#PB_ListIcon_AlwaysShowSelection|#PB_ListIcon_FullRowSelect)
      
      gOutput=EditorGadget(#PB_Any,0,0,w,h,#PB_Editor_ReadOnly)
      
      gStatus=ListIconGadget(#PB_Any,0,0,w,h,"X",20,#PB_ListIcon_FullRowSelect)
      AddGadgetColumn(gStatus,1,"Y",20)
      AddGadgetColumn(gStatus,2,"File",40)
      
      gVSplitter=SplitterGadget(#PB_Any,5,5,w,h,gBranch,gStatus,#PB_Splitter_Vertical)
      
      
      gHSplitter=SplitterGadget(#PB_Any,
                                5,5,
                                WindowWidth(win)-10,WindowHeight(win)-10,
                                gVSplitter,gOutput,
                                #PB_Splitter_SecondFixed)
      
      SetGadgetState(gHSplitter,sh)
      SetGadgetState(gVSplitter,sv)
      
      
      ;active=Branch_FillList(gBranch)
      ;Branch_FillStatus(gStatus)
      
      SetGadgetColor(gOutput,#PB_Gadget_BackColor,RGB($e0,$e0,$e0))
      SetGadgetColor(goutput,#PB_Gadget_FrontColor,RGB($0,$0,$0))
      
      SetGadgetColor(gBranch,#PB_Gadget_BackColor,RGB($F0,$F0,$F0))
      SetGadgetColor(gBranch,#PB_Gadget_FrontColor,RGB($0,$0,$0))
      
      SetGadgetColor(gStatus,#PB_Gadget_BackColor,RGB($F0,$F0,$F0))
      SetGadgetColor(gStatus,#PB_Gadget_FrontColor,RGB($0,$0,$0))
            
      Repeat
        event=WaitWindowEvent()
        Select event
          Case #PB_Event_ActivateWindow
            Branch_FillList(gBranch)
            Branch_FillStatus(gStatus)
            
          Case #PB_Event_SizeWindow
            ResizeGadget(gHSplitter,
                         5,5,
                         WindowWidth(win)-10,WindowHeight(win)-10)
            
            
          Case #PB_Event_Menu ;{ Menu
            Select EventMenu()
              Case #mRefresh
                Branch_FillStatus(gStatus)
                
              Case #mContext
                Branch_ShowMenu(win,gBranch,PM_Entry,PM_Empty,PM_Rebase)
                
              Case #mViewAllHistroy
                RunProgram(GitkPRG,"--all",ProjectRoot+repo+#ds)
                
              Case #mViewHistory
                branch=GetGadgetText(gBranch)
                If branch
                  RunProgram(GitkPRG,#DQUOTE$+branch+#DQUOTE$,ProjectRoot+repo+#ds)
                Else
                  RunProgram(GitkPRG,"",ProjectRoot+repo+#ds)
                EndIf
                
              Case #mGui
                RunProgram(GitGuiPRG,"",ProjectRoot+repo+#ds)
                
              Case #mExit
                Break
                
              Case #mCheckout
                NewActive=GetGadgetState(gBranch)
                If NewActive<>active And NewActive>=0
                  active=Branch_Switch(gBranch,gOutput,NewActive)                    
                EndIf
                
              Case #mNew
                NewActive=GetGadgetState(gBranch)
                If NewActive>=0 And NewActive<>active
                  active=Branch_Switch(gBranch,gOutput,NewActive)
                EndIf
                If active=NewActive
                  branch=InputRequester(#title,"New Branch","")
                  If branch<>""
                    branch_DoGit(gBranch,gOutput,"checkout -b "+#DQUOTE$+branch+#DQUOTE$,"Can't create branch.")
                  EndIf
                EndIf
                
              Case #mRename
                branch=GetGadgetText(gBranch)
                If branch
                  NewName=InputRequester(#Title,"Rename branch "+branch,branch)
                  If NewName<>branch
                    Branch_DoGit(gBranch,gOutput,"branch -m "+#DQUOTE$+branch+#DQUOTE$+" "+#DQUOTE$+NewName+#DQUOTE$,"Can't rename branch.")
                  EndIf
                EndIf                
                
              Case #mForceDelete
                branch=GetGadgetText(gBranch)
                If branch<>""
                  Branch_DoGit(gBranch,gOutput,"branch -D "+#DQUOTE$+branch+#DQUOTE$,"Can't delete branch (force).")
                EndIf
                
              Case #mDelete
                branch=GetGadgetText(gBranch)
                If branch<>""
                  Branch_DoGit(gBranch,gOutput,"branch -d "+#DQUOTE$+branch+#DQUOTE$,"Can't delete branch.")
                EndIf
                
              Case #mMerge
                state=GetGadgetState(gbranch)
                If state<>active And state>=0
                  branch=GetGadgetText(gBranch)
                  Branch_DoGit(gBranch,gOutput,"merge "+#DQUOTE$+branch+#DQUOTE$,"Can't merge branch.")
                EndIf
                
              Case #mRebase
                branch=GetGadgetText(gBranch)
                If branch<>""
                  If Branch_DoGit(gBranch,gOutput,"rebase "+#DQUOTE$+branch+#DQUOTE$,"Can't rebase branch.")
                    active=Branch_FillList(gBranch)
                    Branch_FillStatus(gStatus)
                  EndIf
                EndIf  
                
              Case #mRebaseAbort
                Branch_DoGit(gBranch,gOutput,"rebase --abort","Can't abort rebase")
                Branch_FillStatus(gStatus)
                
              Case #mRebaseContinue
                If Branch_DoGit(gBranch,gOutput,"add --all","Can't add all.")=0
                  Branch_DoGit(gBranch,gOutput,"rebase --continue ","Can't continue rebase")
                  Branch_FillStatus(gStatus)
                EndIf
                
              Case #mTag
                If GetGadgetState(gBranch)=active
                  tag("")
                  SetGadgetText(gOutput,LastPRGOutput)
                EndIf
                
              Case #mCommit
                If GetGadgetState(gBranch)=active
                  Commit("")
                  SetGadgetText(gOutput,LastPRGOutput)
                  Branch_FillStatus(gStatus)
                EndIf
                
            EndSelect            
            ;}
                       
          Case #PB_Event_Gadget;{ Gadgets
            Select EventGadget()
              Case gStatus
                Select EventType()
                  Case #PB_EventType_RightClick
                    DisplayPopupMenu(PM_status,WindowID(win))
                    
                  Case #PB_EventType_LeftDoubleClick
                    state=GetGadgetState(gStatus)
                    If state>=0
                      file=GetGadgetItemText(gStatus,state,2)
                      If file<>""
                        ShowFile(ProjectRoot+repo+#ds+file)
                        ;runProgram(ProjectRoot+repo+#ds+file)
                      EndIf
                      
                    EndIf
                    
                EndSelect
                
                
              Case gVSplitter
                SetGadgetItemAttribute(gBranch,0,#PB_ListIcon_ColumnWidth,GadgetWidth(gBranch)-40,0)
                SetGadgetItemAttribute(gStatus,0,#PB_ListIcon_ColumnWidth,GadgetWidth(gStatus)-40-20-20,2)
                
              Case gBranch
                Select EventType()
                  Case #PB_EventType_RightClick
                    Branch_ShowMenu(win,gBranch,PM_Entry,PM_Empty,PM_Rebase)
                    
                  Case #PB_EventType_LeftDoubleClick
                    NewActive=GetGadgetState(gBranch)
                    If NewActive>=0 And NewActive<>active
                      active=Branch_Switch(gBranch,gOutput,NewActive)
                    EndIf
                    
                  Case #PB_EventType_Change
                    NewActive=Active
                    For i=0 To CountGadgetItems(gBranch)-1
                      state=GetGadgetItemState(gBranch,i)
                      If state&#PB_ListIcon_Checked And active<>i
                        NewActive=i
                        SetGadgetItemState(gBranch,i,state & ~#PB_ListIcon_Checked)
                      EndIf  
                    Next
                    If NewActive=active
                      state=GetGadgetItemState(gBranch,active)
                      If state&#PB_ListIcon_Checked=0
                        SetGadgetItemState(gBranch,active,state|#PB_ListIcon_Checked)
                      EndIf
                    Else
                      active=Branch_Switch(gBranch,gOutput,NewActive)
                    EndIf  
                EndSelect
            EndSelect
            ;}
            
        EndSelect
        
      Until event=#PB_Event_CloseWindow
      If WindowWidth(win)<>w Or WindowHeight(win)<>h Or GetGadgetState(gHSplitter)<>sh Or GetGadgetState(gVSplitter)<>sv
        OpenPreferences(#GithelperINI)
        PreferenceGroup("Window")
        WritePreferenceInteger("width",WindowWidth(win))
        WritePreferenceInteger("height",WindowHeight(win))
        WritePreferenceInteger("SplitterH",GetGadgetState(gHSplitter))
        WritePreferenceInteger("SplitterV",GetGadgetState(gVSplitter))
        ClosePreferences()  
      EndIf
          
      CloseWindow(win)
      
    EndIf
    
  ;EndIf  
EndProcedure

Procedure.s DoMenu()
  Protected win
  Protected pm_main
  Protected event
  Protected command.s

  pm_main=CreatePopupMenu(#PB_Any)
  MenuItem(#mCommit,"Commit")
  MenuItem(#mTag,"Tag")
  MenuItem(#mBranch,"Branch")
  MenuItem(#mGui,"Gui")
  OpenSubMenu("View History")
  MenuItem(#mViewHistory,GetDefaultBranch())
  MenuItem(#mViewAllHistroy,"All")
  CloseSubMenu()
  If DoLog
    MenuItem(#mOpenLog,"Open Log")
  EndIf  
  win=OpenWindow(#PB_Any,0,0,10,10,"dummy",#PB_Window_Invisible)
  If win
    While WindowEvent():Wend
    DisplayPopupMenu(pm_main,WindowID(win))
    Repeat
      event=WaitWindowEvent(1000)
      If event=0
        Break
      EndIf
      If event=#PB_Event_Menu 
        Select EventMenu()
          Case #mTag:command="TAG"            
          Case #mCommit:command="COMMIT"
          Case #mBranch:command="BRANCH"
          Case #mGUI:command="GUI"
          Case #mViewHistory:command="VIEWHISTORY"
          Case #mViewAllHistroy:Command="VIEWALLHISTORY"
          Case #mOpenLog:command="OPENLOG"
        EndSelect
        Break
      EndIf
    ForEver
    CloseWindow(win)
  EndIf
  ProcedureReturn command
EndProcedure

;- Create ini
If FileSize(#GithelperINI)<=0
  CreateDefaultIni()
  RunProgram(#GithelperINI,"",GetCurrentDirectory(),#PB_Program_Wait)  
EndIf

;- Read ini
If OpenPreferences(#GithelperINI)
  PreferenceGroup("Global")
  GitPRG=ReadPreferenceString("git","")
  GitGuiPRG=ReadPreferenceString("git-gui","")
  GitkPRG=ReadPreferenceString("gitk","")
  ProjectRoot=ReadPreferenceString("ProjectRoot","")
  DateFormat=ReadPreferenceString("DateFormat",#DateFormat)
  DoLog=ReadPreferenceInteger("DoLog",99)
  If DoLog=99
    DoLog=1
    WritePreferenceInteger("DoLog",dolog)
  EndIf
  DoBuild=ReadPreferenceInteger("DoBuildNo",99)
  If DoBuild=99
    DoBuild=0
    WritePreferenceInteger("DoBuildNo",DoBuild)
  EndIf
  
  PreferenceGroup("Environment")
  If ExaminePreferenceKeys()
    While  NextPreferenceKey() ; While a key exists
      SetEnvironmentVariable(PreferenceKeyName(),PreferenceKeyValue())
    Wend
  EndIf    
  ClosePreferences()
      
EndIf


;-Get Repo name
Define SourceFile.s
SourceFile=ProgramParameter()
If Left(SourceFile,Len(ProjectRoot))=ProjectRoot
  repo= Mid(SourceFile,Len(ProjectRoot)+1)
  repo=Left(repo,FindString(repo,#ds)-1)
EndIf
If repo=""
  Debug "no repo"
  End
EndIf  

;-Git already initalised?
If FileSize(ProjectRoot+repo+#ds+".git")<>-2
  CreateGitIgnore(ProjectRoot+repo+#ds)
  If RunPRG(GitPRG,"init",ProjectRoot+repo+#ds)<>0
    MessageRequester(#Title,"Can't initalize git, see log.")
    End
  Else
    commit("Initial Version")
    SetBuildBranch(GetDefaultBranch())
  EndIf
EndIf

;-Command
Define command.s=ProgramParameter() 
  
If UCase(command)="MENU"
  command=DoMenu()
EndIf

Select UCase(command)
  Case "COMMIT"
    commit(ProgramParameter())
  Case "GUI"
    RunProgram(GitGuiPRG,"",ProjectRoot+repo+#ds)
  Case "VIEWHISTORY"
    RunProgram(GitkPRG,GetDefaultBranch(),ProjectRoot+repo+#ds)
  Case "VIEWALLHISTORY"
    RunProgram(GitkPRG,"--all",ProjectRoot+repo+#ds)
  Case "OPENLOG"
    RunProgram(#GitLog)
  Case "BRANCH"
    DoBranch()
  Case "TAG"  
    tag(ProgramParameter())
  Case ""
    End
  Default
    MessageRequester(#Title,"Unkown command"+#LF$+command)
EndSelect


Re: [IDE-Tool] GitHelper

Posted: Sun Jun 04, 2017 11:01 pm
by kenmo
Thanks for this, GPI. :)

I use Mercurial more than Git, and I started a similar Mercurial tool for the IDE... never finished it though