Blame tests/checkout/checkout_helpers.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "checkout_helpers.h"
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
Packit Service 20376f
void assert_on_branch(git_repository *repo, const char *branch)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head;
Packit Service 20376f
	git_buf bname = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
Packit Service 20376f
	cl_assert_(git_reference_type(head) == GIT_REF_SYMBOLIC, branch);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
Packit Service 20376f
	cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	git_buf_free(&bname);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void reset_index_to_treeish(git_object *treeish)
Packit Service 20376f
{
Packit Service 20376f
	git_object *tree;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_repository *repo = git_object_owner(treeish);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJ_TREE));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, repo));
Packit Service 20376f
	cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
Packit Service 20376f
	cl_git_pass(git_index_write(index));
Packit Service 20376f
Packit Service 20376f
	git_object_free(tree);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int checkout_count_callback(
Packit Service 20376f
	git_checkout_notify_t why,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const git_diff_file *baseline,
Packit Service 20376f
	const git_diff_file *target,
Packit Service 20376f
	const git_diff_file *workdir,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	checkout_counts *ct = payload;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(baseline); GIT_UNUSED(target); GIT_UNUSED(workdir);
Packit Service 20376f
Packit Service 20376f
	if (why & GIT_CHECKOUT_NOTIFY_CONFLICT) {
Packit Service 20376f
		ct->n_conflicts++;
Packit Service 20376f
Packit Service 20376f
		if (ct->debug) {
Packit Service 20376f
			if (workdir) {
Packit Service 20376f
				if (baseline) {
Packit Service 20376f
					if (target)
Packit Service 20376f
						fprintf(stderr, "M %s (conflicts with M %s)\n",
Packit Service 20376f
							workdir->path, target->path);
Packit Service 20376f
					else
Packit Service 20376f
						fprintf(stderr, "M %s (conflicts with D %s)\n",
Packit Service 20376f
							workdir->path, baseline->path);
Packit Service 20376f
				} else {
Packit Service 20376f
					if (target)
Packit Service 20376f
						fprintf(stderr, "Existing %s (conflicts with A %s)\n",
Packit Service 20376f
							workdir->path, target->path);
Packit Service 20376f
					else
Packit Service 20376f
						fprintf(stderr, "How can an untracked file be a conflict (%s)\n", workdir->path);
Packit Service 20376f
				}
Packit Service 20376f
			} else {
Packit Service 20376f
				if (baseline) {
Packit Service 20376f
					if (target)
Packit Service 20376f
						fprintf(stderr, "D %s (conflicts with M %s)\n",
Packit Service 20376f
							target->path, baseline->path);
Packit Service 20376f
					else
Packit Service 20376f
						fprintf(stderr, "D %s (conflicts with D %s)\n",
Packit Service 20376f
							baseline->path, baseline->path);
Packit Service 20376f
				} else {
Packit Service 20376f
					if (target)
Packit Service 20376f
						fprintf(stderr, "How can an added file with no workdir be a conflict (%s)\n", target->path);
Packit Service 20376f
					else
Packit Service 20376f
						fprintf(stderr, "How can a nonexistent file be a conflict (%s)\n", path);
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (why & GIT_CHECKOUT_NOTIFY_DIRTY) {
Packit Service 20376f
		ct->n_dirty++;
Packit Service 20376f
Packit Service 20376f
		if (ct->debug) {
Packit Service 20376f
			if (workdir)
Packit Service 20376f
				fprintf(stderr, "M %s\n", workdir->path);
Packit Service 20376f
			else
Packit Service 20376f
				fprintf(stderr, "D %s\n", baseline->path);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (why & GIT_CHECKOUT_NOTIFY_UPDATED) {
Packit Service 20376f
		ct->n_updates++;
Packit Service 20376f
Packit Service 20376f
		if (ct->debug) {
Packit Service 20376f
			if (baseline) {
Packit Service 20376f
				if (target)
Packit Service 20376f
					fprintf(stderr, "update: M %s\n", path);
Packit Service 20376f
				else
Packit Service 20376f
					fprintf(stderr, "update: D %s\n", path);
Packit Service 20376f
			} else {
Packit Service 20376f
				if (target)
Packit Service 20376f
					fprintf(stderr, "update: A %s\n", path);
Packit Service 20376f
				else
Packit Service 20376f
					fprintf(stderr, "update: this makes no sense %s\n", path);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (why & GIT_CHECKOUT_NOTIFY_UNTRACKED) {
Packit Service 20376f
		ct->n_untracked++;
Packit Service 20376f
Packit Service 20376f
		if (ct->debug)
Packit Service 20376f
			fprintf(stderr, "? %s\n", path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (why & GIT_CHECKOUT_NOTIFY_IGNORED) {
Packit Service 20376f
		ct->n_ignored++;
Packit Service 20376f
Packit Service 20376f
		if (ct->debug)
Packit Service 20376f
			fprintf(stderr, "I %s\n", path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void tick_index(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	struct timespec ts;
Packit Service 20376f
	struct p_timeval times[2];
Packit Service 20376f
Packit Service 20376f
	cl_assert(index->on_disk);
Packit Service 20376f
	cl_assert(git_index_path(index));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_index_read(index, true));
Packit Service 20376f
	ts = index->stamp.mtime;
Packit Service 20376f
Packit Service 20376f
	times[0].tv_sec = ts.tv_sec;
Packit Service 20376f
	times[0].tv_usec = ts.tv_nsec / 1000;
Packit Service 20376f
	times[1].tv_sec = ts.tv_sec + 5;
Packit Service 20376f
	times[1].tv_usec = ts.tv_nsec / 1000;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_utimes(git_index_path(index), times));
Packit Service 20376f
	cl_git_pass(git_index_read(index, true));
Packit Service 20376f
}