]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
edit-util: catch and warn about edits outside of markers 34025/head
authorMike Yuan <me@yhndnzj.com>
Sat, 17 Aug 2024 20:36:19 +0000 (22:36 +0200)
committerMike Yuan <me@yhndnzj.com>
Sun, 18 Aug 2024 14:42:29 +0000 (16:42 +0200)
The users still periodically come back to #24208.
Let's add a detection for this hence.

src/shared/edit-util.c

index 5dd8595a36937c0afcff1f225e05137fa8293622..89ca9459e063b69fd6caec6279863ec08e8bc538 100644 (file)
@@ -315,10 +315,12 @@ static int run_editor(const EditFileContext *context) {
 static int strip_edit_temp_file(EditFile *e) {
         _cleanup_free_ char *old_contents = NULL, *tmp = NULL, *new_contents = NULL;
         const char *stripped;
+        bool with_marker;
         int r;
 
         assert(e);
         assert(e->context);
+        assert(!e->context->marker_start == !e->context->marker_end);
         assert(e->temp);
 
         r = read_full_file(e->temp, &old_contents, NULL);
@@ -329,12 +331,12 @@ static int strip_edit_temp_file(EditFile *e) {
         if (!tmp)
                 return log_oom();
 
-        if (e->context->marker_start && !e->context->stdin) {
+        with_marker = e->context->marker_start && !e->context->stdin;
+
+        if (with_marker) {
                 /* Trim out the lines between the two markers */
                 char *contents_start, *contents_end;
 
-                assert(e->context->marker_end);
-
                 contents_start = strstrafter(tmp, e->context->marker_start) ?: tmp;
 
                 contents_end = strstr(contents_start, e->context->marker_end);
@@ -345,8 +347,28 @@ static int strip_edit_temp_file(EditFile *e) {
         } else
                 stripped = strstrip(tmp);
 
-        if (isempty(stripped))
+        if (isempty(stripped)) {
+                /* People keep coming back to #24208 due to edits outside of markers. Let's detect this
+                 * and point them in the right direction. */
+                if (with_marker)
+                        for (const char *p = old_contents;;) {
+                                p = skip_leading_chars(p, WHITESPACE);
+                                if (*p == '\0')
+                                        break;
+                                if (*p != '#') {
+                                        log_warning("Found modifications outside of the staging area, which would be discarded.");
+                                        break;
+                                }
+
+                                /* Skip the whole line if commented out */
+                                p = strchr(p, '\n');
+                                if (!p)
+                                        break;
+                                p++;
+                        }
+
                 return 0; /* File is empty (has no real changes) */
+        }
 
         /* Trim prefix and suffix, but ensure suffixed by single newline */
         new_contents = strjoin(stripped, "\n");