forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch.cpp
More file actions
73 lines (67 loc) · 1.92 KB
/
Copy pathbranch.cpp
File metadata and controls
73 lines (67 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "git2cpp/initializer.h"
#include "git2cpp/repo.h"
#include <cassert>
#include <iostream>
namespace
{
using namespace git;
int create_branch(Repository & repo, const char * name, bool force)
{
try
{
auto head = repo.head();
auto branch = repo.create_branch(name, repo.commit_lookup(head.target()), force);
assert(branch);
assert(std::strcmp(branch.name(), name));
std::cout << "branch " << name << " has been created" << std::endl;
return EXIT_SUCCESS;
}
catch (branch_create_error const & e)
{
switch (e.reason)
{
case branch_create_error::already_exists:;
std::cerr << "a branch named '" << name << "' already exists" << std::endl;
break;
case branch_create_error::invalid_spec:
std::cerr << "'" << name << "' is not a valid branch name" << std::endl;
break;
case branch_create_error::unknown:
break;
}
return EXIT_FAILURE;
}
}
void print_help()
{
std::cerr << "invalid command line arguments, expected are:" << std::endl
<< "[[-f] branch_name]" << std::endl;
}
}
int main(int argc, char* argv[])
{
Initializer threads_initializer;
Repository repo(".");
switch (argc)
{
case 1:
{
auto branches = repo.branches(git::branch_type::ALL);
for (auto const & b : branches)
std::cout << b.name() << std::endl;
return EXIT_SUCCESS;
}
case 2:
return create_branch(repo, argv[1], false);
case 3:
if (std::strcmp(argv[1], "-f") != 0)
{
print_help();
return EXIT_FAILURE;
}
return create_branch(repo, argv[2], true);
default:
print_help();
return EXIT_FAILURE;
}
}