Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25b75cd

Browse files
committed
commit-graph: Create git_commit_graph as an abstraction for the file
This change does a medium-size refactor of the git_commit_graph_file and the interaction with the ODB. Now instead of the ODB owning a direct reference to the git_commit_graph_file, there will be an intermediate git_commit_graph. The main advantage of that is that now end users can explicitly set a git_commit_graph that is eagerly checked for errors, while still being able to lazily use the commit-graph in a regular ODB, if the file is present.
1 parent 248606e commit 25b75cd

File tree

10 files changed

+332
-181
lines changed

10 files changed

+332
-181
lines changed

fuzzers/commit_graph_fuzzer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
3131

3232
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
3333
{
34-
git_commit_graph_file cgraph = {{0}};
34+
git_commit_graph_file file = {{0}};
3535
git_commit_graph_entry e;
3636
git_buf commit_graph_buf = GIT_BUF_INIT;
3737
git_oid oid = {{0}};
@@ -62,19 +62,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
6262
git_buf_attach_notowned(&commit_graph_buf, (char *)data, size);
6363
}
6464

65-
if (git_commit_graph_parse(
66-
&cgraph,
65+
if (git_commit_graph_file_parse(
66+
&file,
6767
(const unsigned char *)git_buf_cstr(&commit_graph_buf),
6868
git_buf_len(&commit_graph_buf))
6969
< 0)
7070
goto cleanup;
7171

7272
/* Search for any oid, just to exercise that codepath. */
73-
if (git_commit_graph_entry_find(&e, &cgraph, &oid, GIT_OID_HEXSZ) < 0)
73+
if (git_commit_graph_entry_find(&e, &file, &oid, GIT_OID_HEXSZ) < 0)
7474
goto cleanup;
7575

7676
cleanup:
77-
git_commit_graph_close(&cgraph);
77+
git_commit_graph_file_close(&file);
7878
git_buf_dispose(&commit_graph_buf);
7979
return 0;
8080
}

include/git2/odb.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,21 @@ GIT_EXTERN(size_t) git_odb_num_backends(git_odb *odb);
544544
*/
545545
GIT_EXTERN(int) git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos);
546546

547+
/**
548+
* Set the git commit-graph for the ODB.
549+
*
550+
* After a successfull call, the ownership of the cgraph parameter will be
551+
* transferred to libgit2, and the caller should not free it.
552+
*
553+
* The commit-graph can also be unset by explicitly passing NULL as the cgraph
554+
* parameter.
555+
*
556+
* @param odb object database
557+
* @param cgraph the git commit-graph
558+
* @return 0 on success; error code otherwise
559+
*/
560+
GIT_EXTERN(int) git_odb_set_commit_graph(git_odb *odb, git_commit_graph *cgraph);
561+
547562
/** @} */
548563
GIT_END_DECL
549564
#endif

include/git2/sys/commit_graph.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_sys_git_commit_graph_h__
8+
#define INCLUDE_sys_git_commit_graph_h__
9+
10+
#include "git2/common.h"
11+
#include "git2/types.h"
12+
13+
/**
14+
* @file git2/sys/commit_graph.h
15+
* @brief Git commit-graph
16+
* @defgroup git_commit_graph Git commit-graph APIs
17+
* @ingroup Git
18+
* @{
19+
*/
20+
GIT_BEGIN_DECL
21+
22+
/**
23+
* Opens a `git_commit_graph` from a path to an objects directory.
24+
*
25+
* This finds, opens, and validates the `commit-graph` file.
26+
*
27+
* @param cgraph_out the `git_commit_graph` struct to initialize.
28+
* @param objects_dir the path to a git objects directory.
29+
* @return Zero on success; -1 on failure.
30+
*/
31+
GIT_EXTERN(int) git_commit_graph_open(git_commit_graph **cgraph_out, const char *objects_dir);
32+
33+
/**
34+
* Frees commit-graph data. This should only be called when memory allocated
35+
* using `git_commit_graph_open` is not returned to libgit2 because it was not
36+
* associated with the ODB through a successful call to
37+
* `git_odb_set_commit_graph`.
38+
*
39+
* @param cgraph the commit-graph object to free. If NULL, no action is taken.
40+
*/
41+
GIT_EXTERN(void) git_commit_graph_free(git_commit_graph *cgraph);
42+
43+
GIT_END_DECL
44+
45+
#endif

include/git2/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ typedef struct git_refdb git_refdb;
102102
/** A custom backend for refs */
103103
typedef struct git_refdb_backend git_refdb_backend;
104104

105+
/** A git commit-graph */
106+
typedef struct git_commit_graph git_commit_graph;
107+
105108
/**
106109
* Representation of an existing git repository,
107110
* including all its object contents

0 commit comments

Comments
 (0)