Skip to content

Commit 9cfa7b6

Browse files
authored
Merge pull request #941 from AppImage/fix-cli
Fix various CLI behavior problems
2 parents d3280e0 + c6d0336 commit 9cfa7b6

File tree

1 file changed

+69
-64
lines changed

1 file changed

+69
-64
lines changed

src/appimagetool.c

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -552,62 +552,6 @@ main (int argc, char *argv[])
552552
if (showVersionOnly)
553553
exit(0);
554554

555-
/* Parse VERSION environment variable.
556-
* We cannot use g_environ_getenv (g_get_environ() since it is too new for CentOS 6
557-
* Also, if VERSION is not set and -g is called and if git is on the path, use
558-
* git rev-parse --short HEAD
559-
* TODO: Might also want to somehow make use of
560-
* git rev-parse --abbrev-ref HEAD
561-
* git log -1 --format=%ci */
562-
gchar* version_env = getenv("VERSION");
563-
564-
if (guess_update_information) {
565-
char* gitPath = g_find_program_in_path("git");
566-
567-
if (gitPath != NULL) {
568-
if (version_env == NULL) {
569-
GError* error = NULL;
570-
gchar* out = NULL;
571-
572-
char command_line[] = "git rev-parse --short HEAD";
573-
574-
// *not* the exit code! must be interpreted via g_spawn_check_exit_status!
575-
int exit_status = -1;
576-
577-
// g_spawn_command_line_sync returns whether the program succeeded
578-
gint ret = g_spawn_command_line_sync(command_line, &out, NULL, &exit_status, &error);
579-
580-
if (ret != 0 || error != NULL) {
581-
// g_spawn_command_line_sync might have set error already, in that case we don't want to overwrite
582-
if (error == NULL) {
583-
// to get a proper error message, we now fetch the message via the returned exit code
584-
// the call returns false if the call failed, and this is what we expect to have happened
585-
// hence we can assume that there must be an error in GLib if it returned true
586-
if (g_spawn_check_exit_status(exit_status, &error)) {
587-
g_printerr("Failed to run 'git rev-parse --short HEAD, but GLib says the process didn't exit abnormally");
588-
}
589-
}
590-
591-
if (error == NULL) {
592-
g_printerr("Failed to run 'git rev-parse --short HEAD, but failed to interpret GLib error state: %d\n", exit_status);
593-
} else {
594-
g_printerr("Failed to run 'git rev-parse --short HEAD: %s (code %d)\n", error->message, error->code);
595-
}
596-
} else {
597-
version_env = g_strstrip(out);
598-
599-
if (version_env != NULL) {
600-
g_printerr("NOTE: Using the output of 'git rev-parse --short HEAD' as the version:\n");
601-
g_printerr(" %s\n", version_env);
602-
g_printerr(" Please set the $VERSION environment variable if this is not intended\n");
603-
}
604-
}
605-
}
606-
}
607-
608-
free(gitPath);
609-
}
610-
611555
if(!((0 == strcmp(sqfs_comp, "gzip")) || (0 ==strcmp(sqfs_comp, "xz"))))
612556
die("Only gzip (faster execution, larger files) and xz (slower execution, smaller files) compression is supported at the moment. Let us know if there are reasons for more, should be easy to add. You could help the project by doing some systematic size/performance measurements. Watch for size, execution speed, and zsync delta size.");
613557
/* Check for dependencies here. Better fail early if they are not present. */
@@ -656,7 +600,63 @@ main (int argc, char *argv[])
656600
}
657601

