Commit 759b2230 by Edward Thomson

Merge pull request #3303 from libgit2/cmn/index-add-submodule

Allow adding a submodule through git_index_add_bypath
parents 91dad181 247d27c2
...@@ -48,6 +48,7 @@ typedef enum { ...@@ -48,6 +48,7 @@ typedef enum {
GIT_EEOF = -20, /**< Unexpected EOF */ GIT_EEOF = -20, /**< Unexpected EOF */
GIT_EINVALID = -21, /**< Invalid operation or input */ GIT_EINVALID = -21, /**< Invalid operation or input */
GIT_EUNCOMMITTED = -22, /**< Uncommitted changes in index prevented operation */ GIT_EUNCOMMITTED = -22, /**< Uncommitted changes in index prevented operation */
GIT_EDIRECTORY = -23, /**< The operation is not valid for a directory */
GIT_PASSTHROUGH = -30, /**< Internal only */ GIT_PASSTHROUGH = -30, /**< Internal only */
GIT_ITEROVER = -31, /**< Signals end of iteration with iterator */ GIT_ITEROVER = -31, /**< Signals end of iteration with iterator */
......
...@@ -185,6 +185,12 @@ int git_blob__create_from_paths( ...@@ -185,6 +185,12 @@ int git_blob__create_from_paths(
(error = git_repository_odb(&odb, repo)) < 0) (error = git_repository_odb(&odb, repo)) < 0)
goto done; goto done;
if (S_ISDIR(st.st_mode)) {
giterr_set(GITERR_ODB, "cannot create blob from '%s'; it is a directory", content_path);
error = GIT_EDIRECTORY;
goto done;
}
if (out_st) if (out_st)
memcpy(out_st, &st, sizeof(st)); memcpy(out_st, &st, sizeof(st));
......
...@@ -1236,9 +1236,29 @@ int git_index_add_bypath(git_index *index, const char *path) ...@@ -1236,9 +1236,29 @@ int git_index_add_bypath(git_index *index, const char *path)
assert(index && path); assert(index && path);
if ((ret = index_entry_init(&entry, index, path)) < 0 || if ((ret = index_entry_init(&entry, index, path)) == 0)
(ret = index_insert(index, &entry, 1, false)) < 0) ret = index_insert(index, &entry, 1, false);
/* If we were given a directory, let's see if it's a submodule */
if (ret < 0 && ret != GIT_EDIRECTORY)
return ret;
if (ret == GIT_EDIRECTORY) {
git_submodule *sm;
git_error_state err;
giterr_capture(&err, ret);
ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
if (ret == GIT_ENOTFOUND)
return giterr_restore(&err);
else
git__free(err.error_msg.message);
ret = git_submodule_add_to_index(sm, false);
git_submodule_free(sm);
return ret; return ret;
}
/* Adding implies conflict was resolved, move conflict entries to REUC */ /* Adding implies conflict was resolved, move conflict entries to REUC */
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
......
#include "clar_libgit2.h"
#include "repository.h"
#include "../submodule/submodule_helpers.h"
static git_repository *g_repo;
static git_index *g_idx;
void test_index_bypath__initialize(void)
{
g_repo = setup_fixture_submod2();
cl_git_pass(git_repository_index__weakptr(&g_idx, g_repo));
}
void test_index_bypath__cleanup(void)
{
g_repo = NULL;
g_idx = NULL;
}
void test_index_bypath__add_directory(void)
{
cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
}
void test_index_bypath__add_submodule(void)
{
unsigned int status;
const char *sm_name = "sm_changed_head";
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
cl_git_pass(git_index_add_bypath(g_idx, sm_name));
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
cl_assert_equal_i(0, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "submodule_helpers.h" #include "submodule_helpers.h"
#include "config/config_helpers.h" #include "config/config_helpers.h"
#include "fileops.h" #include "fileops.h"
#include "repository.h"
static git_repository *g_repo = NULL; static git_repository *g_repo = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment