Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index-pack.c « network « examples - github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03f3ae37eabfb84167a5f9e2b767410edcd7dad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <git2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"

// This could be run in the main loop whilst the application waits for
// the indexing to finish in a worker thread
int index_cb(const git_indexer_stats *stats, void *data)
{
  printf("\rProcessing %d of %d", stats->processed, stats->total);
}

int index_pack(git_repository *repo, int argc, char **argv)
{
  git_indexer_stream *idx;
  git_indexer_stats stats = {0, 0};
  int error, fd;
  char hash[GIT_OID_HEXSZ + 1] = {0};
  ssize_t read_bytes;
  char buf[512];

  if (argc < 2) {
	  fprintf(stderr, "I need a packfile\n");
	  return EXIT_FAILURE;
  }

  if (git_indexer_stream_new(&idx, ".git") < 0) {
	  puts("bad idx");
	  return -1;
  }

  if ((fd = open(argv[1], 0)) < 0) {
	  perror("open");
	  return -1;
  }

  do {
	  read_bytes = read(fd, buf, sizeof(buf));
	  if (read_bytes < 0)
		  break;

	  if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
		  goto cleanup;

	  printf("\rIndexing %d of %d", stats.processed, stats.total);
  } while (read_bytes > 0);

  if (read_bytes < 0) {
	  error = -1;
	  perror("failed reading");
	  goto cleanup;
  }

  if ((error = git_indexer_stream_finalize(idx, &stats)) < 0)
	  goto cleanup;

  printf("\rIndexing %d of %d\n", stats.processed, stats.total);

  git_oid_fmt(hash, git_indexer_stream_hash(idx));
  puts(hash);

cleanup:
  close(fd);
  git_indexer_stream_free(idx);
  return error;
}

int index_pack_old(git_repository *repo, int argc, char **argv)
{
  git_indexer *indexer;
  git_indexer_stats stats;
  int error;
  char hash[GIT_OID_HEXSZ + 1] = {0};

  if (argc < 2) {
    fprintf(stderr, "I need a packfile\n");
    return EXIT_FAILURE;
  }

  // Create a new indexer
  error = git_indexer_new(&indexer, argv[1]);
  if (error < 0)
    return error;

  // Index the packfile. This function can take a very long time and
  // should be run in a worker thread.
  error = git_indexer_run(indexer, &stats);
  if (error < 0)
    return error;

  // Write the information out to an index file
  error = git_indexer_write(indexer);

  // Get the packfile's hash (which should become it's filename)
  git_oid_fmt(hash, git_indexer_hash(indexer));
  puts(hash);

  git_indexer_free(indexer);

  return 0;
}