658602
/* If the first argument is a directory, then we assume that we should package it */
659-
if (g_file_test (remaining_args[0], G_FILE_TEST_IS_DIR)){
603+
if (g_file_test(remaining_args[0], G_FILE_TEST_IS_DIR)) {
604+
/* Parse VERSION environment variable.
605+
* We cannot use g_environ_getenv (g_get_environ() since it is too new for CentOS 6
606+
* Also, if VERSION is not set and -g is called and if git is on the path, use
607+
* git rev-parse --short HEAD
608+
* TODO: Might also want to somehow make use of
609+
* git rev-parse --abbrev-ref HEAD
610+
* git log -1 --format=%ci */
611+
gchar* version_env = getenv("VERSION");
612+
613+
if (guess_update_information) {
614+
char* gitPath = g_find_program_in_path("git");
615+
616+
if (gitPath != NULL) {
617+
if (version_env == NULL) {
618+
GError* error = NULL;
619+
gchar* out = NULL;
620+
621+
char command_line[] = "git rev-parse --short HEAD";
622+
623+
// *not* the exit code! must be interpreted via g_spawn_check_exit_status!
624+
int exit_status = -1;
625+
626+
// g_spawn_command_line_sync returns whether the program succeeded
627+
gint ret = g_spawn_command_line_sync(command_line, &out, NULL, &exit_status, &error);
628+
629+
if (ret != 0 || error != NULL) {
630+
// g_spawn_command_line_sync might have set error already, in that case we don't want to overwrite
631+
if (error == NULL) {
632+
// to get a proper error message, we now fetch the message via the returned exit code
633+
// the call returns false if the call failed, and this is what we expect to have happened
634+
// hence we can assume that there must be an error in GLib if it returned true
635+
if (g_spawn_check_exit_status(exit_status, &error)) {
636+
g_printerr("Failed to run 'git rev-parse --short HEAD, but GLib says the process didn't exit abnormally");
637+
}
638+
}
639+
640+
if (error == NULL) {
641+
g_printerr("Failed to run 'git rev-parse --short HEAD, but failed to interpret GLib error state: %d\n", exit_status);
642+
} else {
643+
g_printerr("Failed to run 'git rev-parse --short HEAD: %s (code %d)\n", error->message, error->code);
644+
}
645+
} else {
646+
version_env = g_strstrip(out);
647+
648+
if (version_env != NULL) {
649+
g_printerr("NOTE: Using the output of 'git rev-parse --short HEAD' as the version:\n");
650+
g_printerr(" %s\n", version_env);
651+
g_printerr(" Please set the $VERSION environment variable if this is not intended\n");
652+
}
653+
}
654+
}
655+
}
656+
657+
free(gitPath);
658+
}
659+
660660
char *destination;
661661
char source[PATH_MAX];
662662
realpath(remaining_args[0], source);
@@ -1344,13 +1344,18 @@ main (int argc, char *argv[])
13441344
fprintf(stderr, "Please consider submitting your AppImage to AppImageHub, the crowd-sourced\n");
13451345
fprintf(stderr, "central directory of available AppImages, by opening a pull request\n");
13461346
fprintf(stderr, "at https://github.com/AppImage/appimage.github.io\n");
1347-
}
1348-
1349-
/* If the first argument is a regular file, then we assume that we should unpack it */
1350-
if (g_file_test (remaining_args[0], G_FILE_TEST_IS_REGULAR)){
1351-
fprintf (stdout, "%s is a file, assuming it is an AppImage and should be unpacked\n", remaining_args[0]);
1347+
1348+
return 0;
1349+
} else if (g_file_test(remaining_args[0], G_FILE_TEST_IS_REGULAR)) {
1350+
/* If the first argument is a regular file, then we assume that we should unpack it */
1351+
fprintf(stdout, "%s is a file, assuming it is an AppImage and should be unpacked\n", remaining_args[0]);
13521352
die("To be implemented");
1353+
return 1;
1354+
} else {
1355+
fprintf(stderr, "Error: no such file or directory: %s\n", remaining_args[0]);
1356+
return 1;
13531357
}
1354-
1355-
return 0;
1358+
1359+
// should never be reached
1360+
return 1;
13561361
}

0 commit comments

Comments
 (0)