Guest User

Untitled

a guest
May 20th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. error = git_repository_open(&repo, ".git");
  2. if (error < GIT_SUCCESS)
  3. repo = NULL;
  4.  
  5. ...
  6.  
  7.  
  8.  
  9. static int rename_packfile(char *packname, git_indexer *idx)
  10. {
  11. char path[GIT_PATH_MAX], oid[GIT_OID_HEXSZ + 1], *slash;
  12. int ret;
  13.  
  14. strcpy(path, packname);
  15. slash = strrchr(path, '/');
  16.  
  17. if (!slash)
  18. return GIT_EINVALIDARGS;
  19.  
  20. memset(oid, 0x0, sizeof(oid));
  21. // The name of the packfile is given by it's hash which you can get
  22. // with git_indexer_hash after the index has been written out to
  23. // disk. Rename the packfile to its "real" name in the same
  24. // directory as it was originally (libgit2 stores it in the folder
  25. // where the packs go, so a rename in place is the right thing to do here
  26. git_oid_fmt(oid, git_indexer_hash(idx));
  27. ret = sprintf(slash + 1, "pack-%s.pack", oid);
  28. if(ret < 0)
  29. return GIT_EOSERR;
  30.  
  31. printf("Renaming pack to %s\n", path);
  32. return rename(packname, path);
  33. }
  34.  
  35. int fetch(git_repository *repo, int argc, char **argv)
  36. {
  37. git_remote *remote = NULL;
  38. git_indexer *idx = NULL;
  39. git_indexer_stats stats;
  40. int error;
  41. char *packname = NULL;
  42.  
  43. // Get the remote and connect to it
  44. printf("Fetching %s\n", argv[1]);
  45. error = git_remote_new(&remote, repo, argv[1], NULL);
  46. if (error < GIT_SUCCESS)
  47. return error;
  48.  
  49. error = git_remote_connect(remote, GIT_DIR_FETCH);
  50. if (error < GIT_SUCCESS)
  51. return error;
  52.  
  53. // Download the packfile from the server. As we don't know its hash
  54. // yet, it will get a temporary filename
  55. error = git_remote_download(&packname, remote);
  56. if (error < GIT_SUCCESS)
  57. return error;
  58.  
  59. // No error and a NULL packname means no packfile was needed
  60. if (packname != NULL) {
  61. printf("The packname is %s\n", packname);
  62.  
  63. // Create a new instance indexer
  64. error = git_indexer_new(&idx, packname);
  65. if (error < GIT_SUCCESS)
  66. return error;
  67.  
  68. // This should be run in paralel, but it'd be too complicated for the example
  69. error = git_indexer_run(idx, &stats);
  70. if (error < GIT_SUCCESS)
  71. return error;
  72.  
  73. printf("Received %d objects\n", stats.total);
  74.  
  75. // Write the index file. The index will be stored with the
  76. // correct filename
  77. error = git_indexer_write(idx);
  78. if (error < GIT_SUCCESS)
  79. return error;
  80.  
  81. error = rename_packfile(packname, idx);
  82. if (error < GIT_SUCCESS)
  83. return error;
  84. }
  85.  
  86. // Update the references in the remote's namespace to point to the
  87. // right commits. This may be needed even if there was no packfile
  88. // to download, which can happen e.g. when the branches have been
  89. // changed but all the neede objects are available locally.
  90. error = git_remote_update_tips(remote);
  91. if (error < GIT_SUCCESS)
  92. return error;
  93.  
  94. free(packname);
  95. git_indexer_free(idx);
  96. git_remote_free(remote);
  97.  
  98. return GIT_SUCCESS;
  99. }
  100.  
  101. ...
  102.  
  103. if(repo)
  104. git_repository_free(repo);
Add Comment
Please, Sign In to add comment