TrackGit
SwitchBranch.cpp
Go to the documentation of this file.
1 
8 #include "SwitchBranch.h"
9 #include "../UI/SwitchBranchWindow.h"
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <git2.h>
14 
20 {
21  fRepo = repo;
22  fSwitchBranchWindow = NULL;
23 }
24 
25 
29 void
31 {
32 }
33 
34 
41 int
42 SwitchBranch::GetBranches(BString repoPath, vector<BString>& branches)
43 {
44  git_repository* repo;
45  git_branch_iterator* iterator;
46  git_reference* ref;
47  git_branch_t type;
48  int ret = 0;
49 
50  git_libgit2_init();
51 
52  ret = git_repository_open_ext(&repo, repoPath.String(), 0, NULL);
53  if (ret < 0)
54  return ret;
55 
56  ret = git_branch_iterator_new(&iterator, repo, GIT_BRANCH_LOCAL);
57  if (ret < 0)
58  return ret;
59 
60  while (git_branch_next(&ref, &type, iterator) == 0)
61  branches.push_back(git_reference_shorthand(ref));
62 
63  return ret;
64 }
65 
66 
73 {
74  if (fSwitchBranchWindow == NULL)
76  return fSwitchBranchWindow;
77 }
78 
79 
86 int
87 SwitchBranch::DoSwitchBranch(BString repoPath, BString branchName)
88 {
89  git_repository* repo;
90  git_object* tree = NULL;
91  git_checkout_options opts;
92  int ret = 0;
93 
94  git_libgit2_init();
95 
96  ret = git_repository_open_ext(&repo, repoPath.String(), 0, NULL);
97  if (ret < 0)
98  return ret;
99 
100  git_checkout_init_options(&opts, GIT_CHECKOUT_OPTIONS_VERSION);
101 
102  ret = git_revparse_single(&tree, repo, branchName.String());
103  if (ret < 0)
104  return ret;
105 
106  ret = git_checkout_tree(repo, tree, &opts);
107  if (ret < 0)
108  return ret;
109 
110  BString ref("refs/heads/%s");
111  ref.ReplaceFirst("%s", branchName.String());
112  ret = git_repository_set_head(repo, ref.String());
113 ret:
114  git_repository_free(repo);
115 
116  git_libgit2_shutdown();
117 
118  return ret;
119 }
virtual TrackGitWindow * GetWindow()
This returns pointer to the Switch Branch window.
Status Options structure.
Definition: Status.h:23
static int GetBranches(BString, vector< BString > &)
This function gets all the branche names in given repository.
The Switch Branch Window class.
virtual void Execute()
SwitchBranch command execution.
static int DoSwitchBranch(BString, BString)
This function creates a new branch in repo with given name.
TrackGitWindow * fSwitchBranchWindow
The Switch Branch Window.
Definition: SwitchBranch.h:28
Header file of Switch Branch command.
SwitchBranch(BString)
SwitchBranch class constructor.
The TrackGit Window class.
BString fRepo
The repo/directory where command is called.
Definition: SwitchBranch.h:24