Blame examples/network/clone.c

Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include <git2.h>
Packit Service 20376f
#include <git2/clone.h>
Packit Service 20376f
#include <stdio.h>
Packit Service 20376f
#include <stdlib.h>
Packit Service 20376f
#include <string.h>
Packit Service 20376f
#ifndef _WIN32
Packit Service 20376f
# include <pthread.h>
Packit Service 20376f
# include <unistd.h>
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
typedef struct progress_data {
Packit Service 20376f
	git_transfer_progress fetch_progress;
Packit Service 20376f
	size_t completed_steps;
Packit Service 20376f
	size_t total_steps;
Packit Service 20376f
	const char *path;
Packit Service 20376f
} progress_data;
Packit Service 20376f
Packit Service 20376f
static void print_progress(const progress_data *pd)
Packit Service 20376f
{
Packit Service 20376f
	int network_percent = pd->fetch_progress.total_objects > 0 ?
Packit Service 20376f
		(100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects :
Packit Service 20376f
		0;
Packit Service 20376f
	int index_percent = pd->fetch_progress.total_objects > 0 ?
Packit Service 20376f
		(100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects :
Packit Service 20376f
		0;
Packit Service 20376f
Packit Service 20376f
	int checkout_percent = pd->total_steps > 0
Packit Service 20376f
		? (100 * pd->completed_steps) / pd->total_steps
Packit Service 20376f
		: 0;
Packit Service 20376f
	int kbytes = pd->fetch_progress.received_bytes / 1024;
Packit Service 20376f
Packit Service 20376f
	if (pd->fetch_progress.total_objects &&
Packit Service 20376f
		pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) {
Packit Service 20376f
		printf("Resolving deltas %d/%d\r",
Packit Service 20376f
		       pd->fetch_progress.indexed_deltas,
Packit Service 20376f
		       pd->fetch_progress.total_deltas);
Packit Service 20376f
	} else {
Packit Service 20376f
		printf("net %3d%% (%4d kb, %5d/%5d)  /  idx %3d%% (%5d/%5d)  /  chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
Packit Service 20376f
		   network_percent, kbytes,
Packit Service 20376f
		   pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
Packit Service 20376f
		   index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
Packit Service 20376f
		   checkout_percent,
Packit Service 20376f
		   pd->completed_steps, pd->total_steps,
Packit Service 20376f
		   pd->path);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int sideband_progress(const char *str, int len, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	(void)payload; // unused
Packit Service 20376f
Packit Service 20376f
	printf("remote: %.*s", len, str);
Packit Service 20376f
	fflush(stdout);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int fetch_progress(const git_transfer_progress *stats, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	progress_data *pd = (progress_data*)payload;
Packit Service 20376f
	pd->fetch_progress = *stats;
Packit Service 20376f
	print_progress(pd);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	progress_data *pd = (progress_data*)payload;
Packit Service 20376f
	pd->completed_steps = cur;
Packit Service 20376f
	pd->total_steps = tot;
Packit Service 20376f
	pd->path = path;
Packit Service 20376f
	print_progress(pd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
int do_clone(git_repository *repo, int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	progress_data pd = {{0}};
Packit Service 20376f
	git_repository *cloned_repo = NULL;
Packit Service 20376f
	git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
Packit Service 20376f
	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	const char *url = argv[1];
Packit Service 20376f
	const char *path = argv[2];
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	(void)repo; // unused
Packit Service 20376f
Packit Service 20376f
	// Validate args
Packit Service 20376f
	if (argc < 3) {
Packit Service 20376f
		printf ("USAGE: %s <url> <path>\n", argv[0]);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	// Set up options
Packit Service 20376f
	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
Packit Service 20376f
	checkout_opts.progress_cb = checkout_progress;
Packit Service 20376f
	checkout_opts.progress_payload = &pd;
Packit Service 20376f
	clone_opts.checkout_opts = checkout_opts;
Packit Service 20376f
	clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
Packit Service 20376f
	clone_opts.fetch_opts.callbacks.transfer_progress = &fetch_progress;
Packit Service 20376f
	clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb;
Packit Service 20376f
	clone_opts.fetch_opts.callbacks.payload = &pd;
Packit Service 20376f
Packit Service 20376f
	// Do the clone
Packit Service 20376f
	error = git_clone(&cloned_repo, url, path, &clone_opts);
Packit Service 20376f
	printf("\n");
Packit Service 20376f
	if (error != 0) {
Packit Service 20376f
		const git_error *err = giterr_last();
Packit Service 20376f
		if (err) printf("ERROR %d: %s\n", err->klass, err->message);
Packit Service 20376f
		else printf("ERROR %d: no detailed info\n", error);
Packit Service 20376f
	}
Packit Service 20376f
	else if (cloned_repo) git_repository_free(cloned_repo);
Packit Service 20376f
	return error;
Packit Service 20376f
}