]> git.proxmox.com Git - libgit2.git/blob - tests/rebase/submodule.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / rebase / submodule.c
1 #include "clar_libgit2.h"
2 #include "git2/checkout.h"
3 #include "git2/rebase.h"
4 #include "posix.h"
5 #include "signature.h"
6 #include "../submodule/submodule_helpers.h"
7
8 #include <fcntl.h>
9
10 static git_repository *repo;
11 static git_signature *signature;
12
13 /* Fixture setup and teardown */
14 void test_rebase_submodule__initialize(void)
15 {
16 git_index *index;
17 git_oid tree_oid, commit_id;
18 git_tree *tree;
19 git_commit *parent;
20 git_object *obj;
21 git_reference *master_ref;
22 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
23 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
24
25 repo = cl_git_sandbox_init("rebase-submodule");
26 cl_git_pass(git_signature_new(&signature,
27 "Rebaser", "rebaser@rebaser.rb", 1405694510, 0));
28
29 rewrite_gitmodules(git_repository_workdir(repo));
30
31 cl_git_pass(git_submodule_set_url(repo, "my-submodule", git_repository_path(repo)));
32
33 /* We have to commit the rewritten .gitmodules file */
34 cl_git_pass(git_repository_index(&index, repo));
35 cl_git_pass(git_index_add_bypath(index, ".gitmodules"));
36 cl_git_pass(git_index_write(index));
37
38 cl_git_pass(git_index_write_tree(&tree_oid, index));
39 cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
40
41 cl_git_pass(git_repository_head(&master_ref, repo));
42 cl_git_pass(git_commit_lookup(&parent, repo, git_reference_target(master_ref)));
43
44 cl_git_pass(git_commit_create_v(&commit_id, repo, git_reference_name(master_ref), signature, signature, NULL, "Fixup .gitmodules", tree, 1, parent));
45
46 /* And a final reset, for good measure */
47 cl_git_pass(git_object_lookup(&obj, repo, &commit_id, GIT_OBJECT_COMMIT));
48 cl_git_pass(git_reset(repo, obj, GIT_RESET_HARD, &opts));
49
50 git_index_free(index);
51 git_object_free(obj);
52 git_commit_free(parent);
53 git_reference_free(master_ref);
54 git_tree_free(tree);
55 }
56
57 void test_rebase_submodule__cleanup(void)
58 {
59 git_signature_free(signature);
60 cl_git_sandbox_cleanup();
61 }
62
63 void test_rebase_submodule__init_untracked(void)
64 {
65 git_rebase *rebase;
66 git_reference *branch_ref, *upstream_ref;
67 git_annotated_commit *branch_head, *upstream_head;
68 git_str untracked_path = GIT_STR_INIT;
69 FILE *fp;
70 git_submodule *submodule;
71
72 cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/asparagus"));
73 cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
74
75 cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
76 cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
77
78 cl_git_pass(git_submodule_lookup(&submodule, repo, "my-submodule"));
79 cl_git_pass(git_submodule_update(submodule, 1, NULL));
80
81 git_str_printf(&untracked_path, "%s/my-submodule/untracked", git_repository_workdir(repo));
82 fp = fopen(git_str_cstr(&untracked_path), "w");
83 fprintf(fp, "An untracked file in a submodule should not block a rebase\n");
84 fclose(fp);
85 git_str_dispose(&untracked_path);
86
87 cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
88
89 git_submodule_free(submodule);
90 git_annotated_commit_free(branch_head);
91 git_annotated_commit_free(upstream_head);
92 git_reference_free(branch_ref);
93 git_reference_free(upstream_ref);
94 git_rebase_free(rebase);
95 }