TrackGit
Push.cpp
Go to the documentation of this file.
1 
8 #include "Push.h"
9 #include "../Utils.h"
10 
11 #include <InterfaceKit.h>
12 
13 #include <git2.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 
23 Push::Push(BString repo)
24  :
25  GitCommand()
26 {
27  fRepo = repo;
28  fPushWindow = NULL;
29 }
30 
31 
32 void
34 {
35 }
36 
37 
44 {
45  if (fPushWindow == NULL)
47  return fPushWindow;
48 }
49 
50 
56 void*
57 DoPushThread(void* arg)
58 {
59  struct push_params* p = (struct push_params*) arg;
60 
61  git_libgit2_init();
62 
63  int err;
64  git_repository* repo = NULL;
65  git_remote* remote = NULL;
66  git_reference *head = NULL;
67  git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
68  git_push_options options;
69  git_remote_autotag_option_t tags;
70  git_strarray array;
71  const char* ref_name;
72  char refspec[200];
73 
74  err = git_repository_open(&repo, p->path);
75  if (err < 0)
76  goto ret;
77 
78  if (p->pushWindow)
79  p->pushWindow->SetText("Getting remote...");
80  err = git_remote_lookup(&remote, repo, "origin");
81  if (err < 0)
82  goto ret;
83 
84  callbacks.credentials = cred_acquire_cb;
85 
86  err = git_repository_head(&head, repo);
87  if (err < 0)
88  goto ret;
89 
90  ref_name = git_reference_name(head);
91  sprintf(refspec, "%s:%s", ref_name, ref_name);
92 
93  err = git_push_init_options(&options, GIT_PUSH_OPTIONS_VERSION);
94  if (err < 0)
95  goto ret;
96  memcpy(&options.callbacks, &callbacks, sizeof(git_remote_callbacks));
97 
98  if (p->pushWindow)
99  p->pushWindow->SetText("Pushing changes...");
100 
101  array.count = 1;
102  array.strings = (char**) malloc(sizeof(char*) * array.count);
103  array.strings[0] = refspec;
104 
105  err = git_remote_push(remote, &array, &options);
106  if (err < 0)
107  goto ret;
108 
109  err = git_remote_update_tips(remote, NULL, 1, tags, NULL);
110 
111 ret:
112  if (err < 0) {
113  const git_error* er = giterr_last();
114  printf("Error %d : %s\n", er->klass, er->message);
115 
116  BString buffer("Error : %s");
117  buffer.ReplaceFirst("%s", er->message);
118  BAlert *alert = new BAlert("", buffer.String(), "Cancel",
119  0, 0, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
120  alert->Go();
121  }
122 clean:
123  free(array.strings);
124  git_reference_free(head);
125  git_remote_disconnect(remote);
126  git_remote_free(remote);
127  git_repository_free(repo);
128  git_libgit2_shutdown();
129  if (p->pushWindow && p->pushWindow->LockLooper())
130  p->pushWindow->Quit();
131  free(arg);
132 }
133 
134 
140 pthread_t
142 {
143  struct push_params *p = (struct push_params*)
144  malloc(sizeof(struct push_params));
145  p->path = path;
146  p->pushWindow = pushWindow;
147  pthread_t thread_id;
148  pthread_create(&thread_id, NULL, DoPushThread, p);
149  return thread_id;
150 }
PushWindow * pushWindow
Definition: Push.h:25
Parameters to pass to push thread.
Definition: Push.h:23
Header file of Push command.
virtual void Quit()
The function to Quit the window.
virtual TrackGitWindow * GetWindow()
This returns pointer to the Push window.
Definition: Push.cpp:43
GitCommand Class.
Definition: GitCommand.h:20
The Push window class.
Definition: PushWindow.h:22
BString fRepo
The repository path where Push option is selected.
Definition: Push.h:37
int cred_acquire_cb(git_cred **out, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload)
This functions gets username and password from the user in case credentials are required.
Definition: Utils.cpp:299
void * DoPushThread(void *arg)
This does git push on given repository.
Definition: Push.cpp:57
void SetText(const char *)
This function sets texts of the textview within window.
Definition: PushWindow.cpp:59
Push(BString)
Push command constructor.
Definition: Push.cpp:23
virtual void Execute()
This is where actual calls to libgit2 will go.
Definition: Push.cpp:33
static pthread_t DoPush(PushWindow *, const char *)
This spawns thread to perform push over given repo.
Definition: Push.cpp:141
The TrackGit Window class.
TrackGitWindow * fPushWindow
Push Window.
Definition: Push.h:41
const char * path
Definition: Push.h:24