OSDN Git Service

BrowseRefs: Added 'Switch to this Ref' option
authorJohan 't Hart <johanthart@gmail.com>
Sat, 27 Jun 2009 21:11:18 +0000 (23:11 +0200)
committerJohan 't Hart <johanthart@gmail.com>
Sat, 27 Jun 2009 21:11:18 +0000 (23:11 +0200)
src/Git/Git.cpp
src/Git/Git.h
src/TortoiseProc/AppUtils.cpp
src/TortoiseProc/AppUtils.h
src/TortoiseProc/BrowseRefsDlg.cpp
src/TortoiseProc/BrowseRefsDlg.h
src/TortoiseProc/ChooseVersion.h

index a209a53..35c5a65 100644 (file)
@@ -358,6 +358,17 @@ CString CGit::GetSymbolicRef(const wchar_t* symbolicRefName, bool bStripRefsHead
        return refName;\r
 }\r
 \r
+CString CGit::GetFullRefName(CString shortRefName)\r
+{\r
+       CString refName;\r
+       CString cmd;\r
+       cmd.Format(L"git rev-parse --symbolic-full-name %s", shortRefName);\r
+       if(Run(cmd, &refName, CP_UTF8) != 0)\r
+               return CString();//Error\r
+       int iStart = 0;\r
+       return refName.Tokenize(L"\n", iStart);\r
+}\r
+\r
 CString CGit::StripRefName(CString refName)\r
 {\r
        if(wcsncmp(refName, L"refs/heads/", 11) == 0)\r
index a0d73c3..90fc1c6 100644 (file)
@@ -110,7 +110,9 @@ public:
        int GetRefList(STRING_VECTOR &list);\r
 \r
 \r
-       //Removes 'refs/heads/' or just 'refs'\r
+       //Example: master -> refs/heads/master\r
+       CString GetFullRefName(CString shortRefName);\r
+       //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master\r
        static CString StripRefName(CString refName);\r
        \r
 };\r
index fe45e2f..61f6894 100644 (file)
@@ -1194,17 +1194,19 @@ bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash)
        return FALSE;\r
 }\r
 \r
