TrackGit
Add.cpp
Go to the documentation of this file.
1 
8 #include "Add.h"
9 #include "../Utils.h"
10 
11 #include <InterfaceKit.h>
12 
13 #include <stdlib.h>
14 
15 
21 Add::Add(BString dirPath, vector<char*> files)
22  :
23  GitCommand()
24 {
25  fDirPath = dirPath;
26  fFiles = files;
27 }
28 
29 
35 git_strarray
36 Add::InitArray(vector<char*> files)
37 {
38  git_strarray array = {0};
39  array.count = files.size();
40  array.strings = (char**) malloc(sizeof(char*) * array.count);
41 
42  for (int i=0; i<array.count; i++)
43  array.strings[i] = files[i];
44 
45  return array;
46 }
47 
48 
53 void
54 Add::FreeArray(git_strarray array)
55 {
56  free(array.strings);
57 }
58 
59 
66 int
67 Add::AddFiles(BString dirPath, vector<char*> files)
68 {
69  git_repository *repo = NULL;
70  git_index *index;
71  git_strarray array = InitArray(files);
72  int ret = 0;
73 
74  git_libgit2_init();
75 
76  ret = git_repository_open(&repo, dirPath.String());
77  if (ret != 0)
78  goto ret;
79 
80  ret = git_repository_index(&index, repo);
81  if (ret != 0)
82  goto ret;
83 
84  ret = git_index_add_all(index, &array, 0, NULL, NULL);
85  if (ret != 0)
86  goto ret;
87 
88  ret = git_index_write(index);
89  if (ret != 0)
90  goto ret;
91 
92 ret:
93  FreeArray(array);
94  git_index_free(index);
95  git_repository_free(repo);
96 
97  git_libgit2_shutdown();
98  return ret;
99 }
100 
101 
105 void
107 {
108  if (AddFiles(fDirPath, fFiles) < 0) {
109  const git_error* err = giterr_last();
110  printf("Error %d : %s\n", err->klass, err->message);
111 
112  BString buffer("Error : %s");
113  buffer.ReplaceFirst("%s", err->message);
114  BAlert* alert = new BAlert("", buffer.String(), "Cancel",
115  0, 0, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
116  alert->Go();
117  } else {
118  BString buffer("Files successfully added to the repository.");
119  BAlert* alert = new BAlert("", buffer.String(), "OK",
120  0, 0, B_WIDTH_AS_USUAL);
121  alert->Go();
122  }
123 
124  BMessenger messenger(APP_SIGN);
125  messenger.SendMessage(new BMessage(kQuitWindow));
126 }
#define APP_SIGN
The Application signature.
Definition: Utils.h:45
BString fDirPath
The current directory where Init option is selected.
Definition: Add.h:28
GitCommand Class.
Definition: GitCommand.h:20
vector< char * > fFiles
The list of files to be added.
Definition: Add.h:32
Add(BString, vector< char *>)
Add command constructor.
Definition: Add.cpp:21
static int AddFiles(BString, vector< char *>)
Adds given files into git repository.
Definition: Add.cpp:67
static git_strarray InitArray(vector< char *> files)
Initializes git_strarray.
Definition: Add.cpp:36
Header file of Add command.
virtual void Execute()
Add command excution.
Definition: Add.cpp:106
static void FreeArray(git_strarray)
Frees the allocated strings in git_strarray.
Definition: Add.cpp:54