Skip to content

Commit

Permalink
Move nongit to the Rust side
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 26, 2023
1 parent 52b15b0 commit e6fe5cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/cinnabar-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,6 @@ const struct object_id *get_worktree_head_oid(const struct worktree *wr)
return &wr->head_oid;
}

int nongit = 0;

extern NORETURN void do_panic(const char *err, size_t len);

static NORETURN void die_panic(const char *err, va_list params)
Expand All @@ -355,8 +353,10 @@ static NORETURN void die_panic(const char *err, va_list params)
do_panic(msg, (size_t)(len < 0) ? 0 : len);
}

void init_cinnabar(const char *argv0)
int init_cinnabar(const char *argv0)
{
int nongit = 0;

set_die_routine(die_panic);

// Initialization from common-main.c.
Expand All @@ -377,6 +377,8 @@ void init_cinnabar(const char *argv0)
cleanup_git_config();
save_commit_buffer = 0;
warn_on_object_refname_ambiguity = 0;

return !nongit;
}

int common_exit(const char *file, int line, int code)
Expand Down
2 changes: 1 addition & 1 deletion src/cinnabar-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void remote_get_url(const struct remote *remote, const char * const **url,
int* url_nr);
int remote_skip_default_update(const struct remote *remote);

void init_cinnabar(const char *argv0);
int init_cinnabar(const char *argv0);

void create_git_tree(const struct object_id *tree_id,
const struct object_id *ref_tree,
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ extern "C" {
#[cfg(windows)]
fn wmain(argc: c_int, argv: *const *const u16) -> c_int;

fn init_cinnabar(argv0: *const c_char);

static nongit: c_int;
fn init_cinnabar(argv0: *const c_char) -> c_int;
}

pub unsafe fn do_reload(store: &mut Store, metadata: Option<CommitId>) {
Expand Down Expand Up @@ -217,7 +215,7 @@ fn dump_ref_updates() {
}

static MAYBE_INIT_CINNABAR_2: Lazy<Option<()>> = Lazy::new(|| unsafe {
if nongit != 0 {
if !HAS_GIT_REPO {
return None;
}
let c = get_oid_committish(METADATA_REF.as_bytes());
Expand Down Expand Up @@ -4609,6 +4607,8 @@ fn git_remote_hg(remote: OsString, mut url: OsString) -> Result<c_int, String> {
Ok(0)
}

static mut HAS_GIT_REPO: bool = false;

#[no_mangle]
unsafe extern "C" fn cinnabar_main(_argc: c_int, argv: *const *const c_char) -> c_int {
let now = Instant::now();
Expand Down Expand Up @@ -4639,7 +4639,7 @@ unsafe extern "C" fn cinnabar_main(_argc: c_int, argv: *const *const c_char) ->
);
}
}));
init_cinnabar(exe.as_deref().unwrap_or(argv0).as_ptr());
HAS_GIT_REPO = init_cinnabar(exe.as_deref().unwrap_or(argv0).as_ptr()) != 0;
logging::init(now);

let ret = match argv0_path.file_stem().and_then(OsStr::to_str) {
Expand Down

0 comments on commit e6fe5cf

Please sign in to comment.