Blame examples/network/fetch.c

Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include <git2.h>
Packit ae9e2a
#include <stdio.h>
Packit ae9e2a
#include <stdlib.h>
Packit ae9e2a
#include <string.h>
Packit ae9e2a
#ifndef _WIN32
Packit ae9e2a
# include <pthread.h>
Packit ae9e2a
# include <unistd.h>
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
struct dl_data {
Packit ae9e2a
	git_remote *remote;
Packit ae9e2a
	git_fetch_options *fetch_opts;
Packit ae9e2a
	int ret;
Packit ae9e2a
	int finished;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
static int progress_cb(const char *str, int len, void *data)
Packit ae9e2a
{
Packit ae9e2a
	(void)data;
Packit ae9e2a
	printf("remote: %.*s", len, str);
Packit ae9e2a
	fflush(stdout); /* We don't have the \n to force the flush */
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * This function gets called for each remote-tracking branch that gets
Packit ae9e2a
 * updated. The message we output depends on whether it's a new one or
Packit ae9e2a
 * an update.
Packit ae9e2a
 */
Packit ae9e2a
static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
Packit ae9e2a
{
Packit ae9e2a
	char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
Packit ae9e2a
	(void)data;
Packit ae9e2a
Packit ae9e2a
	git_oid_fmt(b_str, b);
Packit ae9e2a
	b_str[GIT_OID_HEXSZ] = '\0';
Packit ae9e2a
Packit ae9e2a
	if (git_oid_iszero(a)) {
Packit ae9e2a
		printf("[new]     %.20s %s\n", b_str, refname);
Packit ae9e2a
	} else {
Packit ae9e2a
		git_oid_fmt(a_str, a);
Packit ae9e2a
		a_str[GIT_OID_HEXSZ] = '\0';
Packit ae9e2a
		printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * This gets called during the download and indexing. Here we show
Packit ae9e2a
 * processed and total objects in the pack and the amount of received
Packit ae9e2a
 * data. Most frontends will probably want to show a percentage and
Packit ae9e2a
 * the download rate.
Packit ae9e2a
 */
Packit ae9e2a
static int transfer_progress_cb(const git_transfer_progress *stats, void *payload)
Packit ae9e2a
{
Packit ae9e2a
	(void)payload;
Packit ae9e2a
Packit ae9e2a
	if (stats->received_objects == stats->total_objects) {
Packit ae9e2a
		printf("Resolving deltas %d/%d\r",
Packit ae9e2a
		       stats->indexed_deltas, stats->total_deltas);
Packit ae9e2a
	} else if (stats->total_objects > 0) {
Packit ae9e2a
		printf("Received %d/%d objects (%d) in %" PRIuZ " bytes\r",
Packit ae9e2a
		       stats->received_objects, stats->total_objects,
Packit ae9e2a
		       stats->indexed_objects, stats->received_bytes);
Packit ae9e2a
	}
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Entry point for this command */
Packit ae9e2a
int fetch(git_repository *repo, int argc, char **argv)
Packit ae9e2a
{
Packit ae9e2a
	git_remote *remote = NULL;
Packit ae9e2a
	const git_transfer_progress *stats;
Packit ae9e2a
	git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
Packit ae9e2a
Packit ae9e2a
	if (argc < 2) {
Packit ae9e2a
		fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
Packit ae9e2a
		return EXIT_FAILURE;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* Figure out whether it's a named remote or a URL */
Packit ae9e2a
	printf("Fetching %s for repo %p\n", argv[1], repo);
Packit ae9e2a
	if (git_remote_lookup(&remote, repo, argv[1]) < 0)
Packit ae9e2a
		if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
Packit ae9e2a
			goto on_error;
Packit ae9e2a
Packit ae9e2a
	/* Set up the callbacks (only update_tips for now) */
Packit ae9e2a
	fetch_opts.callbacks.update_tips = &update_cb;
Packit ae9e2a
	fetch_opts.callbacks.sideband_progress = &progress_cb;
Packit ae9e2a
	fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
Packit ae9e2a
	fetch_opts.callbacks.credentials = cred_acquire_cb;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Perform the fetch with the configured refspecs from the
Packit ae9e2a
	 * config. Update the reflog for the updated references with
Packit ae9e2a
	 * "fetch".
Packit ae9e2a
	 */
Packit ae9e2a
	if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0)
Packit ae9e2a
		goto on_error;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * If there are local objects (we got a thin pack), then tell
Packit ae9e2a
	 * the user how many objects we saved from having to cross the
Packit ae9e2a
	 * network.
Packit ae9e2a
	 */
Packit ae9e2a
	stats = git_remote_stats(remote);
Packit ae9e2a
	if (stats->local_objects > 0) {
Packit ae9e2a
		printf("\rReceived %d/%d objects in %" PRIuZ " bytes (used %d local objects)\n",
Packit ae9e2a
		       stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
Packit ae9e2a
	} else{
Packit ae9e2a
		printf("\rReceived %d/%d objects in %" PRIuZ "bytes\n",
Packit ae9e2a
			stats->indexed_objects, stats->total_objects, stats->received_bytes);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_remote_free(remote);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
Packit ae9e2a
 on_error:
Packit ae9e2a
	git_remote_free(remote);
Packit ae9e2a
	return -1;
Packit ae9e2a
}