-bool CAppUtils::Switch(CString *CommitHash)\r
+bool CAppUtils::Switch(CString *CommitHash, CString initialRefName)\r
 {\r
        CGitSwitchDlg dlg;\r
        if(CommitHash)\r
                dlg.m_Base=*CommitHash;\r
+       if(!initialRefName.IsEmpty())\r
+               dlg.m_initialRefName = initialRefName;\r
        \r
        if (dlg.DoModal() == IDOK)\r
        {\r
                CString cmd;\r
                CString track;\r
-               CString base;\r
+//             CString base;\r
                CString force;\r
                CString branch;\r
 \r
index f6bc9a1..0f11a38 100644 (file)
@@ -156,7 +156,7 @@ public:
        \r
        static bool Export(CString *BashHash=NULL);\r
        static bool CreateBranchTag(bool IsTag=TRUE,CString *CommitHash=NULL);\r
-       static bool Switch(CString *CommitHash);\r
+       static bool Switch(CString *CommitHash, CString initialRefName = CString());\r
 \r
 //     static bool IgnoreFile(CTGitPath &file, bool IsMask);\r
        static bool IgnoreFile(CTGitPathList &filelist,bool IsMask);\r
index 989284a..05caeca 100644 (file)
@@ -554,6 +554,7 @@ void CBrowseRefsDlg::ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPSh
                bool bShowDeleteTagOption                       = false;\r
                bool bShowDeleteRemoteBranchOption      = false;\r
                bool bShowFetchOption                           = false;\r
+               bool bShowSwitchOption                          = false;\r
 \r
                CString fetchFromCmd;\r
 \r
@@ -561,6 +562,7 @@ void CBrowseRefsDlg::ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPSh
                {\r
                        bShowReflogOption = true;\r
                        bShowDeleteBranchOption = true;\r
+                       bShowSwitchOption = true;\r
                }\r
                else if(selectedLeafs[0]->IsFrom(L"refs/remotes"))\r
                {\r
@@ -582,6 +584,7 @@ void CBrowseRefsDlg::ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPSh
                                                                                        popupMenu.AppendMenuIcon(eCmd_ViewLog, L"Show Log", IDI_LOG);\r
                if(bShowReflogOption)                           popupMenu.AppendMenuIcon(eCmd_ShowReflog, L"Show Reflog", IDI_LOG);\r
                if(bShowFetchOption)                            popupMenu.AppendMenuIcon(eCmd_Fetch, fetchFromCmd, IDI_PULL);\r
+               if(bShowSwitchOption)                           popupMenu.AppendMenuIcon(eCmd_Switch, L"Switch to this Ref", IDI_SWITCH);\r
                if(bShowDeleteTagOption)                        popupMenu.AppendMenuIcon(eCmd_DeleteTag, L"Delete Tag", IDI_DELETE);\r
                if(bShowDeleteBranchOption)                     popupMenu.AppendMenuIcon(eCmd_DeleteBranch, L"Delete Branch", IDI_DELETE);\r
                if(bShowDeleteRemoteBranchOption)       popupMenu.AppendMenuIcon(eCmd_DeleteRemoteBranch, L"Delete Remote Branch", IDI_DELETE);\r
@@ -671,6 +674,11 @@ void CBrowseRefsDlg::ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPSh
                        Refresh();\r
                }\r
                break;\r
+       case eCmd_Switch:\r
+               {\r
+                       CAppUtils::Switch(NULL, selectedLeafs[0]->GetRefName());\r
+               }\r
+               break;\r
        case eCmd_AddRemote:\r
                {\r
                        CAddRemoteDlg(this).DoModal();\r
index f211b73..d722e87 100644 (file)
@@ -69,7 +69,8 @@ public:
                eCmd_DeleteTag,\r
                eCmd_ShowReflog,\r
                eCmd_Diff,\r
-               eCmd_Fetch\r
+               eCmd_Fetch,\r
+               eCmd_Switch\r
        };\r
 \r
        enum eCol\r
index d41f8ef..1f18fb3 100644 (file)
@@ -6,6 +6,7 @@
 class CChooseVersion\r
 {\r
 public:\r
+       CString m_initialRefName;\r
        \r
 private:\r
        CWnd *  m_pWin;\r
@@ -92,27 +93,40 @@ protected:
                CString resultRef = CBrowseRefsDlg::PickRef(false, m_VersionName, gPickRef_All);\r
                if(resultRef.IsEmpty())\r
                        return;\r
-               if(wcsncmp(resultRef,L"refs/",5)==0)\r
-                       resultRef = resultRef.Mid(5);\r
-               if(wcsncmp(resultRef,L"heads/",6)==0)\r
+               SelectRef(resultRef, false);\r
+       }\r
+\r
+       void SelectRef(CString refName, bool bRefNameIsPossiblyNotFullName = true)\r
+       {\r
+               if(bRefNameIsPossiblyNotFullName)\r
+               {\r
+                       //Make sure refName is a full ref name first\r
+                       CString fullRefName = g_Git.GetFullRefName(refName);\r
+                       if(!fullRefName.IsEmpty())\r
+                               refName = fullRefName;\r
+               }\r
+\r
+               if(wcsncmp(refName,L"refs/",5)==0)\r
+                       refName = refName.Mid(5);\r
+               if(wcsncmp(refName,L"heads/",6)==0)\r
                {\r
-                       resultRef = resultRef.Mid(6);\r
+                       refName = refName.Mid(6);\r
                        SetDefaultChoose(IDC_RADIO_BRANCH);\r
                        m_ChooseVersioinBranch.SetCurSel(\r
-                               m_ChooseVersioinBranch.FindStringExact(-1, resultRef));\r
+                               m_ChooseVersioinBranch.FindStringExact(-1, refName));\r
                }\r
-               else if(wcsncmp(resultRef,L"remotes/",8)==0)\r
+               else if(wcsncmp(refName,L"remotes/",8)==0)\r
                {\r
                        SetDefaultChoose(IDC_RADIO_BRANCH);\r
                        m_ChooseVersioinBranch.SetCurSel(\r
-                               m_ChooseVersioinBranch.FindStringExact(-1, resultRef));\r
+                               m_ChooseVersioinBranch.FindStringExact(-1, refName));\r
                }\r
-               else if(wcsncmp(resultRef,L"tags/",5)==0)\r
+               else if(wcsncmp(refName,L"tags/",5)==0)\r
                {\r
-                       resultRef = resultRef.Mid(5);\r
+                       refName = refName.Mid(5);\r
                        SetDefaultChoose(IDC_RADIO_TAGS);\r
                        m_ChooseVersioinTags.SetCurSel(\r
-                               m_ChooseVersioinTags.FindStringExact(-1, resultRef));\r
+                               m_ChooseVersioinTags.FindStringExact(-1, refName));\r
                }\r
                OnVersionChanged();\r
        }\r
@@ -134,7 +148,10 @@ protected:
                m_ChooseVersioinBranch.SetCurSel(current);\r
 \r
 \r
-               OnVersionChanged();\r
+               if(m_initialRefName.IsEmpty())\r
+                       OnVersionChanged();\r
+               else\r
+                       SelectRef(m_initialRefName);\r
        }\r
 public:                                        \r
        CString m_VersionName;\r