Fossil SCM

Initial skeleton for artifact_to_json(). Still lots to do here.

stephan 2025-03-24 12:18 trunk
Commit 1c248f4de10f91d59dedcd559ce53751b39654b64babfc3d96212210d6ad6559
1 file changed +58
--- src/manifest.c
+++ src/manifest.c
@@ -2911,5 +2911,63 @@
29112911
if( g.argc!=3 ) usage("RECORDID");
29122912
rid = name_to_rid(g.argv[2]);
29132913
content_get(rid, &content);
29142914
manifest_crosslink(rid, &content, MC_NONE);
29152915
}
2916
+
2917
+void artifact_to_json(Manifest *p, Blob *pOut){
2918
+ char * zTmp;
2919
+ blob_reset(pOut);
2920
+ blob_append_literal(pOut, "{");
2921
+ blob_appendf(pOut, "\"rid\": %d, ", p->rid);
2922
+ zTmp = rid_to_uuid(p->rid);
2923
+ blob_appendf(pOut, "\"uuid\": %!j", zTmp);
2924
+ fossil_free(zTmp);
2925
+ blob_append_literal(pOut, "}");
2926
+}
2927
+
2928
+/*
2929
+** Convenience wrapper around artifact_to_json() which accepts any
2930
+** artifact name which is legal for symbolic_name_to_rid(). On success
2931
+** it returns the rid of the artifact. Returns 0 if no such artifact
2932
+** exists and a negative value if the name is ambiguous.
2933
+**
2934
+** If it returns a valid rid, pOut will be cleared before emitting the
2935
+** JSON to it.
2936
+**
2937
+*/
2938
+int artifact_to_json_by_name(const char *zName, Blob *pOut){
2939
+ int rid;
2940
+
2941
+ rid = symbolic_name_to_rid(zName, 0);
2942
+ if( rid>0 ){
2943
+ Manifest * const p = manifest_get(rid, CFTYPE_ANY, 0);
2944
+ assert(p && "Is it possible to fail this if rid is a phantom?");
2945
+ artifact_to_json(p, pOut);
2946
+ manifest_destroy(p);
2947
+ }
2948
+ return rid;
2949
+}
2950
+
2951
+/*
2952
+** COMMAND: test-artifact-to-json
2953
+**
2954
+** Usage: %fossil test-artifact-to-json symbolic-name [...names]
2955
+**
2956
+** Tests the artifact_to_json() and artifact_to_json_by_name() APIs.
2957
+*/
2958
+void test_manifest_to_json(void){
2959
+ int i;
2960
+ Blob b = empty_blob;
2961
+
2962
+ db_find_and_open_repository(0,0);
2963
+ for( i=2; i<g.argc; ++i ){
2964
+ char const *zName = g.argv[i];
2965
+ const int rc = artifact_to_json_by_name(zName, &b);
2966
+ if( rc<=0 ){
2967
+ fossil_warning("Error reading artifact %Q\n", zName);
2968
+ }else{
2969
+ fossil_print("%b\n", &b);
2970
+ }
2971
+ blob_reset(&b);
2972
+ }
2973
+}
29162974
--- src/manifest.c
+++ src/manifest.c
@@ -2911,5 +2911,63 @@
2911 if( g.argc!=3 ) usage("RECORDID");
2912 rid = name_to_rid(g.argv[2]);
2913 content_get(rid, &content);
2914 manifest_crosslink(rid, &content, MC_NONE);
2915 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2916
--- src/manifest.c
+++ src/manifest.c
@@ -2911,5 +2911,63 @@
2911 if( g.argc!=3 ) usage("RECORDID");
2912 rid = name_to_rid(g.argv[2]);
2913 content_get(rid, &content);
2914 manifest_crosslink(rid, &content, MC_NONE);
2915 }
2916
2917 void artifact_to_json(Manifest *p, Blob *pOut){
2918 char * zTmp;
2919 blob_reset(pOut);
2920 blob_append_literal(pOut, "{");
2921 blob_appendf(pOut, "\"rid\": %d, ", p->rid);
2922 zTmp = rid_to_uuid(p->rid);
2923 blob_appendf(pOut, "\"uuid\": %!j", zTmp);
2924 fossil_free(zTmp);
2925 blob_append_literal(pOut, "}");
2926 }
2927
2928 /*
2929 ** Convenience wrapper around artifact_to_json() which accepts any
2930 ** artifact name which is legal for symbolic_name_to_rid(). On success
2931 ** it returns the rid of the artifact. Returns 0 if no such artifact
2932 ** exists and a negative value if the name is ambiguous.
2933 **
2934 ** If it returns a valid rid, pOut will be cleared before emitting the
2935 ** JSON to it.
2936 **
2937 */
2938 int artifact_to_json_by_name(const char *zName, Blob *pOut){
2939 int rid;
2940
2941 rid = symbolic_name_to_rid(zName, 0);
2942 if( rid>0 ){
2943 Manifest * const p = manifest_get(rid, CFTYPE_ANY, 0);
2944 assert(p && "Is it possible to fail this if rid is a phantom?");
2945 artifact_to_json(p, pOut);
2946 manifest_destroy(p);
2947 }
2948 return rid;
2949 }
2950
2951 /*
2952 ** COMMAND: test-artifact-to-json
2953 **
2954 ** Usage: %fossil test-artifact-to-json symbolic-name [...names]
2955 **
2956 ** Tests the artifact_to_json() and artifact_to_json_by_name() APIs.
2957 */
2958 void test_manifest_to_json(void){
2959 int i;
2960 Blob b = empty_blob;
2961
2962 db_find_and_open_repository(0,0);
2963 for( i=2; i<g.argc; ++i ){
2964 char const *zName = g.argv[i];
2965 const int rc = artifact_to_json_by_name(zName, &b);
2966 if( rc<=0 ){
2967 fossil_warning("Error reading artifact %Q\n", zName);
2968 }else{
2969 fossil_print("%b\n", &b);
2970 }
2971 blob_reset(&b);
2972 }
2973 }
2974

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button