static inline int agb_git_tree_entry_equal(const git_tree_entry * tree_a, const git_tree_entry * tree_b ) { if(tree_a==NULL && tree_b==NULL) return 1; if(tree_a==NULL || tree_b==NULL) return 0; const git_oid * oid_a = git_tree_entry_id(tree_a); const git_oid * oid_b = git_tree_entry_id(tree_b); return git_oid_equal(oid_a,oid_b); }
static int tree_reader_read( git_buf *out, git_oid *out_id, git_filemode_t *out_filemode, git_reader *_reader, const char *filename) { tree_reader *reader = (tree_reader *)_reader; git_tree_entry *tree_entry = NULL; git_blob *blob = NULL; int error; if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 || (error = git_blob_lookup(&blob, git_tree_owner(reader->tree), git_tree_entry_id(tree_entry))) < 0 || (error = git_buf_set(out, git_blob_rawcontent(blob), git_blob_rawsize(blob))) < 0) goto done; if (out_id) git_oid_cpy(out_id, git_tree_entry_id(tree_entry)); if (out_filemode) *out_filemode = git_tree_entry_filemode(tree_entry); done: git_blob_free(blob); git_tree_entry_free(tree_entry); return error; }
static int compare_ids(TreeEntry *a, TreeEntry *b) { const git_oid *id_a, *id_b; id_a = git_tree_entry_id(a->entry); id_b = git_tree_entry_id(b->entry); return git_oid_cmp(id_a, id_b); }
static int find_subtree_in_current_level( git_tree **out, git_repository *repo, git_tree *parent, const char *annotated_object_sha, int fanout) { size_t i; const git_tree_entry *entry; *out = NULL; if (parent == NULL) return GIT_ENOTFOUND; for (i = 0; i < git_tree_entrycount(parent); i++) { entry = git_tree_entry_byindex(parent, i); if (!git__ishex(git_tree_entry_name(entry))) continue; if (S_ISDIR(git_tree_entry_filemode(entry)) && strlen(git_tree_entry_name(entry)) == 2 && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) return git_tree_lookup(out, repo, git_tree_entry_id(entry)); /* Not a DIR, so do we have an already existing blob? */ if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) return GIT_EEXISTS; } return GIT_ENOTFOUND; }
int CRepositoryBrowser::ReadTreeRecursive(git_repository &repo, git_tree * tree, CShadowFilesTree * treeroot) { size_t count = git_tree_entrycount(tree); for (size_t i = 0; i < count; ++i) { const git_tree_entry *entry = git_tree_entry_byindex(tree, i); if (entry == NULL) continue; const int mode = git_tree_entry_filemode(entry); CString base = CUnicodeUtils::GetUnicode(git_tree_entry_name(entry), CP_UTF8); const git_oid *oid = git_tree_entry_id(entry); CShadowFilesTree * pNextTree = &treeroot->m_ShadowTree[base]; pNextTree->m_sName = base; pNextTree->m_pParent = treeroot; pNextTree->m_hash = CGitHash((char *)oid->id); if (mode == GIT_FILEMODE_COMMIT) pNextTree->m_bSubmodule = true; else if (mode & S_IFDIR) { pNextTree->m_bFolder = true; TVINSERTSTRUCT tvinsert = {0}; tvinsert.hParent = treeroot->m_hTree; tvinsert.hInsertAfter = TVI_SORT; tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE; tvinsert.itemex.pszText = base.GetBuffer(base.GetLength()); tvinsert.itemex.lParam = (LPARAM)pNextTree; tvinsert.itemex.iImage = m_nIconFolder; tvinsert.itemex.iSelectedImage = m_nOpenIconFolder; pNextTree->m_hTree = m_RepoTree.InsertItem(&tvinsert); base.ReleaseBuffer(); git_object *object = nullptr; git_tree_entry_to_object(&object, &repo, entry); if (object == nullptr) continue; ReadTreeRecursive(repo, (git_tree*)object, pNextTree); git_object_free(object); } else { git_blob * blob = nullptr; git_blob_lookup(&blob, &repo, oid); if (blob == NULL) continue; pNextTree->m_iSize = git_blob_rawsize(blob); git_blob_free(blob); } } return 0; }
static VALUE rb_git_treeentry_fromC(const git_tree_entry *entry) { VALUE rb_entry; VALUE type; if (!entry) return Qnil; rb_entry = rb_hash_new(); rb_hash_aset(rb_entry, CSTR2SYM("name"), rugged_str_new2(git_tree_entry_name(entry), NULL)); rb_hash_aset(rb_entry, CSTR2SYM("oid"), rugged_create_oid(git_tree_entry_id(entry))); rb_hash_aset(rb_entry, CSTR2SYM("filemode"), INT2FIX(git_tree_entry_filemode(entry))); switch(git_tree_entry_type(entry)) { case GIT_OBJ_TREE: type = CSTR2SYM("tree"); break; case GIT_OBJ_BLOB: type = CSTR2SYM("blob"); break; default: type = Qnil; break; } rb_hash_aset(rb_entry, CSTR2SYM("type"), type); return rb_entry; }
PyObject * TreeEntry_id__get__(TreeEntry *self) { const git_oid *oid; oid = git_tree_entry_id(self->entry); return git_oid_to_python(oid); }
static void assert_workdir_matches_tree( git_repository *repo, const git_oid *id, const char *root, bool recurse) { git_object *obj; git_tree *tree; size_t i, max_i; git_buf path = GIT_BUF_INIT; if (!root) root = git_repository_workdir(repo); cl_assert(root); cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJ_ANY)); cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJ_TREE)); git_object_free(obj); max_i = git_tree_entrycount(tree); for (i = 0; i < max_i; ++i) { const git_tree_entry *te = git_tree_entry_byindex(tree, i); cl_assert(te); cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te))); switch (git_tree_entry_type(te)) { case GIT_OBJ_COMMIT: assert_dir_exists(path.ptr); break; case GIT_OBJ_TREE: assert_dir_exists(path.ptr); if (recurse) assert_workdir_matches_tree( repo, git_tree_entry_id(te), path.ptr, true); break; case GIT_OBJ_BLOB: switch (git_tree_entry_filemode(te)) { case GIT_FILEMODE_BLOB: case GIT_FILEMODE_BLOB_EXECUTABLE: assert_file_exists(path.ptr); /* because of cross-platform, don't confirm exec bit yet */ break; case GIT_FILEMODE_LINK: cl_assert_(git_path_exists(path.ptr), path.ptr); /* because of cross-platform, don't confirm link yet */ break; default: cl_assert(false); /* really?! */ } break; default: cl_assert(false); /* really?!! */ } } git_tree_free(tree); git_buf_free(&path); }
git_blob *git_tree_entry_blob(git_repository *repo, const git_tree_entry *entry) { const git_oid *id = git_tree_entry_id(entry); git_blob *blob; if (git_blob_lookup(&blob, repo, id)) return NULL; return blob; }
static VALUE rb_git_tree_entry_sha_GET(VALUE self) { rugged_tree_entry *tree_entry; char out[40]; Data_Get_Struct(self, rugged_tree_entry, tree_entry); git_oid_fmt(out, git_tree_entry_id(tree_entry->entry)); return rugged_str_new(out, 40, NULL); }
PyObject * TreeEntry_repr(TreeEntry *self) { char str[GIT_OID_HEXSZ + 1] = { 0 }; const char *typename; typename = git_object_type2string(git_tree_entry_type(self->entry)); git_oid_fmt(str, git_tree_entry_id(self->entry)); return PyString_FromFormat("pygit2.TreeEntry('%s', %s, %s)", git_tree_entry_name(self->entry), typename, str); }
PyObject * TreeEntry_to_object(TreeEntry *self) { const git_oid *entry_oid; Repository *repo; repo = ((Object*)(self->owner))->repo; entry_oid = git_tree_entry_id(self->entry); return lookup_object(repo, entry_oid, GIT_OBJ_ANY); }
static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool) { git_repository *repo; size_t i, j, nentries, ntrees; int error; repo = git_tree_owner(tree); git_oid_cpy(&cache->oid, git_tree_id(tree)); nentries = git_tree_entrycount(tree); /* * We make sure we know how many trees we need to allocate for * so we don't have to realloc and change the pointers for the * parents. */ ntrees = 0; for (i = 0; i < nentries; i++) { const git_tree_entry *entry; entry = git_tree_entry_byindex(tree, i); if (git_tree_entry_filemode(entry) == GIT_FILEMODE_TREE) ntrees++; } cache->children_count = ntrees; cache->children = git_pool_mallocz(pool, ntrees * sizeof(git_tree_cache *)); GITERR_CHECK_ALLOC(cache->children); j = 0; for (i = 0; i < nentries; i++) { const git_tree_entry *entry; git_tree *subtree; entry = git_tree_entry_byindex(tree, i); if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE) continue; if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0) return error; if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0) return error; error = read_tree_recursive(cache->children[j], subtree, pool); git_tree_free(subtree); j++; if (error < 0) return error; } return 0; }
void add_tree_to_index(git_tree * tree, const char * prefix) { for (size_t i = 0; i < git_tree_entrycount(tree); i++) { /* Get the tree entry */ const git_tree_entry *tree_entry; tree_entry = git_tree_entry_byindex(tree,(unsigned int)i); if (tree_entry == NULL) { printf("Tree entry not found"); libgit_error(); } /* Get the oid of a tree entry */ const git_oid *entry_oid = git_tree_entry_id (tree_entry); /* is a sub directory ? */ git_tree * subtree; if (git_tree_lookup(&subtree, repo, entry_oid) == 0) { char * subprefix = xmalloc(sizeof(char)*1024); const char * dirname = git_tree_entry_name (tree_entry); git2__joinpath(subprefix, prefix, dirname); add_tree_to_index(subtree, subprefix); continue; } char * completename = xmalloc(sizeof(char)*1024); git2__joinpath(completename, prefix, git_tree_entry_name (tree_entry)); git_index_entry source_entry = { {0,0},//git_index_time ctime {0,0},//git_index_time mtime 0,//unsigned int dev 0,//unsigned int ino git_tree_entry_attributes(tree_entry),//unsigned int mode 0,//unsigned int uid 0,//unsigned int gid 0,//git_off_t file_size *entry_oid, 0, 0,//unsigned short flags_extended completename }; git_index_append2(index_cur, &source_entry); } }
static int find_blob(git_oid *blob, git_tree *tree, const char *target) { size_t i; const git_tree_entry *entry; for (i=0; i<git_tree_entrycount(tree); i++) { entry = git_tree_entry_byindex(tree, i); if (!strcmp(git_tree_entry_name(entry), target)) { /* found matching note object - return */ git_oid_cpy(blob, git_tree_entry_id(entry)); return 0; } } return GIT_ENOTFOUND; }
/** Show each entry with its type, id and attributes */ static void show_tree(const git_tree *tree) { size_t i, max_i = (int)git_tree_entrycount(tree); char oidstr[GIT_OID_HEXSZ + 1]; const git_tree_entry *te; for (i = 0; i < max_i; ++i) { te = git_tree_entry_byindex(tree, i); git_oid_tostr(oidstr, sizeof(oidstr), git_tree_entry_id(te)); printf("%06o %s %s\t%s\n", git_tree_entry_filemode(te), git_object_type2string(git_tree_entry_type(te)), oidstr, git_tree_entry_name(te)); } }
static int checksum_tree_callback (const char *root, const git_tree_entry *entry, void *data) { int iter_r = 1; int tmp_r; struct TreeWalkData *twdata = data; git_otype otype = git_tree_entry_type (entry); switch (otype) { case GIT_OBJ_TREE: case GIT_OBJ_BLOB: if (!checksum_object_id (twdata, git_tree_entry_id (entry), twdata->error)) { twdata->caught_error = TRUE; return -1; } break; case GIT_OBJ_COMMIT: { git_submodule *submod = NULL; tmp_r = git_submodule_lookup (&submod, twdata->repo, git_tree_entry_name (entry)); if (!handle_libgit_ret (tmp_r, twdata->error)) goto out; tmp_r = checksum_submodule (twdata, submod); if (tmp_r != 0) goto out; git_submodule_free (submod); } break; default: g_assert_not_reached (); } iter_r = 0; out: if (iter_r > 0) twdata->caught_error = TRUE; return iter_r; }
static int lookup_object_by_name(request_rec *r, git_repository *repo, const git_oid *tree_oid, const char *name, const git_oid **oid) { const git_tree_entry *entry; int ret; git_tree *tree; char *extname; int type = WIKI_NOTFOUND; ret = git_tree_lookup(&tree, repo, tree_oid); extname = apr_pstrcat(r->pool, name, ".md", NULL); entry = git_tree_entry_byname(tree, extname); //printf("extname: %s\n", extname); if (entry) { type = WIKI_MARKDOWN; }else{ entry = git_tree_entry_byname(tree, name); if (entry) { int attr = git_tree_entry_attributes(entry); if(S_ISDIR(attr)){ type = WIKI_DIR; }else{ type = WIKI_FOUND; } } } git_tree_free(tree); if (entry == NULL) { return WIKI_NOTFOUND; } *oid = git_tree_entry_id(entry); // is this safe? return type; }
static int TreewalkCB_FindFileRecentCommit(const char *root, const git_tree_entry *entry, void *payload) { TreewalkStruct *treewalkstruct = (TreewalkStruct *)payload; char folder[MAX_PATH] = {0}; strcpy_s(folder, root); strcat_s(folder, git_tree_entry_name(entry)); strcat_s(folder, "/"); if (strstr(treewalkstruct->folder, folder)) return 0; if (!strcmp(treewalkstruct->folder, root)) { if (!strcmp(git_tree_entry_name(entry), treewalkstruct->name)) { git_oid_cpy(&treewalkstruct->oid, git_tree_entry_id(entry)); return GIT_EUSER; } return 1; } return 1; }
int ReadTreeRecursive(git_repository &repo, git_tree * tree, CStringA base, int (*CallBack) (const unsigned char *, const char *, int, const char *, unsigned int, int, void *),void *data) { size_t count = git_tree_entrycount(tree); for (int i = 0; i < count; i++) { const git_tree_entry *entry = git_tree_entry_byindex(tree, i); if (entry == NULL) continue; int mode = git_tree_entry_attributes(entry); if( CallBack(git_tree_entry_id(entry)->id, base, base.GetLength(), git_tree_entry_name(entry), mode, 0, data) == READ_TREE_RECURSIVE ) { if(mode&S_IFDIR) { git_object *object = NULL; git_tree_entry_2object(&object, &repo, entry); if (object == NULL) continue; CStringA parent = base; parent += git_tree_entry_name(entry); parent += "/"; ReadTreeRecursive(repo, (git_tree*)object, parent, CallBack, data); git_object_free(object); } } } return 0; }
int CRepositoryBrowser::ReadTreeRecursive(git_repository &repo, const git_tree * tree, CShadowFilesTree * treeroot) { size_t count = git_tree_entrycount(tree); bool hasSubfolders = false; for (size_t i = 0; i < count; ++i) { const git_tree_entry *entry = git_tree_entry_byindex(tree, i); if (entry == NULL) continue; const int mode = git_tree_entry_filemode(entry); CString base = CUnicodeUtils::GetUnicode(git_tree_entry_name(entry), CP_UTF8); const git_oid *oid = git_tree_entry_id(entry); CShadowFilesTree * pNextTree = &treeroot->m_ShadowTree[base]; pNextTree->m_sName = base; pNextTree->m_pParent = treeroot; pNextTree->m_hash = CGitHash((char *)oid->id); if (mode == GIT_FILEMODE_COMMIT) pNextTree->m_bSubmodule = true; else if (mode & S_IFDIR) { hasSubfolders = true; pNextTree->m_bFolder = true; pNextTree->m_bLoaded = false; TVINSERTSTRUCT tvinsert = {0}; tvinsert.hParent = treeroot->m_hTree; tvinsert.hInsertAfter = TVI_SORT; tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE | TVIF_CHILDREN; tvinsert.itemex.pszText = base.GetBuffer(base.GetLength()); tvinsert.itemex.cChildren = 1; tvinsert.itemex.lParam = (LPARAM)pNextTree; tvinsert.itemex.iImage = m_nIconFolder; tvinsert.itemex.iSelectedImage = m_nOpenIconFolder; pNextTree->m_hTree = m_RepoTree.InsertItem(&tvinsert); base.ReleaseBuffer(); } else { if (mode == GIT_FILEMODE_BLOB_EXECUTABLE) pNextTree->m_bExecutable = true; if (mode == GIT_FILEMODE_LINK) pNextTree->m_bSymlink = true; CAutoBlob blob; git_blob_lookup(blob.GetPointer(), &repo, oid); if (!blob) continue; pNextTree->m_iSize = git_blob_rawsize(blob); } } if (!hasSubfolders) { TVITEM tvitem = { 0 }; tvitem.hItem = treeroot->m_hTree; tvitem.mask = TVIF_CHILDREN; tvitem.cChildren = 0; m_RepoTree.SetItem(&tvitem); } return 0; }
static int mne_git_tree_entry_cb(const char *root, git_tree_entry *entry, void *arg) { mne_git_walk_ctx *ctx = (mne_git_walk_ctx*)arg; git_otype type = git_tree_entry_type(entry); if (likely(type == GIT_OBJ_BLOB)) { const git_oid *blob_oid = git_tree_entry_id(entry); char *sha1 = malloc(sizeof(char) * GIT_OID_HEXSZ + 1); assert(sha1 != NULL); git_oid_tostr(sha1, GIT_OID_HEXSZ + 1, blob_oid); git_odb_object *blob_odb_object; git_odb_read(&blob_odb_object, odb, blob_oid); gpointer blob = g_hash_table_lookup(blobs, (gpointer)sha1); char **sha1_refs; if (blob == NULL) { ctx->distinct_blobs++; char *tmp_data = (char*)git_odb_object_data(blob_odb_object); int data_len = strlen(tmp_data); char *data = malloc(sizeof(char) * (data_len + 1)); memcpy(data, tmp_data, data_len); data[data_len] = 0; assert((strlen(root) + strlen(git_tree_entry_name(entry))) < MNE_MAX_PATH_LENGTH); char *path = malloc(sizeof(char) * MNE_MAX_PATH_LENGTH); assert(path != NULL); strcpy(path, root); strcat(path, git_tree_entry_name(entry)); /* TOOD: Check that the blob <-> path mapping is 1-1. */ g_hash_table_insert(paths, (gpointer)sha1, (gpointer)path); g_hash_table_insert(blobs, (gpointer)sha1, (gpointer)data); ctx->bytes += (unsigned long)(sizeof(char) * strlen(data)); sha1_refs = malloc(sizeof(char*) * total_refs); assert(sha1_refs != NULL); int i; for (i = 0; i < total_refs; i++) sha1_refs[i] = NULL; g_hash_table_insert(refs, (gpointer)sha1, (gpointer)sha1_refs); } else { sha1_refs = g_hash_table_lookup(refs, (gpointer)sha1); free(sha1); } git_odb_object_free(blob_odb_object); int i; for (i = 0; i < total_refs; i++) { if (sha1_refs[i] != NULL) continue; sha1_refs[i] = ctx->ref_name; break; } } return GIT_OK; }
/** * Merges two tree objects, producing a third tree. * The base tree allows the algorithm to distinguish between adds and deletes. * The algorithm always prefers the item from tree 1 when there is a conflict. */ static int sync_merge_trees(git_oid *out, git_repository *repo, const git_oid *base_id, const git_oid *id1, const git_oid *id2) { int e = 0; git_tree *base_tree = NULL; git_tree *tree1 = NULL; git_tree *tree2 = NULL; git_treebuilder *tb = NULL; size_t size1, size2; size_t i1 = 0; size_t i2 = 0; const git_tree_entry *e1 = NULL; const git_tree_entry *e2 = NULL; enum { ONLY1 = 1, ONLY2 = 2, BOTH = 3 } state = BOTH; git_check(git_tree_lookup(&base_tree, repo, base_id)); git_check(git_tree_lookup(&tree1, repo, id1)); git_check(git_tree_lookup(&tree2, repo, id2)); git_check(git_treebuilder_new(&tb, repo, NULL)); size1 = git_tree_entrycount(tree1); size2 = git_tree_entrycount(tree2); while (1) { // Advance to next file: if (state == ONLY1 || state == BOTH) { e1 = i1 < size1 ? git_tree_entry_byindex(tree1, i1++) : NULL; } if (state == ONLY2 || state == BOTH) { e2 = i2 < size2 ? git_tree_entry_byindex(tree2, i2++) : NULL; } // Determine state: if (e1 && e2) { int s = strcmp(git_tree_entry_name(e1), git_tree_entry_name(e2)); state = s < 0 ? ONLY1 : s > 0 ? ONLY2 : BOTH; } else if (e1 && !e2) { state = ONLY1; } else if (!e1 && e2) { state = ONLY2; } else { break; } // Grab the entry in question: const git_tree_entry *entry = (state == ONLY1 || state == BOTH) ? e1 : e2; const git_tree_entry *base_entry = git_tree_entry_byname(base_tree, git_tree_entry_name(entry)); // Decide what to do with the entry: if (state == BOTH && base_entry && GIT_OBJ_TREE == git_tree_entry_type(e1) && GIT_OBJ_TREE == git_tree_entry_type(e2) && GIT_OBJ_TREE == git_tree_entry_type(base_entry)) { // Merge sub-trees: git_oid new_tree; git_check(sync_merge_trees(&new_tree, repo, git_tree_entry_id(base_entry), git_tree_entry_id(e1), git_tree_entry_id(e2))); git_check(git_treebuilder_insert(NULL, tb, git_tree_entry_name(e1), &new_tree, git_tree_entry_filemode(e1))); } else if (state == BOTH && base_entry) { if (git_oid_cmp(git_tree_entry_id(base_entry), git_tree_entry_id(e1))) { // Entry `e1` has changes, so use that: git_check(git_treebuilder_insert(NULL, tb, git_tree_entry_name(e1), git_tree_entry_id(e1), git_tree_entry_filemode(e1))); } else { // Entry `e1` has no changes, so use `e2`: git_check(git_treebuilder_insert(NULL, tb, git_tree_entry_name(e2), git_tree_entry_id(e2), git_tree_entry_filemode(e2))); } } else if (state == BOTH || !base_entry) { // Entry was added, or already present: git_check(git_treebuilder_insert(NULL, tb, git_tree_entry_name(entry), git_tree_entry_id(entry), git_tree_entry_filemode(entry))); } // Otherwise, the entry was deleted. } // Write tree: git_check(git_treebuilder_write(out, tb)); exit: if (base_tree) git_tree_free(base_tree); if (tree1) git_tree_free(tree1); if (tree2) git_tree_free(tree2); if (tb) git_treebuilder_free(tb); return e; }
QByteArray PmrWorkspace::headFileContents(const QString &pFileName) { // Retrieve the contents of the given file name at the HEAD revision // Note: the below code is based on Repository::GetHeadBlob() from git-utils // (see https://github.com/atom/git-utils)... git_reference *head; if (git_repository_head(&head, mGitRepository) != GIT_OK) { return {}; } const git_oid *sha = git_reference_target(head); git_commit *commit; int commitStatus = git_commit_lookup(&commit, mGitRepository, sha); git_reference_free(head); if (commitStatus != GIT_OK) { return {}; } git_tree *tree; int treeStatus = git_commit_tree(&tree, commit); git_commit_free(commit); if (treeStatus != GIT_OK) { return {}; } git_tree_entry *treeEntry; if (git_tree_entry_bypath(&treeEntry, tree, pFileName.toUtf8().constData()) != GIT_OK) { git_tree_free(tree); return {}; } git_blob *blob = nullptr; const git_oid *blobSha = git_tree_entry_id(treeEntry); if ( (blobSha != nullptr) && (git_blob_lookup(&blob, mGitRepository, blobSha) != GIT_OK)) { blob = nullptr; } git_tree_entry_free(treeEntry); git_tree_free(tree); if (blob == nullptr) { return {}; } QByteArray res(static_cast<const char *>(git_blob_rawcontent(blob)), int(git_blob_rawsize(blob))); git_blob_free(blob); return res; }
void PmrWorkspace::stageFile(const QString &pPath, bool pStage) { // Un/stage the file, which path is given, and let people know of the // outcome if (isOpen()) { QByteArray relativePathByteArray = QDir(mPath).relativeFilePath(pPath).toUtf8(); const char *relativePath = relativePathByteArray.constData(); bool success = false; git_index *index; if (git_repository_index(&index, mGitRepository) == GIT_OK) { if (pStage) { uint statusFlags = 0; git_status_file(&statusFlags, mGitRepository, relativePath); if ((statusFlags & GIT_STATUS_WT_DELETED) != 0) { success = git_index_remove_bypath(index, relativePath) == GIT_OK; } else { success = git_index_add_bypath(index, relativePath) == GIT_OK; } } else if (git_repository_head_unborn(mGitRepository) == 1) { success = git_index_remove_bypath(index, relativePath) == GIT_OK; } else { // We need to add a "reset stage" to the index, which means // getting the tree for HEAD and tree_entry for the file git_reference *head; if (git_repository_head(&head, mGitRepository) == GIT_OK) { git_tree *headTree; if (git_reference_peel(reinterpret_cast<git_object **>(&headTree), head, GIT_OBJECT_TREE) == GIT_OK) { git_tree_entry *headEntry; if (git_tree_entry_bypath(&headEntry, headTree, relativePath) == GIT_OK) { git_index_entry indexEntry; memset(&indexEntry, '\0', sizeof(git_index_entry)); indexEntry.id = *git_tree_entry_id(headEntry); indexEntry.mode = uint32_t(git_tree_entry_filemode(headEntry)); indexEntry.path = relativePath; git_index_add(index, &indexEntry); git_tree_entry_free(headEntry); success = true; } else { success = git_index_remove_bypath(index, relativePath) == GIT_OK; } git_tree_free(headTree); } git_reference_free(head); } } if (success) { git_index_write(index); } git_index_free(index); } if (!success) { emitGitError(tr("An error occurred while trying to stage %1.").arg(pPath)); } } }
static inline const git_oid * agb_git_tree_entry_id(const git_tree_entry * tree) { if(!tree) return NULL; return git_tree_entry_id(tree); }
PyObject * TreeEntry_hex__get__(TreeEntry *self) { return git_oid_to_py_str(git_tree_entry_id(self->entry)); }
/* * Dive directory, name is [[yyyy-]mm-]nn-ddd-hh:mm:ss[~hex] in older git repositories * but [[yyyy-]mm-]nn-ddd-hh=mm=ss[~hex] in newer repos as ':' is an illegal character for Windows files * and 'timeoff' points to what should be the time part of * the name (the first digit of the hour). * * The root path will be of the form yyyy/mm[/tripdir], */ static int dive_directory(const char *root, const git_tree_entry *entry, const char *name, int timeoff) { int yyyy = -1, mm = -1, dd = -1; int h, m, s; int mday_off, month_off, year_off; struct tm tm; /* Skip the '-' before the time */ mday_off = timeoff; if (!mday_off || name[--mday_off] != '-') return GIT_WALK_SKIP; /* Skip the day name */ while (mday_off > 0 && name[--mday_off] != '-') /* nothing */; mday_off = mday_off - 2; month_off = mday_off - 3; year_off = month_off - 5; if (mday_off < 0) return GIT_WALK_SKIP; /* Get the time of day -- parse both time formats so we can read old repos when not on Windows */ if (sscanf(name+timeoff, "%d:%d:%d", &h, &m, &s) != 3 && sscanf(name+timeoff, "%d=%d=%d", &h, &m, &s) != 3) return GIT_WALK_SKIP; if (!validate_time(h, m, s)) return GIT_WALK_SKIP; /* * Using the "git_tree_walk()" interface is simple, but * it kind of sucks as an interface because there is * no sane way to pass the hierarchy to the callbacks. * The "payload" is a fixed one-time thing: we'd like * the "current trip" to be passed down to the dives * that get parsed under that trip, but we can't. * * So "active_trip" is not the trip that is in the hierarchy * _above_ us, it's just the trip that was _before_ us. But * if a dive is not in a trip at all, we can't tell. * * We could just do a better walker that passes the * return value around, but we hack around this by * instead looking at the one hierarchical piece of * data we have: the pathname to the current entry. * * This is pretty hacky. The magic '8' is the length * of a pathname of the form 'yyyy/mm/'. */ if (strlen(root) == 8) finish_active_trip(); /* * Get the date. The day of the month is in the dive directory * name, the year and month might be in the path leading up * to it. */ dd = atoi(name + mday_off); if (year_off < 0) { if (sscanf(root, "%d/%d", &yyyy, &mm) != 2) return GIT_WALK_SKIP; } else yyyy = atoi(name + year_off); if (month_off >= 0) mm = atoi(name + month_off); if (!validate_date(yyyy, mm, dd)) return GIT_WALK_SKIP; /* Ok, close enough. We've gotten sufficient information */ memset(&tm, 0, sizeof(tm)); tm.tm_hour = h; tm.tm_min = m; tm.tm_sec = s; tm.tm_year = yyyy; tm.tm_mon = mm-1; tm.tm_mday = dd; finish_active_dive(); active_dive = create_new_dive(utc_mktime(&tm)); memcpy(active_dive->git_id, git_tree_entry_id(entry)->id, 20); return GIT_WALK_OK; }